|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSOAuthServerBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\OAuthServerBundle\Tests\Command; |
| 13 | + |
| 14 | +use FOS\OAuthServerBundle\Command\CleanCommand; |
| 15 | +use Symfony\Component\Console\Application; |
| 16 | +use Symfony\Component\Console\Tester\CommandTester; |
| 17 | +use Symfony\Component\DependencyInjection\Container; |
| 18 | + |
| 19 | +class CleanCommandTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var CleanCommand |
| 23 | + */ |
| 24 | + private $command; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Container |
| 28 | + */ |
| 29 | + private $container; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $command = new CleanCommand(); |
| 37 | + |
| 38 | + $application = new Application(); |
| 39 | + $application->add($command); |
| 40 | + |
| 41 | + $this->container = new Container(); |
| 42 | + |
| 43 | + $this->command = $application->find($command->getName()); |
| 44 | + $this->command->setContainer($this->container); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Delete expired tokens for provided classes. |
| 49 | + * |
| 50 | + * @dataProvider classProvider |
| 51 | + * |
| 52 | + * @param string $class A fully qualified class name. |
| 53 | + */ |
| 54 | + public function testItShouldRemoveExpiredToken($class) |
| 55 | + { |
| 56 | + $expiredAccessTokens = 5; |
| 57 | + $accessTokenManager = $this->getMock($class); |
| 58 | + $accessTokenManager |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('deleteExpired') |
| 61 | + ->will($this->returnValue($expiredAccessTokens)); |
| 62 | + |
| 63 | + $expiredRefreshTokens = 183; |
| 64 | + $refreshTokenManager = $this->getMock($class); |
| 65 | + $refreshTokenManager |
| 66 | + ->expects($this->once()) |
| 67 | + ->method('deleteExpired') |
| 68 | + ->will($this->returnValue($expiredRefreshTokens)); |
| 69 | + |
| 70 | + $expiredAuthCodes = 0; |
| 71 | + $authCodeManager = $this->getMock($class); |
| 72 | + $authCodeManager |
| 73 | + ->expects($this->once()) |
| 74 | + ->method('deleteExpired') |
| 75 | + ->will($this->returnValue($expiredAuthCodes)); |
| 76 | + |
| 77 | + $this->container->set('fos_oauth_server.access_token_manager', $accessTokenManager); |
| 78 | + $this->container->set('fos_oauth_server.refresh_token_manager', $refreshTokenManager); |
| 79 | + $this->container->set('fos_oauth_server.auth_code_manager', $authCodeManager); |
| 80 | + |
| 81 | + $tester = new CommandTester($this->command); |
| 82 | + $tester->execute(array('command' => $this->command->getName())); |
| 83 | + |
| 84 | + $display = $tester->getDisplay(); |
| 85 | + |
| 86 | + $this->assertContains(sprintf('Removed %d items from %s storage.', $expiredAccessTokens, 'Access token'), $display); |
| 87 | + $this->assertContains(sprintf('Removed %d items from %s storage.', $expiredRefreshTokens, 'Refresh token'), $display); |
| 88 | + $this->assertContains(sprintf('Removed %d items from %s storage.', $expiredAuthCodes, 'Auth code'), $display); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Skip classes for deleting expired tokens that do not implement AuthCodeManagerInterface or TokenManagerInterface. |
| 93 | + */ |
| 94 | + public function testItShouldNotRemoveExpiredTokensForOtherClasses() |
| 95 | + { |
| 96 | + $this->container->set('fos_oauth_server.access_token_manager', new \stdClass()); |
| 97 | + $this->container->set('fos_oauth_server.refresh_token_manager', new \stdClass()); |
| 98 | + $this->container->set('fos_oauth_server.auth_code_manager', new \stdClass()); |
| 99 | + |
| 100 | + $tester = new CommandTester($this->command); |
| 101 | + $tester->execute(array('command' => $this->command->getName())); |
| 102 | + |
| 103 | + $display = $tester->getDisplay(); |
| 104 | + |
| 105 | + $this->assertNotRegExp(sprintf('\'Removed (\d)+ items from %s storage.\'', 'Access token'), $display); |
| 106 | + $this->assertNotRegExp(sprintf('\'Removed (\d)+ items from %s storage.\'', 'Refresh token'), $display); |
| 107 | + $this->assertNotRegExp(sprintf('\'Removed (\d)+ items from %s storage.\'', 'Auth code'), $display); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Provides the classes that should be accepted by the CleanCommand. |
| 112 | + * |
| 113 | + * @return array[] |
| 114 | + */ |
| 115 | + public function classProvider() |
| 116 | + { |
| 117 | + return array( |
| 118 | + array('FOS\OAuthServerBundle\Model\TokenManagerInterface'), |
| 119 | + array('FOS\OAuthServerBundle\Model\AuthCodeManagerInterface'), |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
0 commit comments