1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Global Trading Technologies Ltd workflow-extension-bundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* (c) fduch <[email protected]> |
9
|
|
|
* @date 28.07.16 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\Tests\Functional; |
13
|
|
|
|
14
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand; |
15
|
|
|
use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand; |
16
|
|
|
use Doctrine\ORM\EntityManager; |
17
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Tests\Functional\Configuration\ScheduleCase\Fixtures\ClientBundle\ClientBundle; |
18
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Tests\Functional\Configuration\ScheduleCase\Fixtures\ClientBundle\Entity\Client; |
19
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Tests\Functional\Configuration\ScheduleCase\Fixtures\Event; |
20
|
|
|
use JMS\JobQueueBundle\Command\RunCommand; |
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27
|
|
|
|
28
|
|
|
class ScheduleCaseTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* WebClient emulator |
32
|
|
|
* |
33
|
|
|
* @var \Symfony\Bundle\FrameworkBundle\Client |
34
|
|
|
*/ |
35
|
|
|
protected $client; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Application |
39
|
|
|
*/ |
40
|
|
|
protected $app; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function setUp() |
46
|
|
|
{ |
47
|
|
|
$this->initApplication(); |
48
|
|
|
|
49
|
|
|
$this->initDbSchema($this->client->getContainer()); |
|
|
|
|
50
|
|
|
|
51
|
|
|
// load fixtures |
52
|
|
|
$fixturesBundle = new ClientBundle(); |
53
|
|
|
$fixturesPath = $fixturesBundle->getPath() . '/DataFixtures/ORM'; |
54
|
|
|
$this->loadFixtures($this->client->getContainer(), $fixturesPath); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function initApplication() |
58
|
|
|
{ |
59
|
|
|
if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) { |
60
|
|
|
self::markTestSkipped('This test requires SQLite support in your environment'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
parent::setUp(); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->client = $this->createClient( |
66
|
|
|
array( |
67
|
|
|
"app_name" => "ScheduleCaseTest", |
68
|
|
|
"test_case" => "ScheduleCase", |
69
|
|
|
"root_config" => "config.yml", |
70
|
|
|
"config_dir" => __DIR__ . "/Configuration", |
71
|
|
|
"root_dir" => __DIR__ . "/Configuration/ScheduleCase", |
72
|
|
|
"environment" => "test", |
73
|
|
|
"debug" => false |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
$this->app = new Application($this->client->getKernel()); |
77
|
|
|
// add jms-job-id option to the application in order to be able to run scheduler |
78
|
|
|
$this->app->getDefinition()->addOption( |
79
|
|
|
new InputOption('--jms-job-id', null, InputOption::VALUE_REQUIRED, 'The ID of the Job.') |
80
|
|
|
); |
81
|
|
|
$this->app->setAutoExit(false); |
82
|
|
|
$this->app->setCatchExceptions(false); |
83
|
|
|
|
84
|
|
|
return $this->app; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ContainerInterface $container |
89
|
|
|
* @param string $em |
90
|
|
|
*/ |
91
|
|
|
protected function initDbSchema(ContainerInterface $container, $em = 'default') |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$schemaCreateCommand = new CreateSchemaDoctrineCommand(); |
94
|
|
|
$this->runConsoleCommand($schemaCreateCommand, ["--em" => $em]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param ContainerInterface $container |
99
|
|
|
* @param array|string|false $fixturesPath |
100
|
|
|
* @param string $em |
101
|
|
|
* @param bool|true $append |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function loadFixtures(ContainerInterface $container, $fixturesPath = false, $em = 'default', $append = true) |
105
|
|
|
{ |
106
|
|
|
$fixtureLoadCommand = new LoadDataFixturesDoctrineCommand(); |
107
|
|
|
$fixtureLoadCommand->setContainer($container); |
108
|
|
|
$params = array( |
109
|
|
|
"--em" => $em, |
110
|
|
|
"--append" => $append |
111
|
|
|
); |
112
|
|
|
if ($fixturesPath) { |
113
|
|
|
$params["--fixtures"] = $fixturesPath; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->runConsoleCommand($fixtureLoadCommand, $params); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @large |
121
|
|
|
* @group functional |
122
|
|
|
*/ |
123
|
|
|
public function testScheduleWorks() |
124
|
|
|
{ |
125
|
|
|
$container = $this->client->getContainer(); |
126
|
|
|
/** @var EventDispatcherInterface $eventDispatcher */ |
127
|
|
|
$eventDispatcher = $container->get('event_dispatcher'); |
128
|
|
|
|
129
|
|
|
/** @var EntityManager $clientEm */ |
130
|
|
|
$clientEm = $container->get("doctrine")->getManagerForClass(Client::class); |
131
|
|
|
$clientRepo = $clientEm->getRepository(Client::class); |
132
|
|
|
/** @var Client $subject */ |
133
|
|
|
$subject = $clientRepo->findOneBy(['name' => "Johnny"]); |
134
|
|
|
$event = new Event($subject); |
135
|
|
|
|
136
|
|
|
// simply check that client can be activated (also sleeping transition should be scheduled (+1s) inside) |
137
|
|
|
$eventDispatcher->dispatch('activating.event', $event); |
138
|
|
|
self::assertEquals('active', $subject->getStatus()); |
139
|
|
|
|
140
|
|
|
// sleep for a while (wait 2 sec to avoid "the same second" collision) |
141
|
|
|
// and run scheduler with 1-sec runtime to check that sleeping transition is executed |
142
|
|
|
sleep(2); |
143
|
|
|
$this->runScheduler(1); |
144
|
|
|
$clientEm->refresh($subject); |
145
|
|
|
self::assertEquals('sleeping', $subject->getStatus()); |
146
|
|
|
|
147
|
|
|
$eventDispatcher->dispatch('prolong.event', $event); |
148
|
|
|
|
149
|
|
|
// sleep for a while (in order to execute scheduler in the time when closing transition should be applied without prolong.event fired) |
150
|
|
|
// and run scheduler with 1-sec runtime to check that closed transition is not executed due to prolongation |
151
|
|
|
usleep(0.5*1e6); |
152
|
|
|
$this->runScheduler(1); |
153
|
|
|
$clientEm->refresh($subject); |
154
|
|
|
self::assertEquals('sleeping', $subject->getStatus()); |
155
|
|
|
|
156
|
|
|
// sleep for a while (some time we waste during waiting that scheduler finish his work so no need to wait so long) |
157
|
|
|
// and run scheduler with 1-sec runtime to check that closed transition is executed |
158
|
|
|
$this->runScheduler(1); |
159
|
|
|
$clientEm->refresh($subject); |
160
|
|
|
self::assertEquals('closed', $subject->getStatus()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function runScheduler($runtime = 1) |
164
|
|
|
{ |
165
|
|
|
$schedulerCommand = new RunCommand(); |
166
|
|
|
$this->runConsoleCommand( |
167
|
|
|
$schedulerCommand, |
168
|
|
|
[ |
169
|
|
|
'--max-runtime' => $runtime, |
170
|
|
|
// set worker name explicitly in order to avoid errors caused by 50 characters name restrictions on |
171
|
|
|
// testing envs like travis |
172
|
|
|
'--worker-name' => uniqid("worker_") |
173
|
|
|
] |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function runConsoleCommand(Command $command, array $params = []) |
178
|
|
|
{ |
179
|
|
|
$command->setApplication($this->app); |
180
|
|
|
// use CommandTester to simple command running |
181
|
|
|
$commandRunner = new CommandTester($command); |
182
|
|
|
return $commandRunner->execute($params); |
183
|
|
|
} |
184
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: