1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\Mapper; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Povs\ListerBundle\Exception\ListFieldException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Povilas Margaiatis <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractFieldTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @dataProvider idProvider |
15
|
|
|
* @param array $data field data |
16
|
|
|
* @param string $id expected field id |
17
|
|
|
*/ |
18
|
|
|
public function testId(array $data, string $id): void |
19
|
|
|
{ |
20
|
|
|
$field = $this->getField($data); |
21
|
|
|
$this->assertEquals($id, $field->getId()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @dataProvider pathsProvider |
26
|
|
|
* @param array $data |
27
|
|
|
* @param array $paths |
28
|
|
|
* @param string $exception |
29
|
|
|
*/ |
30
|
|
|
public function testPaths(array $data, array $paths, ?string $exception): void |
31
|
|
|
{ |
32
|
|
|
if ($exception) { |
33
|
|
|
$this->expectException($exception); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$field = $this->getField($data); |
37
|
|
|
|
38
|
|
|
if (!$exception) { |
39
|
|
|
$this->assertEquals($this->getPaths($field), $paths); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testInvalidOptionsThrowsException(): void |
44
|
|
|
{ |
45
|
|
|
$this->expectException(ListFieldException::class); |
46
|
|
|
$this->getField(['id', ['invalid_option' => true], null]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array [data[], expectedId] |
51
|
|
|
*/ |
52
|
|
|
public function idProvider(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
[['id', [], null], 'id'], |
56
|
|
|
[['test.dot.conversion', [], null], 'test_dot_conversion'] |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array [data[], expectedPaths[], exception] |
62
|
|
|
*/ |
63
|
|
|
public function pathsProvider(): array |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
[['id', [], null], ['id'], null], |
67
|
|
|
[['test.dot.conversion', [], null], ['test.dot.conversion'], null], |
68
|
|
|
[['test', ['property' => 'prop'], null], ['test.prop'], null], |
69
|
|
|
[['test', ['property' => ['prop1', 'prop2']], null], ['test.prop1', 'test.prop2'], null], |
70
|
|
|
[['test', ['property' => ['key1' => 'prop1', 'key2' => 'prop2']], null], ['key1' => 'test.prop1', 'key2' => 'test.prop2'], null], |
71
|
|
|
[['test', ['path' => 'customPath', 'property' => ['prop']], null], ['customPath.prop'], null], |
72
|
|
|
[['test', ['path' => ['key1' => 'val1', 'key2' => 'val2']], null], ['key1' => 'val1', 'key2' => 'val2'], null], |
73
|
|
|
[['test', ['path' => ['key1' => 'val1'], 'property' => ['prop']], null], ['val1.prop'], null], |
74
|
|
|
[['test', ['path' => 'customPath', 'property' => ['prop1', 'prop2']], null], ['customPath.prop1', 'customPath.prop2'], null], |
75
|
|
|
[['test', ['path' => '', 'property' => ['prop1', 'prop2']], null], ['prop1', 'prop2'], null], |
76
|
|
|
[['test', ['path' => ['customPath1', 'customPath2', 'customPath3'], 'property' => ['prop1', 'prop2']], null], [], ListFieldException::class] |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $data |
82
|
|
|
* |
83
|
|
|
* @return AbstractField |
84
|
|
|
*/ |
85
|
|
|
abstract protected function getField(array $data): AbstractField; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param AbstractField $field |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
abstract protected function getPaths(AbstractField $field): array; |
93
|
|
|
} |
94
|
|
|
|