1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gerrie\Tests\API\DataService; |
12
|
|
|
|
13
|
|
|
class DataServiceTestBase extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
public function testGetterAndSetterName() |
17
|
|
|
{ |
18
|
|
|
$instance = $this->getServiceMock(); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$name = 'UnitTest Test name'; |
21
|
|
|
|
22
|
|
|
$this->assertNotEmpty($instance->getName()); |
23
|
|
|
$instance->setName($name); |
24
|
|
|
$this->assertEquals($name, $instance->getName()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetterAndSetterConnector() |
28
|
|
|
{ |
29
|
|
|
$instance = $this->getServiceMock(); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$dummyConnector = new \stdClass(); |
32
|
|
|
|
33
|
|
|
$instance->setConnector($dummyConnector); |
34
|
|
|
$this->assertEquals($dummyConnector, $instance->getConnector()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetterAndSetterConfig() |
38
|
|
|
{ |
39
|
|
|
$instance = $this->getServiceMock(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$dummyConfig = [ |
42
|
|
|
'key' => 'value', |
43
|
|
|
'hash' => [ |
44
|
|
|
'my' => 'map', |
45
|
|
|
'Gerrit' => 'Gerrie', |
46
|
|
|
'HTTP' => 'SSH' |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$instance->setConfig($dummyConfig); |
51
|
|
|
$this->assertEquals($dummyConfig, $instance->getConfig()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetHost() |
55
|
|
|
{ |
56
|
|
|
$dummyHost = 'example.com'; |
57
|
|
|
$instance = $this->getServiceMock(['host' => $dummyHost]); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->assertEquals($dummyHost, $instance->getHost()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testSetterAndGetterQueryLimitWithoutInitialisation() |
63
|
|
|
{ |
64
|
|
|
$instance = $this->getServiceMock(); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$queryLimit = 1234; |
67
|
|
|
$instance->setQueryLimit($queryLimit); |
68
|
|
|
|
69
|
|
|
$this->assertInternalType('int', $instance->getQueryLimit()); |
70
|
|
|
$this->assertEquals($queryLimit, $instance->getQueryLimit()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testTransformJsonResponseWithValidJson() |
74
|
|
|
{ |
75
|
|
|
$json = '{"count":5,"stringVal":"fooBar","smallArray":[5,3,1]}'; |
76
|
|
|
|
77
|
|
|
$jsonResult = [ |
78
|
|
|
'count' => 5, |
79
|
|
|
'stringVal' => "fooBar", |
80
|
|
|
'smallArray' => [ |
81
|
|
|
5, |
82
|
|
|
3, |
83
|
|
|
1 |
84
|
|
|
] |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$instance = $this->getServiceMock(); |
|
|
|
|
88
|
|
|
$response = $instance->transformJsonResponse($json); |
89
|
|
|
|
90
|
|
|
$this->assertInternalType('array', $response); |
91
|
|
|
$this->assertEquals($jsonResult, $response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testTransformJsonResponseWithInvalidJson() |
95
|
|
|
{ |
96
|
|
|
$json = '{"count":5 "stringVal":"fooBar","smallArray":[5,3,1]}'; |
97
|
|
|
|
98
|
|
|
$instance = $this->getServiceMock(); |
|
|
|
|
99
|
|
|
$response = $instance->transformJsonResponse($json); |
100
|
|
|
|
101
|
|
|
$this->assertNull($response); |
102
|
|
|
} |
103
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: