1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CronBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CronBundle\Command\CronScanCommand; |
6
|
|
|
use Alpixel\Bundle\CronBundle\Tests\Fixtures\CronTestCommand; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
8
|
|
|
use Symfony\Component\Console\Application; |
9
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
|
12
|
|
|
class CronScanCommandTest extends KernelTestCase |
13
|
|
|
{ |
14
|
|
|
private $container; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
self::bootKernel(); |
19
|
|
|
$this->container = static::$kernel->getContainer(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testExecute() |
23
|
|
|
{ |
24
|
|
|
$commandTester = $this->createCommandTester($this->container); |
25
|
|
|
|
26
|
|
|
$exitCode = $commandTester->execute([ |
27
|
|
|
'command' => 'cron:scan', |
28
|
|
|
], [ |
29
|
|
|
'decorated' => false, |
30
|
|
|
'interactive' => false, |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$this->assertEquals(0, $exitCode, 'Returns 0 in case of success'); |
34
|
|
|
$this->assertRegExp('/Finished scanning for cron jobs/', $commandTester->getDisplay()); |
35
|
|
|
|
36
|
|
|
$commandTester = $this->createCommandTester($this->container, [new CronTestCommand()]); |
37
|
|
|
|
38
|
|
|
$exitCode = $commandTester->execute([ |
39
|
|
|
'command' => 'cron:scan', |
40
|
|
|
], [ |
41
|
|
|
'decorated' => false, |
42
|
|
|
'interactive' => false, |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$this->assertEquals(0, $exitCode, 'Returns 0 in case of success'); |
46
|
|
|
$this->assertRegExp('/Finished scanning for cron jobs/', $commandTester->getDisplay()); |
47
|
|
|
|
48
|
|
|
$entityManager = $this->container->get('doctrine.orm.entity_manager'); |
49
|
|
|
$command = $entityManager->getRepository('CronBundle:CronJob')->findOneByCommand('my_cron_test'); |
50
|
|
|
|
51
|
|
|
$this->assertNotNull($command, 'Can\'t find newly created command in database'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
private function createCommandTester(ContainerInterface $container, $extraCommands = []) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$application = new Application(); |
57
|
|
|
$application->setAutoExit(false); |
58
|
|
|
|
59
|
|
|
$command = new CronScanCommand(); |
60
|
|
|
$command->setContainer($container); |
61
|
|
|
|
62
|
|
|
foreach ($extraCommands as $extraCommand) { |
63
|
|
|
$extraCommand->setContainer($container); |
64
|
|
|
$application->add($extraCommand); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$application->add($command); |
68
|
|
|
|
69
|
|
|
return new CommandTester($application->find('cron:scan')); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.