1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CronBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CronBundle\Command\CronRunCommand; |
6
|
|
|
use Alpixel\Bundle\CronBundle\Command\CronScanCommand; |
7
|
|
|
use Alpixel\Bundle\CronBundle\Tests\Fixtures\CronTestCommand; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
9
|
|
|
use Symfony\Component\Console\Application; |
10
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
|
13
|
|
|
class CronRunCommandTest extends KernelTestCase |
14
|
|
|
{ |
15
|
|
|
private $container; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
self::bootKernel(); |
20
|
|
|
$this->container = static::$kernel->getContainer(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testScanExecute() |
24
|
|
|
{ |
25
|
|
|
$commandTester = $this->createScanCommandTester($this->container, [new CronTestCommand()]); |
26
|
|
|
|
27
|
|
|
$exitCode = $commandTester->execute([ |
28
|
|
|
'command' => 'cron:scan', |
29
|
|
|
], [ |
30
|
|
|
'decorated' => false, |
31
|
|
|
'interactive' => false, |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(0, $exitCode, 'Returns 0 in case of success'); |
35
|
|
|
$this->assertRegExp('/Finished scanning for cron jobs/', $commandTester->getDisplay()); |
36
|
|
|
|
37
|
|
|
$entityManager = $this->container->get('doctrine.orm.entity_manager'); |
38
|
|
|
$command = $entityManager->getRepository('CronBundle:CronJob')->findOneByCommand('my_cron_test'); |
39
|
|
|
|
40
|
|
|
$this->assertNotNull($command, 'Can\'t find newly created command in database'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testRunExecute() |
44
|
|
|
{ |
45
|
|
|
$commandTester = $this->createRunCommandTester($this->container, [new CronTestCommand()]); |
46
|
|
|
|
47
|
|
|
$exitCode = $commandTester->execute([ |
48
|
|
|
'command' => 'cron:run', |
49
|
|
|
], [ |
50
|
|
|
'decorated' => false, |
51
|
|
|
'interactive' => false, |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$this->assertEquals(0, $exitCode, 'Returns 0 in case of success'); |
55
|
|
|
$this->assertRegExp('/Running 1 jobs/', $commandTester->getDisplay()); |
56
|
|
|
$this->assertRegExp('/Cron run completed in/', $commandTester->getDisplay()); |
57
|
|
|
$this->assertEquals('ok', file_get_contents(__DIR__.'/../Fixtures/app/cache/cron_result.log')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
private function createScanCommandTester(ContainerInterface $container, $extraCommands = []) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$application = new Application(); |
63
|
|
|
$application->setAutoExit(false); |
64
|
|
|
|
65
|
|
|
$command = new CronScanCommand(); |
66
|
|
|
$command->setContainer($container); |
67
|
|
|
|
68
|
|
|
foreach ($extraCommands as $extraCommand) { |
69
|
|
|
$extraCommand->setContainer($container); |
70
|
|
|
$application->add($extraCommand); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$application->add($command); |
74
|
|
|
|
75
|
|
|
return new CommandTester($application->find('cron:scan')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
private function createRunCommandTester(ContainerInterface $container, $extraCommands = []) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$application = new Application(); |
81
|
|
|
$application->setAutoExit(false); |
82
|
|
|
|
83
|
|
|
$command = new CronRunCommand(); |
84
|
|
|
$command->setContainer($container); |
85
|
|
|
|
86
|
|
|
foreach ($extraCommands as $extraCommand) { |
87
|
|
|
$extraCommand->setContainer($container); |
88
|
|
|
$application->add($extraCommand); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$application->add($command); |
92
|
|
|
|
93
|
|
|
return new CommandTester($application->find('cron:run')); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.