1 | <?php |
||
15 | abstract class AbstractServiceTest extends TestCase |
||
16 | { |
||
17 | /** |
||
18 | * Purely to attempt to make tests easier to read. |
||
19 | * |
||
20 | * As language parameter is ignored from providers and replced with values in tests, this is used to mark value of |
||
21 | * language argument instead of either askingproviders to use 0, or a valid language array which would then not be |
||
22 | * used. |
||
23 | */ |
||
24 | const LANG_ARG = 0; |
||
25 | |||
26 | /** |
||
27 | * @var \object|\PHPUnit_Framework_MockObject_MockObject |
||
28 | */ |
||
29 | protected $innerApiServiceMock; |
||
30 | |||
31 | /** |
||
32 | * @var object |
||
33 | */ |
||
34 | protected $service; |
||
35 | |||
36 | /** |
||
37 | * @var \eZ\Publish\Core\Repository\Helper\LanguageResolver|\PHPUnit_Framework_MockObject_MockObject |
||
38 | */ |
||
39 | protected $languageHelperMock; |
||
40 | |||
41 | abstract public function getAPIServiceClassName(): string; |
||
42 | |||
43 | abstract public function getSiteAccessAwareServiceClassName(): string; |
||
44 | |||
45 | public function setUp() |
||
46 | { |
||
47 | parent::setUp(); |
||
48 | $this->innerApiServiceMock = $this->getMockBuilder($this->getAPIServiceClassName())->getMock(); |
||
49 | $this->languageHelperMock = $this->getMockBuilder(LanguageResolver::class) |
||
50 | ->disableOriginalConstructor() |
||
51 | ->getMock(); |
||
52 | $serviceClassName = $this->getSiteAccessAwareServiceClassName(); |
||
53 | |||
54 | $this->service = new $serviceClassName( |
||
55 | $this->innerApiServiceMock, |
||
56 | $this->languageHelperMock // Not all services needs or expects this, however once they do it will be there |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | protected function tearDown() |
||
61 | { |
||
62 | unset($this->service); |
||
63 | unset($this->languageHelperMock); |
||
64 | unset($this->innerApiServiceMock); |
||
65 | parent::tearDown(); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return array See signature on {@link testForPassTrough} for arguments and their type. |
||
70 | */ |
||
71 | abstract public function providerForPassTroughMethods(): array; |
||
72 | |||
73 | /** |
||
74 | * Make sure these methods does nothing more then passing the arguments to inner service. |
||
75 | * |
||
76 | * Methods tested here are basically those without as languages argument. |
||
77 | * |
||
78 | * @dataProvider providerForPassTroughMethods |
||
79 | * |
||
80 | * @param string $method |
||
81 | * @param array $arguments |
||
82 | * @param bool $return |
||
83 | */ |
||
84 | final public function testForPassTrough(string $method, array $arguments, bool $return = true) |
||
85 | { |
||
86 | $this->innerApiServiceMock |
||
87 | ->expects($this->once()) |
||
88 | ->method($method) |
||
89 | ->with(...$arguments) |
||
90 | ->willReturn($return); |
||
91 | |||
92 | $actualReturn = $this->service->$method(...$arguments); |
||
93 | |||
94 | if ($return) { |
||
95 | $this->assertTrue($actualReturn); |
||
96 | } else { |
||
97 | $this->assertNull($actualReturn); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return array See signature on {@link testForLanguageLookup} for arguments and their type. |
||
103 | * NOTE: languages / prioritizedLanguage, can be set to 0, it will be replaced by tests methods. |
||
104 | */ |
||
105 | abstract public function providerForLanguagesLookupMethods(): array; |
||
106 | |||
107 | /** |
||
108 | * Method to be able to customize the logic for setting language argument. |
||
109 | * |
||
110 | * @param array $languages |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | protected function getLanguageArgument(array $languages) |
||
118 | |||
119 | /** |
||
120 | * Test that language aware methods does a language lookup when language is not set. |
||
121 | * |
||
122 | * @dataProvider providerForLanguagesLookupMethods |
||
123 | * |
||
124 | * @param string $method |
||
125 | * @param array $arguments |
||
126 | * @param bool $return |
||
127 | * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments. |
||
128 | */ |
||
129 | final public function testForLanguagesLookup(string $method, array $arguments, bool $return, int $languageArgumentIndex) |
||
155 | |||
156 | /** |
||
157 | * Make sure these methods does nothing more then passing the arguments to inner service. |
||
158 | * |
||
159 | * @dataProvider providerForLanguagesLookupMethods |
||
160 | * |
||
161 | * @param string $method |
||
162 | * @param array $arguments |
||
163 | * @param bool $return |
||
164 | * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments. |
||
165 | */ |
||
166 | final public function testForLanguagesPassTrough(string $method, array $arguments, bool $return, int $languageArgumentIndex) |
||
189 | } |
||
190 |