Skip to content

Commit 1b851d7

Browse files
RolfBabijnGuilhemN
authored andcommitted
Added test for the CleanCommand. (#405)
* Added test for the CleanCommand. Added "symfony/console" to dev dependencies and suggestions to be able to use and test it. Removed superfluous blank line. * Added minimum version for "symfony/dependency-injection" where auto loading was introduced for composer.json. * Refactored the CleanCommandTest based on feedback. * Changed container from a mock to the real object. * Changed the two separate interface tests to be provided to one new test. * Removed unnecessary use of regular expressions to test output.
1 parent 59229c2 commit 1b851d7

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

Tests/Command/CleanCommandTest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
"php": "^5.3.3|^7.0",
2020
"friendsofsymfony/oauth2-php": "~1.1",
2121
"symfony/framework-bundle": "~2.2|~3.0",
22-
"symfony/security-bundle": "~2.1|~3.0"
22+
"symfony/security-bundle": "~2.1|~3.0",
23+
"symfony/dependency-injection": "^2.0.5|~3.0"
2324
},
2425
"require-dev": {
2526
"symfony/class-loader": "~2.1|~3.0",
2627
"symfony/yaml": "~2.1|~3.0",
2728
"symfony/form": "~2.3|~3.0",
29+
"symfony/console": "~2.1|~3.0",
2830
"willdurand/propel-typehintable-behavior": "^1.0.4",
2931
"propel/propel1": "^1.6.5",
3032
"phing/phing": "~2.4",
@@ -37,7 +39,8 @@
3739
"doctrine/mongodb-odm-bundle": "*",
3840
"propel/propel-bundle": "If you want to use Propel with Symfony2, then you will have to install the PropelBundle",
3941
"willdurand/propel-typehintable-behavior": "The Typehintable behavior is useful to add type hints on generated methods, to be compliant with interfaces",
40-
"symfony/form" : "Needed to be able to use the AuthorizeFormType"
42+
"symfony/form" : "Needed to be able to use the AuthorizeFormType",
43+
"symfony/console": "Needed to be able to use commands"
4144
},
4245
"autoload": {
4346
"psr-4": { "FOS\\OAuthServerBundle\\": "" }
@@ -47,5 +50,4 @@
4750
"dev-master": "1.5-dev"
4851
}
4952
}
50-
5153
}

0 commit comments

Comments
 (0)