Skip to content

Commit

Permalink
Merge pull request #64 from BedrockStreaming/feat/allow-sf6
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool authored Dec 9, 2021
2 parents 494646f + 0ebd893 commit 9d6af2d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ jobs:
runs-on: ubuntu-18.04
strategy:
matrix:
version: ['7.1', 7.2', '7.3', '7.4', '8.0']
version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
flags: ['', '--prefer-lowest']
fail-fast: false
steps:
- uses: actions/checkout@master
- uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LogBridgeBundle [![Build Status](https://travis-ci.org/M6Web/LogBridgeBundle.svg?branch=master)](https://travis-ci.org/M6Web/LogBridgeBundle)
# LogBridgeBundle [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FBedrockStreaming%2FLogBridgeBundle%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/BedrockStreaming/LogBridgeBundle/goto?ref=master)

Symfony Bundle to log Request/Response with Monolog.

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
"prefer-stable": true,
"require": {
"php": ">=7.1.3",
"symfony/config": "^4.4|^5.0",
"symfony/yaml": "^4.4|^5.0",
"symfony/routing": "^4.4|^5.0",
"symfony/expression-language": "^4.4|^5.0",
"symfony/security-core": "^4.4|^5.0"
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/yaml": "^4.4|^5.0|^6.0",
"symfony/routing": "^4.4|^5.0|^6.0",
"symfony/expression-language": "^4.4|^5.0|^6.0",
"symfony/security-core": "^4.4|^5.0|^6.0"
},
"require-dev": {
"atoum/atoum": "^3.4|^4.0",
"friendsofphp/php-cs-fixer": "^2.19",
"m6web/symfony2-coding-standard": "~1.2",
"m6web/php-cs-fixer-config": "^1.0",
"symfony/http-foundation": "^4.4|^5.0"
"symfony/http-foundation": "^4.4|^5.0|^6.0"
},
"extra": {
"branch-alias": {
Expand Down
11 changes: 8 additions & 3 deletions src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ public function getLogContext(Request $request, Response $response, array $optio
*/
protected function getUsername()
{
if ($this->tokenStorage && $this->tokenStorage->getToken()) {
return $this->tokenStorage->getToken()->getUsername();
if (!$this->tokenStorage || !$token = $this->tokenStorage->getToken()) {
return '';
}

return '';
// compatibility Symfony < 6
if (method_exists('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', 'getUsername')) {
return $token->getUsername();
}

return $token->getUserIdentifier();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/M6Web/Bundle/LogBridgeBundle/Tests/Units/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use atoum;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;

/**
* Base Class Unit test
Expand All @@ -14,7 +15,7 @@ protected function getMockedRouterCollection()
{
$collection = new \mock\Symfony\Component\Routing\RouteCollection();
$collection->getMockController()->get = function($name) {
return $name != 'invalid_route' ? true : false;
return $name != 'invalid_route' ? new Route('/path') : null;
};

return $collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace M6Web\Bundle\LogBridgeBundle\Tests\Units\Formatter;

use atoum;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\User;
use M6Web\Bundle\LogBridgeBundle\Formatter;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -14,21 +15,30 @@
class DefaultFormatter extends atoum
{
const ENVIRONMENT = 'test';
const USERNAME = 'test-username';
const PASSWORD = 'test-password';

private function getUser()
{
return new User('test', 'password');
if (class_exists(User::class)) {
return new User(self::USERNAME, self::PASSWORD);
}

return new InMemoryUser(self::USERNAME, self::PASSWORD);
}

private function getMockedToken(User $user = null)
private function getMockedToken()
{
$user = $user ?: $this->getUser();
$token = new \mock\Symfony\Component\Security\Core\Authentication\TokenInterface();
$usernameMethod = 'getUserIdentifier';
if (method_exists('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', 'getUsername')) {
// compatibility Symfony < 6
$usernameMethod = 'getUsername';
}

$token->getMockController()->getUsername = $user->getUsername();
$token->getMockController()->getUser = $user;
$token->getMockController()->__toString = $user->getUsername();
$token->getMockController()->getCredentials = 'test';
$token = new \mock\Symfony\Component\Security\Core\Authentication\Token\TokenInterface();
$token->getMockController()->$usernameMethod = self::USERNAME;
$token->getMockController()->getUser = $this->getUser();
$token->getMockController()->__toString = self::USERNAME;

return $token;
}
Expand Down Expand Up @@ -85,7 +95,7 @@ public function testProvider()
->integer($logContext['status'])
->isEqualTo($status)
->string($logContext['user'])
->isEqualTo($tokenstorage->getToken()->getUsername())
->isEqualTo(self::USERNAME)
->string($logContext['key'])
->isEqualTo(sprintf('%s.%s.%s.%s', self::ENVIRONMENT, $route, $method, $status))
;
Expand All @@ -104,14 +114,10 @@ public function testPostProvider()
];


$request = new \mock\Symfony\Component\HttpFoundation\Request([], $post);
$request->getMockController()->getMethod = 'POST';
$request = new Request([], $post, [], [], [], ['REQUEST_METHOD' => 'POST']);

$response = new Response('Body content response');
$tokenstorage = $this->getMockedTokenStorage();
$route = $request->get('_route');
$method = $request->getMethod();
$status = $response->getStatusCode();

$this
->if($provider = $this->createProvider())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace M6Web\Bundle\LogBridgeBundle\Tests\Units\Formatter;

use atoum;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\User;
use M6Web\Bundle\LogBridgeBundle\Formatter;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -14,6 +15,8 @@
class ExceptionFormatter extends atoum
{
const ENVIRONMENT = 'test';
const USERNAME = 'test-username';
const PASSWORD = 'test-password';

/**
* @var string
Expand All @@ -30,18 +33,25 @@ public function beforeTestMethod($method)

private function getUser()
{
return new User('test', 'password');
if (class_exists(User::class)) {
return new User(self::USERNAME, self::PASSWORD);
}

return new InMemoryUser(self::USERNAME, self::PASSWORD);
}

private function getMockedToken(User $user = null)
private function getMockedToken()
{
$user = $user ?: $this->getUser();
$token = new \mock\Symfony\Component\Security\Core\Authentication\TokenInterface();
$usernameMethod = 'getUserIdentifier';
if (method_exists('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', 'getUsername')) {
// compatibility Symfony < 6
$usernameMethod = 'getUsername';
}

$token->getMockController()->getUsername = $user->getUsername();
$token->getMockController()->getUser = $user;
$token->getMockController()->__toString = $user->getUsername();
$token->getMockController()->getCredentials = 'test';
$token = new \mock\Symfony\Component\Security\Core\Authentication\Token\TokenInterface();
$token->getMockController()->$usernameMethod = self::USERNAME;
$token->getMockController()->getUser = $this->getUser();
$token->getMockController()->__toString = self::USERNAME;

return $token;
}
Expand Down Expand Up @@ -111,7 +121,7 @@ public function testProvider()
->integer($logContext['status'])
->isEqualTo($status)
->string($logContext['user'])
->isEqualTo($tokenstorage->getToken()->getUsername())
->isEqualTo(self::USERNAME)
->string($logContext['key'])
->isEqualTo(sprintf('%s.%s.%s.%s', self::ENVIRONMENT, $route, $method, $status))
;
Expand Down

0 comments on commit 9d6af2d

Please sign in to comment.