|
4 | 4 |
|
5 | 5 |
|
6 | 6 | use chobie\Jira\Api; |
7 | | -use chobie\Jira\Api\Authentication\Anonymous; |
| 7 | +use chobie\Jira\Api\Authentication\AuthenticationInterface; |
| 8 | +use Prophecy\Prophecy\ObjectProphecy; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * Class ApiTest |
|
14 | 15 | class ApiTest extends \PHPUnit_Framework_TestCase |
15 | 16 | { |
16 | 17 |
|
| 18 | + const ENDPOINT = 'http://jira.company.com'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Api. |
| 22 | + * |
| 23 | + * @var Api |
| 24 | + */ |
| 25 | + protected $api; |
| 26 | + |
17 | 27 | /** |
18 | | - * Tests that any trailing slash in the endpoint url is removed before being stored in the object state |
| 28 | + * Credential. |
| 29 | + * |
| 30 | + * @var AuthenticationInterface |
19 | 31 | */ |
20 | | - public function testSetEndpointTrailingSlash() |
| 32 | + protected $credential; |
| 33 | + |
| 34 | + /** |
| 35 | + * Client. |
| 36 | + * |
| 37 | + * @var ObjectProphecy |
| 38 | + */ |
| 39 | + protected $client; |
| 40 | + |
| 41 | + protected function setUp() |
21 | 42 | { |
22 | | - $api = new Api('https://test.test/', new Anonymous(), null); |
23 | | - $this->assertEquals('https://test.test', $api->getEndpoint()); |
| 43 | + parent::setUp(); |
24 | 44 |
|
25 | | - // Make sure nothing is removed if there is no trailing slash |
26 | | - $url = 'https://urlwithouttrailing.slash'; |
27 | | - $api->setEndPoint($url); |
28 | | - $this->assertEquals($url, $api->getEndpoint()); |
| 45 | + $this->credential = $this->prophesize('chobie\Jira\Api\Authentication\AuthenticationInterface')->reveal(); |
| 46 | + $this->client = $this->prophesize('chobie\Jira\Api\Client\ClientInterface'); |
| 47 | + |
| 48 | + $this->api = new Api(self::ENDPOINT, $this->credential, $this->client->reveal()); |
29 | 49 | } |
30 | 50 |
|
31 | 51 | /** |
32 | | - * Tests that the updateVersion call constructs the correct api call |
| 52 | + * @dataProvider setEndpointDataProvider |
33 | 53 | */ |
| 54 | + public function testSetEndpoint($given_endpoint, $used_endpoint) |
| 55 | + { |
| 56 | + $api = new Api($given_endpoint, $this->credential, $this->client->reveal()); |
| 57 | + $this->assertEquals($used_endpoint, $api->getEndpoint()); |
| 58 | + } |
| 59 | + |
| 60 | + public function setEndpointDataProvider() |
| 61 | + { |
| 62 | + return array( |
| 63 | + 'trailing slash removed' => array('https://test.test/', 'https://test.test'), |
| 64 | + 'nothing removed' => array('https://test.test', 'https://test.test'), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
34 | 68 | public function testUpdateVersion() |
35 | 69 | { |
36 | 70 | $params = array( |
37 | | - 'released' => true, |
38 | | - 'releaseDate' => '2010-07-06', |
| 71 | + 'overdue' => true, |
| 72 | + 'description' => 'new description', |
39 | 73 | ); |
40 | 74 |
|
41 | | - // Stub the api method and keep the rest intact |
42 | | - /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ |
43 | | - $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock(); |
44 | | - $api->expects($this->once())->method('api')->with( |
45 | | - $this->equalTo(Api::REQUEST_PUT), |
46 | | - $this->equalTo('/rest/api/2/version/111000'), |
47 | | - $this->equalTo($params) |
| 75 | + $this->expectClientCall( |
| 76 | + Api::REQUEST_PUT, |
| 77 | + '/rest/api/2/version/111000', |
| 78 | + $params, |
| 79 | + '' |
48 | 80 | ); |
49 | 81 |
|
50 | | - $api->updateVersion(111000, $params); |
| 82 | + $this->assertFalse($this->api->updateVersion(111000, $params)); |
51 | 83 | } |
52 | 84 |
|
53 | | - /** |
54 | | - * Tests that the releaseVersion call constructs the correct api call |
55 | | - */ |
56 | | - public function testReleaseVersion() |
| 85 | + public function testReleaseVersionAutomaticReleaseDate() |
57 | 86 | { |
58 | 87 | $params = array( |
59 | 88 | 'released' => true, |
60 | 89 | 'releaseDate' => date('Y-m-d'), |
61 | 90 | ); |
62 | 91 |
|
63 | | - // Stub the api method and keep the rest intact |
64 | | - /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ |
65 | | - $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock(); |
66 | | - $api->expects($this->once())->method('api')->with( |
67 | | - $this->equalTo(Api::REQUEST_PUT), |
68 | | - $this->equalTo('/rest/api/2/version/111000'), |
69 | | - $this->equalTo($params) |
| 92 | + $this->expectClientCall( |
| 93 | + Api::REQUEST_PUT, |
| 94 | + '/rest/api/2/version/111000', |
| 95 | + $params, |
| 96 | + '' |
70 | 97 | ); |
71 | 98 |
|
72 | | - $api->releaseVersion(111000); |
| 99 | + $this->assertFalse($this->api->releaseVersion(111000)); |
73 | 100 | } |
74 | 101 |
|
75 | | - /** |
76 | | - * Tests that the releaseVersion call constructs the correct api call with overriden release data and params |
77 | | - */ |
78 | | - public function testReleaseVersionAdvanced() |
| 102 | + public function testReleaseVersionParameterMerging() |
79 | 103 | { |
80 | | - $releaseDate = '2010-07-06'; |
| 104 | + $release_date = '2010-07-06'; |
81 | 105 |
|
82 | | - $params = array( |
| 106 | + $expected_params = array( |
83 | 107 | 'released' => true, |
84 | | - 'releaseDate' => $releaseDate, |
| 108 | + 'releaseDate' => $release_date, |
85 | 109 | 'test' => 'extra', |
86 | 110 | ); |
87 | 111 |
|
88 | | - // Stub the api method and keep the rest intact |
89 | | - /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ |
90 | | - $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock(); |
91 | | - $api->expects($this->once())->method('api')->with( |
92 | | - $this->equalTo(Api::REQUEST_PUT), |
93 | | - $this->equalTo('/rest/api/2/version/111000'), |
94 | | - $this->equalTo($params) |
| 112 | + $this->expectClientCall( |
| 113 | + Api::REQUEST_PUT, |
| 114 | + '/rest/api/2/version/111000', |
| 115 | + $expected_params, |
| 116 | + '' |
95 | 117 | ); |
96 | 118 |
|
97 | | - $api->releaseVersion(111000, $releaseDate, array('test' => 'extra')); |
| 119 | + $this->assertFalse($this->api->releaseVersion(111000, $release_date, array('test' => 'extra'))); |
98 | 120 | } |
99 | 121 |
|
100 | | - /** |
101 | | - * Tests FindVersionByName |
102 | | - */ |
103 | 122 | public function testFindVersionByName() |
104 | 123 | { |
105 | | - $name = '3.36.0'; |
106 | | - $versionId = '14206'; |
107 | | - $projectKey = 'POR'; |
| 124 | + $project_key = 'POR'; |
| 125 | + $version_id = '14206'; |
| 126 | + $version_name = '3.36.0'; |
108 | 127 |
|
109 | 128 | $versions = array( |
110 | 129 | array('id' => '14205', 'name' => '3.62.0'), |
111 | | - array('id' => $versionId, 'name' => $name), |
| 130 | + array('id' => $version_id, 'name' => $version_name), |
112 | 131 | array('id' => '14207', 'name' => '3.66.0'), |
113 | 132 | ); |
114 | 133 |
|
115 | | - // Stub the getVersions method and keep the rest intact |
116 | | - /** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */ |
117 | | - $api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('getVersions'))->disableOriginalConstructor()->getMock(); |
118 | | - $api->expects($this->exactly(2))->method('getVersions')->with( |
119 | | - $this->equalTo($projectKey) |
120 | | - )->willReturn($versions); |
| 134 | + $this->expectClientCall( |
| 135 | + Api::REQUEST_GET, |
| 136 | + '/rest/api/2/project/' . $project_key . '/versions', |
| 137 | + array(), |
| 138 | + json_encode($versions) |
| 139 | + ); |
121 | 140 |
|
122 | | - // He should find this one |
123 | | - $this->assertEquals(array('id' => $versionId, 'name' => $name), $api->findVersionByName($projectKey, $name)); |
| 141 | + $this->assertEquals( |
| 142 | + array('id' => $version_id, 'name' => $version_name), |
| 143 | + $this->api->findVersionByName($project_key, $version_name), |
| 144 | + 'Version found' |
| 145 | + ); |
124 | 146 |
|
125 | | - // And there should be no result for this one |
126 | | - $this->assertNull($api->findVersionByName($projectKey, 'i_do_not_exist')); |
| 147 | + $this->assertNull( |
| 148 | + $this->api->findVersionByName($project_key, 'i_do_not_exist') |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Expects a particular client call. |
| 154 | + * |
| 155 | + * @param string $method Request method. |
| 156 | + * @param string $url URL. |
| 157 | + * @param array|string $data Request data. |
| 158 | + * @param string $return_value Return value. |
| 159 | + * @param boolean $is_file This is a file upload request. |
| 160 | + * @param boolean $debug Debug this request. |
| 161 | + * |
| 162 | + * @return void |
| 163 | + */ |
| 164 | + protected function expectClientCall( |
| 165 | + $method, |
| 166 | + $url, |
| 167 | + $data = array(), |
| 168 | + $return_value, |
| 169 | + $is_file = false, |
| 170 | + $debug = false |
| 171 | + ) { |
| 172 | + $this->client |
| 173 | + ->sendRequest($method, $url, $data, self::ENDPOINT, $this->credential, $is_file, $debug) |
| 174 | + ->willReturn($return_value) |
| 175 | + ->shouldBeCalled(); |
127 | 176 | } |
128 | 177 |
|
129 | 178 | } |
0 commit comments