Skip to content

Commit

Permalink
Fix phpunit tests (FriendsOfSymfony#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor_Rebega authored and Igor_Rebega committed Feb 2, 2022
1 parent 5940472 commit c397e64
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Propel/om/
Propel/map/
composer.lock
.php_cs.cache
.phpunit.result.cache
2 changes: 1 addition & 1 deletion Document/AuthCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace FOS\OAuthServerBundle\Document;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use FOS\OAuthServerBundle\Model\AuthCodeInterface;
use FOS\OAuthServerBundle\Model\AuthCodeManager as BaseAuthCodeManager;

Expand Down
2 changes: 1 addition & 1 deletion Document/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace FOS\OAuthServerBundle\Document;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use FOS\OAuthServerBundle\Model\TokenInterface;
use FOS\OAuthServerBundle\Model\TokenManager as BaseTokenManager;

Expand Down
25 changes: 25 additions & 0 deletions Tests/BypassFinalHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Tests;

use DG\BypassFinals;
use PHPUnit\Runner\BeforeTestHook;

final class BypassFinalHook implements BeforeTestHook
{
public function executeBeforeTest(string $test): void
{
BypassFinals::enable();
}
}
12 changes: 6 additions & 6 deletions Tests/Command/CleanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public function testItShouldRemoveExpiredToken(): void

$display = $tester->getDisplay();

$this->assertContains(sprintf('Removed %d items from %s storage.', $expiredAccessTokens, get_class($this->accessTokenManager)), $display);
$this->assertContains(sprintf('Removed %d items from %s storage.', $expiredRefreshTokens, get_class($this->refreshTokenManager)), $display);
$this->assertContains(sprintf('Removed %d items from %s storage.', $expiredAuthCodes, get_class($this->authCodeManager)), $display);
$this->assertStringContainsString(sprintf('Removed %d items from %s storage.', $expiredAccessTokens, get_class($this->accessTokenManager)), $display);
$this->assertStringContainsString(sprintf('Removed %d items from %s storage.', $expiredRefreshTokens, get_class($this->refreshTokenManager)), $display);
$this->assertStringContainsString(sprintf('Removed %d items from %s storage.', $expiredAuthCodes, get_class($this->authCodeManager)), $display);
}

/**
Expand All @@ -109,8 +109,8 @@ public function testItShouldNotRemoveExpiredTokensForOtherClasses(): void

$display = $tester->getDisplay();

$this->assertNotRegExp(sprintf('\'Removed (\d)+ items from %s storage.\'', get_class($this->accessTokenManager)), $display);
$this->assertNotRegExp(sprintf('\'Removed (\d)+ items from %s storage.\'', get_class($this->refreshTokenManager)), $display);
$this->assertNotRegExp(sprintf('\'Removed (\d)+ items from %s storage.\'', get_class($this->authCodeManager)), $display);
$this->assertDoesNotMatchRegularExpression(sprintf('\'Removed (\d)+ items from %s storage.\'', get_class($this->accessTokenManager)), $display);
$this->assertDoesNotMatchRegularExpression(sprintf('\'Removed (\d)+ items from %s storage.\'', get_class($this->refreshTokenManager)), $display);
$this->assertDoesNotMatchRegularExpression(sprintf('\'Removed (\d)+ items from %s storage.\'', get_class($this->authCodeManager)), $display);
}
}
4 changes: 2 additions & 2 deletions Tests/Command/CreateClientCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function testItShouldCreateClient($client): void

$output = $commandTester->getDisplay();

$this->assertContains('Client ID', $output);
$this->assertContains('Client Secret', $output);
$this->assertStringContainsString('Client ID', $output);
$this->assertStringContainsString('Client Secret', $output);
}

/**
Expand Down
195 changes: 117 additions & 78 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,45 @@ public function testShouldNotMandatoryServiceIfNotCustomDriverIsUsed(): void
$configuration = new Configuration();
$processor = new Processor();

$config = $processor->processConfiguration($configuration, [[
'db_driver' => 'orm',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
]]);

$this->assertArraySubset([
'db_driver' => 'orm',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'storage' => 'fos_oauth_server.storage.default',
'user_provider' => null,
'client_manager' => 'fos_oauth_server.client_manager.default',
'access_token_manager' => 'fos_oauth_server.access_token_manager.default',
'refresh_token_manager' => 'fos_oauth_server.refresh_token_manager.default',
'auth_code_manager' => 'fos_oauth_server.auth_code_manager.default',
],
], $config);
$config = $processor->processConfiguration($configuration, [
[
'db_driver' => 'orm',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
]
]);

$this->assertSame(
[
'db_driver' => 'orm',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'model_manager_name' => null,
'authorize' => [
'form' => [
'type' => 'fos_oauth_server_authorize',
'handler' => 'fos_oauth_server.authorize.form.handler.default',
'name' => 'fos_oauth_server_authorize_form',
'validation_groups' => [
0 => 'Authorize',
1 => 'Default',
],
],
],
'service' => [
'storage' => 'fos_oauth_server.storage.default',
'user_provider' => null,
'client_manager' => 'fos_oauth_server.client_manager.default',
'access_token_manager' => 'fos_oauth_server.access_token_manager.default',
'refresh_token_manager' => 'fos_oauth_server.refresh_token_manager.default',
'auth_code_manager' => 'fos_oauth_server.auth_code_manager.default',
'options' => [],
],
], $config);
}

public function testShouldMakeClientManagerServiceMandatoryIfCustomDriverIsUsed(): void
Expand All @@ -77,13 +93,15 @@ public function testShouldMakeClientManagerServiceMandatoryIfCustomDriverIsUsed(
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Invalid configuration for path "fos_oauth_server": The service client_manager must be set explicitly for custom db_driver.');

$processor->processConfiguration($configuration, [[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
]]);
$processor->processConfiguration($configuration, [
[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
]
]);
}

public function testShouldMakeAccessTokenManagerServiceMandatoryIfCustomDriverIsUsed(): void
Expand All @@ -94,16 +112,18 @@ public function testShouldMakeAccessTokenManagerServiceMandatoryIfCustomDriverIs
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Invalid configuration for path "fos_oauth_server": The service access_token_manager must be set explicitly for custom db_driver.');

$processor->processConfiguration($configuration, [[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
],
]]);
$processor->processConfiguration($configuration, [
[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
],
]
]);
}

public function testShouldMakeRefreshTokenManagerServiceMandatoryIfCustomDriverIsUsed(): void
Expand All @@ -114,17 +134,19 @@ public function testShouldMakeRefreshTokenManagerServiceMandatoryIfCustomDriverI
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Invalid configuration for path "fos_oauth_server": The service refresh_token_manager must be set explicitly for custom db_driver.');

$processor->processConfiguration($configuration, [[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
'access_token_manager' => 'anId',
],
]]);
$processor->processConfiguration($configuration, [
[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
'access_token_manager' => 'anId',
],
]
]);
}

public function testShouldMakeAuthCodeManagerServiceMandatoryIfCustomDriverIsUsed(): void
Expand All @@ -135,26 +157,44 @@ public function testShouldMakeAuthCodeManagerServiceMandatoryIfCustomDriverIsUse
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Invalid configuration for path "fos_oauth_server": The service auth_code_manager must be set explicitly for custom db_driver.');

$processor->processConfiguration($configuration, [[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
'access_token_manager' => 'anId',
'refresh_token_manager' => 'anId',
],
]]);
$processor->processConfiguration($configuration, [
[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
'access_token_manager' => 'anId',
'refresh_token_manager' => 'anId',
],
]
]);
}

public function testShouldLoadCustomDriverConfig(): void
{
$configuration = new Configuration();
$processor = new Processor();

$config = $processor->processConfiguration($configuration, [[
$config = $processor->processConfiguration($configuration, [
[
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'client_manager' => 'a_client_manager_id',
'access_token_manager' => 'an_access_token_manager_id',
'refresh_token_manager' => 'a_refresh_token_manager_id',
'auth_code_manager' => 'an_auth_code_manager_id',
],
]
]);

$this->assertSame([
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
Expand All @@ -165,22 +205,21 @@ public function testShouldLoadCustomDriverConfig(): void
'access_token_manager' => 'an_access_token_manager_id',
'refresh_token_manager' => 'a_refresh_token_manager_id',
'auth_code_manager' => 'an_auth_code_manager_id',
],
]]);

$this->assertArraySubset([
'db_driver' => 'custom',
'client_class' => 'aClientClass',
'access_token_class' => 'anAccessTokenClass',
'refresh_token_class' => 'aRefreshTokenClass',
'auth_code_class' => 'anAuthCodeClass',
'service' => [
'storage' => 'fos_oauth_server.storage.default',
'user_provider' => null,
'client_manager' => 'a_client_manager_id',
'access_token_manager' => 'an_access_token_manager_id',
'refresh_token_manager' => 'a_refresh_token_manager_id',
'auth_code_manager' => 'an_auth_code_manager_id',
'options' => [],
],
'model_manager_name' => null,
'authorize' => [
'form' => [
'type' => 'fos_oauth_server_authorize',
'handler' => 'fos_oauth_server.authorize.form.handler.default',
'name' => 'fos_oauth_server_authorize_form',
'validation_groups' => [
0 => 'Authorize',
1 => 'Default',
],
],
],
], $config);
}
Expand Down
21 changes: 8 additions & 13 deletions Tests/Document/AuthCodeManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

namespace FOS\OAuthServerBundle\Tests\Document;

use Doctrine\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\ODM\MongoDB\Query\Query;
use FOS\OAuthServerBundle\Document\AuthCodeManager;
use FOS\OAuthServerBundle\Model\AuthCodeInterface;
use stdClass;

/**
* @group time-sensitive
Expand Down Expand Up @@ -77,20 +78,14 @@ public function setUp(): void
parent::setUp();
}

public function testConstructWillSetParameters(): void
{
$this->assertAttributeSame($this->documentManager, 'dm', $this->instance);
$this->assertAttributeSame($this->className, 'class', $this->instance);
}

public function testGetClassWillReturnClassName(): void
{
$this->assertSame($this->className, $this->instance->getClass());
}

public function testFindAuthCodeBy(): void
{
$randomResult = \random_bytes(10);
$resultObject = new stdClass();
$criteria = [
\random_bytes(10),
];
Expand All @@ -99,10 +94,10 @@ public function testFindAuthCodeBy(): void
->expects($this->once())
->method('findOneBy')
->with($criteria)
->willReturn($randomResult)
->willReturn($resultObject)
;

$this->assertSame($randomResult, $this->instance->findAuthCodeBy($criteria));
$this->assertSame($resultObject, $this->instance->findAuthCodeBy($criteria));
}

public function testUpdateAuthCode(): void
Expand Down Expand Up @@ -188,7 +183,7 @@ public function testDeleteExpired(): void
->willReturn($queryBuilder)
;

$query = $this->getMockBuilder(AbstractQuery::class)
$query = $this->getMockBuilder(Query::class)
->disableOriginalConstructor()
->getMock()
;
Expand Down

0 comments on commit c397e64

Please sign in to comment.