Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require-dev": {
"graham-campbell/analyzer": "^2.4",
"phpunit/phpunit": "^8.5|^9.0",
"php-http/guzzle6-adapter": "^2.0"
"php-http/guzzle6-adapter": "^2.0",
"php-http/mock-client": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
68 changes: 68 additions & 0 deletions tests/MockedClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/*
* This file is part of Bitbucket API Client.
*
* (c) Graham Campbell <graham@alt-three.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Bitbucket\Tests;

use Bitbucket\Client;
use Bitbucket\HttpClient\Builder;
use Http\Message\ResponseFactory;
use Http\Mock\Client as MockClient;
use Psr\Http\Message\ResponseInterface;

/**
* This is the mocked client class.
*
* @author Graham Campbell <graham@alt-three.com>
* @author Giacomo Fabbian <info@giacomofabbian.it>
*/
final class MockedClient
{
/**
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return \Bitbucket\Client
*/
public static function create(ResponseInterface $response)
{
$client = new MockClient(self::createResponseFactory($response));

return new Client(new Builder($client));
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return \Http\Message\ResponseFactory
*/
private static function createResponseFactory(ResponseInterface $response)
{
return new class($response) implements ResponseFactory {
private $response;

public function __construct(ResponseInterface $response)
{
$this->response = $response;
}

public function createResponse(
$statusCode = 200,
$reasonPhrase = null,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return $this->response;
}
};
}
}
129 changes: 129 additions & 0 deletions tests/ProjectsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

/*
* This file is part of Bitbucket API Client.
*
* (c) Graham Campbell <graham@alt-three.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Bitbucket\Tests;

use Bitbucket\Tests\Response\ProjectsCreateResponse;
use Bitbucket\Tests\Response\ProjectsListResponse;
use Bitbucket\Tests\Response\ProjectsRemoveResponse;
use Bitbucket\Tests\Response\ProjectsShowResponse;
use Bitbucket\Tests\Response\ProjectsUpdateResponse;
use PHPUnit\Framework\TestCase;

/**
* This is the projects test class.
*
* @author Graham Campbell <graham@alt-three.com>
* @author Giacomo Fabbian <info@giacomofabbian.it>
*/
final class ProjectsTest extends TestCase
{
public function testProjectList()
{
$client = MockedClient::create(
ProjectsListResponse::create()
);

$response = $client
->workspaces('my-workspace')
->projects()
->list();

$this->assertIsArray($response);
$this->assertCount(9, $response['values']);
}

public function testProjectShow()
{
$client = MockedClient::create(
ProjectsShowResponse::create()
);

$response = $client
->workspaces('my-workspace')
->projects()
->show('Atlassian1');

$this->assertIsArray($response);
$this->assertCount(11, $response);
}

public function testProjectCreate()
{
$client = MockedClient::create(
ProjectsCreateResponse::create()
);

$params = [
'name' => 'name',
'key' => 'key',
'description' => 'description',
'links' => (object) [
'avatar' => (object) [
'href' => '',
],
],
'is_private' => true,
];

$response = $client
->workspaces('my-workspace')
->projects()
->create($params);

$this->assertIsArray($response);
$this->assertCount(11, $response);
}

public function testProjectUpdate()
{
$client = MockedClient::create(
ProjectsUpdateResponse::create()
);

$params = [
'name' => 'name-updated',
'key' => 'Atlassian1',
'description' => 'description-updated',
'links' => (object) [
'avatar' => (object) [
'href' => '',
],
],
'is_private' => true,
];

$response = $client
->workspaces('my-workspace')
->projects()
->update('Atlassian1', $params);

$this->assertIsArray($response);
$this->assertCount(11, $response);
}

public function testProjectRemove()
{
$client = MockedClient::create(
ProjectsRemoveResponse::create()
);

$response = $client
->workspaces('my-workspace')
->projects()
->remove('Atlassian1');

$this->assertIsArray($response);
$this->assertCount(0, $response);
}
}
38 changes: 38 additions & 0 deletions tests/Resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of Bitbucket API Client.
*
* (c) Graham Campbell <graham@alt-three.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Bitbucket\Tests;

/**
* This is the resource class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
final class Resource
{
/**
* @param string $path
*
* @return string
*/
public static function get(string $path)
{
$content = @\file_get_contents(sprintf('%s/Resource/%s', __DIR__, $path));

if ($content === false) {
throw new \RuntimeException(sprintf('Unable to read resource [%s].', $path));
}

return $content;
}
}
58 changes: 58 additions & 0 deletions tests/Resource/projects-create-success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"description":"description",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/workspaces/john_doe/projects/TEST"
},
"html":{
"href":"https://bitbucket.org/john_doe/workspace/projects/TEST"
},
"repositories":[

],
"avatar":{
"href":""
}
},
"uuid":"{4762738f-3caa-44f7-8dd2-bd25237f7ade}",
"created_on":"2020-06-29T10:24:26.594408+00:00",
"workspace":{
"slug":"john_doe",
"type":"workspace",
"name":"john-doe",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/workspaces/john_doe"
},
"html":{
"href":"https://bitbucket.org/john_doe/"
},
"avatar":{
"href":"https://bitbucket.org/workspaces/john_doe/avatar/?ts=1571998380"
}
},
"uuid":"{eaf34f99-08d3-4888-ac79-b806e8927edd}"
},
"key":"key",
"owner":{
"username":"john_doe",
"display_name":"john-doe",
"type":"team",
"uuid":"{eaf34f99-08d3-4888-ac79-b806e8927edd}",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/teams/%7Beaf34f99-08d3-4888-ac79-b806e8927edd%7D"
},
"html":{
"href":"https://bitbucket.org/%7Beaf34f99-08d3-4888-ac79-b806e8927edd%7D/"
},
"avatar":{
"href":"https://bitbucket.org/account/john_doe/avatar/"
}
}
},
"updated_on":"2020-06-29T10:24:26.594431+00:00",
"type":"project",
"is_private":true,
"name":"name"
}
Loading