Completed
Push — siteaccessaware-layer-only ( 14ffb6...972017 )
by André
30:54
created

AbstractServiceTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 166
c 0
b 0
f 0
rs 10
wmc 8
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
getAPIServiceClassName() 0 1 ?
getSiteAccessAwareServiceClassName() 0 1 ?
A setUp() 0 14 1
A tearDown() 0 7 1
providerForPassTroughMethods() 0 1 ?
A testForPassTrough() 0 14 2
providerForLanguagesLookupMethods() 0 1 ?
B testForLanguagesLookup() 0 28 2
B testForLanguagesPassTrough() 0 24 2
1
<?php
2
3
namespace eZ\Publish\Core\Repository\SiteAccessAware\Tests;
4
5
use eZ\Publish\Core\Repository\Helper\LanguageResolver;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Abstract tests for SiteAccessAware Services.
10
 *
11
 * Implies convention for methods on these services to either:
12
 * - Do nothing, pass-through call and optionally (default:true) return value
13
 * - lookup languages [IF not defined by callee] on one of the arguments given and pass it to next one.
14
 *
15
 */
16
abstract class AbstractServiceTest extends TestCase
17
{
18
    /**
19
     * Purely to attempt to make tests easier to read.
20
     *
21
     * As language parameter is ignored from providers and replced with values in tests, this is used to mark value of
22
     * language argument instead of either askingproviders to use 0, or a valid language array which would then not be
23
     * used.
24
     */
25
    const LANG_ARG = 0;
26
27
    /**
28
     * @var \object|\PHPUnit_Framework_MockObject_MockObject
29
     */
30
    protected $innerApiServiceMock;
31
32
    /**
33
     * @var object
34
     */
35
    protected $service;
36
37
    /**
38
     * @var \eZ\Publish\Core\Repository\Helper\LanguageResolver|\PHPUnit_Framework_MockObject_MockObject
39
     */
40
    protected $languageHelperMock;
41
42
43
    abstract public function getAPIServiceClassName(): string;
44
    abstract public function getSiteAccessAwareServiceClassName(): string;
45
46
47
    public function setUp()
48
    {
49
        parent::setUp();
50
        $this->innerApiServiceMock = $this->getMockBuilder($this->getAPIServiceClassName())->getMock();
51
        $this->languageHelperMock = $this->getMockBuilder(LanguageResolver::class)
52
            ->disableOriginalConstructor()
53
            ->getMock();
54
        $serviceClassName = $this->getSiteAccessAwareServiceClassName();
55
56
        $this->service = new $serviceClassName(
57
            $this->innerApiServiceMock,
58
            $this->languageHelperMock // Not all services needs or expects this, however once they do it will be there
59
        );
60
    }
61
62
    protected function tearDown()
63
    {
64
        unset($this->service);
65
        unset($this->languageHelperMock);
66
        unset($this->innerApiServiceMock);
67
        parent::tearDown();
68
    }
69
70
    /**
71
     * @return array See signature on {@link testForPassTrough} for arguments and their type.
72
     */
73
    abstract public function providerForPassTroughMethods(): array;
74
75
    /**
76
     * Make sure these methods does nothing more then passing the arguments to inner service.
77
     *
78
     * Methods tested here are basically those without as languages argument.
79
     *
80
     * @dataProvider providerForPassTroughMethods
81
     *
82
     * @param string $method
83
     * @param array $arguments
84
     * @param boolean $return
85
     */
86
    final public function testForPassTrough(string $method, array $arguments, bool $return)
87
    {
88
        $this->innerApiServiceMock
89
            ->expects($this->once())
90
            ->method($method)
91
            ->with(...$arguments)
92
            ->willReturn($return);
93
94
        $actualReturn = $this->service->$method(...$arguments);
95
96
        if ($return) {
97
            $this->assertTrue($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
     * Test that language aware methods does a language lookup when language is not set
109
     *
110
     * @dataProvider providerForLanguagesLookupMethods
111
     *
112
     * @param string $method
113
     * @param array $arguments
114
     * @param boolean $return
115
     * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments.
116
     */
117
    final public function testForLanguagesLookup(string $method, array $arguments, bool $return, int $languageArgumentIndex)
118
    {
119
        $languages = ['eng-GB', 'eng-US'];
120
        $arguments[$languageArgumentIndex] = [];
121
122
        $expectedArguments = $arguments;
123
        $expectedArguments[$languageArgumentIndex] = $languages;
124
125
126
        $this->languageHelperMock
127
            ->expects($this->once())
128
            ->method('getLanguages')
129
            ->with([])
130
            ->willReturn($languages);
131
132
        $this->innerApiServiceMock
133
            ->expects($this->once())
134
            ->method($method)
135
            ->with(...$expectedArguments)
136
            ->willReturn($return);
137
138
139
        $actualReturn = $this->service->$method(...$arguments);
140
141
        if ($return) {
142
            $this->assertTrue($actualReturn);
143
        }
144
    }
145
146
147
    /**
148
     * Make sure these methods does nothing more then passing the arguments to inner service.
149
     *
150
     * @dataProvider providerForLanguagesLookupMethods
151
     *
152
     * @param string $method
153
     * @param array $arguments
154
     * @param boolean $return
155
     * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments.
156
     */
157
    final public function testForLanguagesPassTrough(string $method, array $arguments, bool $return, int $languageArgumentIndex)
158
    {
159
        $languages = ['eng-GB', 'eng-US'];
160
        $arguments[$languageArgumentIndex] = $languages;
161
162
        $this->languageHelperMock
163
            ->expects($this->once())
164
            ->method('getLanguages')
165
            ->with($languages)
166
            ->willReturn($languages);
167
168
        $this->innerApiServiceMock
169
            ->expects($this->once())
170
            ->method($method)
171
            ->with(...$arguments)
172
            ->willReturn($return);
173
174
175
        $actualReturn = $this->service->$method(...$arguments);
176
177
        if ($return) {
178
            $this->assertTrue($actualReturn);
179
        }
180
    }
181
}
182