Skip to content

Commit 46bf301

Browse files
Merge pull request #50 from BitbucketAPI/improved-tests
Added additional tests
2 parents b4b9f00 + 52c78ef commit 46bf301

17 files changed

+1229
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"require-dev": {
2424
"graham-campbell/analyzer": "^2.4",
2525
"phpunit/phpunit": "^8.5|^9.0",
26-
"php-http/guzzle6-adapter": "^2.0"
26+
"php-http/guzzle6-adapter": "^2.0",
27+
"php-http/mock-client": "^1.3"
2728
},
2829
"autoload": {
2930
"psr-4": {

tests/MockedClient.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Bitbucket API Client.
7+
*
8+
* (c) Graham Campbell <graham@alt-three.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Bitbucket\Tests;
15+
16+
use Bitbucket\Client;
17+
use Bitbucket\HttpClient\Builder;
18+
use Http\Message\ResponseFactory;
19+
use Http\Mock\Client as MockClient;
20+
use Psr\Http\Message\ResponseInterface;
21+
22+
/**
23+
* This is the mocked client class.
24+
*
25+
* @author Graham Campbell <graham@alt-three.com>
26+
* @author Giacomo Fabbian <info@giacomofabbian.it>
27+
*/
28+
final class MockedClient
29+
{
30+
/**
31+
* @param \Psr\Http\Message\ResponseInterface $response
32+
*
33+
* @return \Bitbucket\Client
34+
*/
35+
public static function create(ResponseInterface $response)
36+
{
37+
$client = new MockClient(self::createResponseFactory($response));
38+
39+
return new Client(new Builder($client));
40+
}
41+
42+
/**
43+
* @param \Psr\Http\Message\ResponseInterface $response
44+
*
45+
* @return \Http\Message\ResponseFactory
46+
*/
47+
private static function createResponseFactory(ResponseInterface $response)
48+
{
49+
return new class($response) implements ResponseFactory {
50+
private $response;
51+
52+
public function __construct(ResponseInterface $response)
53+
{
54+
$this->response = $response;
55+
}
56+
57+
public function createResponse(
58+
$statusCode = 200,
59+
$reasonPhrase = null,
60+
array $headers = [],
61+
$body = null,
62+
$protocolVersion = '1.1'
63+
) {
64+
return $this->response;
65+
}
66+
};
67+
}
68+
}

tests/ProjectsTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Bitbucket API Client.
7+
*
8+
* (c) Graham Campbell <graham@alt-three.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Bitbucket\Tests;
15+
16+
use Bitbucket\Tests\Response\ProjectsCreateResponse;
17+
use Bitbucket\Tests\Response\ProjectsListResponse;
18+
use Bitbucket\Tests\Response\ProjectsRemoveResponse;
19+
use Bitbucket\Tests\Response\ProjectsShowResponse;
20+
use Bitbucket\Tests\Response\ProjectsUpdateResponse;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* This is the projects test class.
25+
*
26+
* @author Graham Campbell <graham@alt-three.com>
27+
* @author Giacomo Fabbian <info@giacomofabbian.it>
28+
*/
29+
final class ProjectsTest extends TestCase
30+
{
31+
public function testProjectList()
32+
{
33+
$client = MockedClient::create(
34+
ProjectsListResponse::create()
35+
);
36+
37+
$response = $client
38+
->workspaces('my-workspace')
39+
->projects()
40+
->list();
41+
42+
$this->assertIsArray($response);
43+
$this->assertCount(9, $response['values']);
44+
}
45+
46+
public function testProjectShow()
47+
{
48+
$client = MockedClient::create(
49+
ProjectsShowResponse::create()
50+
);
51+
52+
$response = $client
53+
->workspaces('my-workspace')
54+
->projects()
55+
->show('Atlassian1');
56+
57+
$this->assertIsArray($response);
58+
$this->assertCount(11, $response);
59+
}
60+
61+
public function testProjectCreate()
62+
{
63+
$client = MockedClient::create(
64+
ProjectsCreateResponse::create()
65+
);
66+
67+
$params = [
68+
'name' => 'name',
69+
'key' => 'key',
70+
'description' => 'description',
71+
'links' => (object) [
72+
'avatar' => (object) [
73+
'href' => '',
74+
],
75+
],
76+
'is_private' => true,
77+
];
78+
79+
$response = $client
80+
->workspaces('my-workspace')
81+
->projects()
82+
->create($params);
83+
84+
$this->assertIsArray($response);
85+
$this->assertCount(11, $response);
86+
}
87+
88+
public function testProjectUpdate()
89+
{
90+
$client = MockedClient::create(
91+
ProjectsUpdateResponse::create()
92+
);
93+
94+
$params = [
95+
'name' => 'name-updated',
96+
'key' => 'Atlassian1',
97+
'description' => 'description-updated',
98+
'links' => (object) [
99+
'avatar' => (object) [
100+
'href' => '',
101+
],
102+
],
103+
'is_private' => true,
104+
];
105+
106+
$response = $client
107+
->workspaces('my-workspace')
108+
->projects()
109+
->update('Atlassian1', $params);
110+
111+
$this->assertIsArray($response);
112+
$this->assertCount(11, $response);
113+
}
114+
115+
public function testProjectRemove()
116+
{
117+
$client = MockedClient::create(
118+
ProjectsRemoveResponse::create()
119+
);
120+
121+
$response = $client
122+
->workspaces('my-workspace')
123+
->projects()
124+
->remove('Atlassian1');
125+
126+
$this->assertIsArray($response);
127+
$this->assertCount(0, $response);
128+
}
129+
}

tests/Resource.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Bitbucket API Client.
7+
*
8+
* (c) Graham Campbell <graham@alt-three.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Bitbucket\Tests;
15+
16+
/**
17+
* This is the resource class.
18+
*
19+
* @author Graham Campbell <graham@alt-three.com>
20+
*/
21+
final class Resource
22+
{
23+
/**
24+
* @param string $path
25+
*
26+
* @return string
27+
*/
28+
public static function get(string $path)
29+
{
30+
$content = @\file_get_contents(sprintf('%s/Resource/%s', __DIR__, $path));
31+
32+
if ($content === false) {
33+
throw new \RuntimeException(sprintf('Unable to read resource [%s].', $path));
34+
}
35+
36+
return $content;
37+
}
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"description":"description",
3+
"links":{
4+
"self":{
5+
"href":"https://api.bitbucket.org/2.0/workspaces/john_doe/projects/TEST"
6+
},
7+
"html":{
8+
"href":"https://bitbucket.org/john_doe/workspace/projects/TEST"
9+
},
10+
"repositories":[
11+
12+
],
13+
"avatar":{
14+
"href":""
15+
}
16+
},
17+
"uuid":"{4762738f-3caa-44f7-8dd2-bd25237f7ade}",
18+
"created_on":"2020-06-29T10:24:26.594408+00:00",
19+
"workspace":{
20+
"slug":"john_doe",
21+
"type":"workspace",
22+
"name":"john-doe",
23+
"links":{
24+
"self":{
25+
"href":"https://api.bitbucket.org/2.0/workspaces/john_doe"
26+
},
27+
"html":{
28+
"href":"https://bitbucket.org/john_doe/"
29+
},
30+
"avatar":{
31+
"href":"https://bitbucket.org/workspaces/john_doe/avatar/?ts=1571998380"
32+
}
33+
},
34+
"uuid":"{eaf34f99-08d3-4888-ac79-b806e8927edd}"
35+
},
36+
"key":"key",
37+
"owner":{
38+
"username":"john_doe",
39+
"display_name":"john-doe",
40+
"type":"team",
41+
"uuid":"{eaf34f99-08d3-4888-ac79-b806e8927edd}",
42+
"links":{
43+
"self":{
44+
"href":"https://api.bitbucket.org/2.0/teams/%7Beaf34f99-08d3-4888-ac79-b806e8927edd%7D"
45+
},
46+
"html":{
47+
"href":"https://bitbucket.org/%7Beaf34f99-08d3-4888-ac79-b806e8927edd%7D/"
48+
},
49+
"avatar":{
50+
"href":"https://bitbucket.org/account/john_doe/avatar/"
51+
}
52+
}
53+
},
54+
"updated_on":"2020-06-29T10:24:26.594431+00:00",
55+
"type":"project",
56+
"is_private":true,
57+
"name":"name"
58+
}

0 commit comments

Comments
 (0)