-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAbstractProcessTest.php
More file actions
145 lines (128 loc) · 4.89 KB
/
Copy pathAbstractProcessTest.php
File metadata and controls
145 lines (128 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (C) 2017-2021 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Tests;
use CleverAge\ProcessBundle\Manager\ProcessManager;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry;
use CleverAge\ProcessBundle\Registry\TransformerRegistry;
use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use CleverAge\ProcessBundle\EventListener\DataQueueEventListener;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Provide all necessary setup to test a process
*/
abstract class AbstractProcessTest extends KernelTestCase
{
/** @var ProcessManager */
protected $processManager;
/** @var ProcessConfigurationRegistry */
protected $processConfigurationRegistry;
/** @var TransformerRegistry */
protected $transformerRegistry;
/**
* Initialize DI
*/
protected function setUp()
{
static::bootKernel();
$this->processManager = $this->getContainer()->get(ProcessManager::class);
$this->processConfigurationRegistry = $this->getContainer()->get(ProcessConfigurationRegistry::class);
$this->transformerRegistry = $this->getContainer()->get(TransformerRegistry::class);
}
/**
* Assert that an array of values match what's been registered in the standard queue
* It can also match task codes using the checkTask flag
*
* @param array $expected
* @param string $processName
* @param bool $checkTask
*/
protected function assertDataQueue(array $expected, string $processName, bool $checkTask = true)
{
$dataQueueListener = $this->getContainer()->get(DataQueueEventListener::class);
$actualQueue = $dataQueueListener->getQueue($processName);
self::assertCount(\count($expected), $actualQueue, 'Event count does not match');
/**
* @var int $key
* @var ProcessState $value
*/
foreach ($actualQueue as $key => $value) {
self::assertArrayHasKey($key, $expected);
if ($checkTask) {
if (array_key_exists('task', $expected[$key])) {
self::assertEquals(
$expected[$key]['task'],
$value->getPreviousState()->getTaskConfiguration()->getCode(),
"Task #{$key} does not match"
);
}
if (array_key_exists('value', $expected[$key])) {
self::assertEquals($expected[$key]['value'], $value->getInput(), "Value #{$key} does not match");
}
} else {
self::assertEquals($expected[$key], $value->getInput(), "Value #{$key} does not match");
}
}
}
/**
* Returns the booted symfony container
*
* Compatibility backport for symfony/phpunit-bridge that should work with v3 or v4
*
* @return ContainerInterface
*/
protected function getContainer(): ContainerInterface
{
if (isset(self::$container)) {
return self::$container;
}
$container = self::$kernel->getContainer();
$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
return $container;
}
/**
* Helper method to configure options and test a transformation
*
* @param string $transformerCode
* @param mixed $expected
* @param mixed $value
* @param array $options
*
* @throws ExceptionInterface
*/
protected function assertTransformation(string $transformerCode, $expected, $value, array $options = [])
{
$result = $this->transform($transformerCode, $value, $options);
self::assertEquals($expected, $result);
}
/**
* Transform some value using referenced transformer with given options
*
* @param string $transformerCode
* @param mixed $value
* @param array $options
*
* @return mixed
* @throws ExceptionInterface
*/
protected function transform(string $transformerCode, $value, array $options = [])
{
$transformer = $this->transformerRegistry->getTransformer($transformerCode);
if ($transformer instanceof ConfigurableTransformerInterface) {
$resolver = new OptionsResolver();
$transformer->configureOptions($resolver);
$options = $resolver->resolve($options);
}
return $transformer->transform($value, $options);
}
}