1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
9
|
|
|
use LauLamanApps\IzettleApi\AbstractClient; |
10
|
|
|
use LauLamanApps\IzettleApi\Client\AccessToken; |
11
|
|
|
use Mockery; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Ramsey\Uuid\Uuid; |
15
|
|
|
|
16
|
|
|
class TestClassForAbstractClient extends AbstractClient |
17
|
|
|
{ |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
abstract class AbstractClientTest extends TestCase |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
const ACCESS_TOKEN = 'access-token'; |
23
|
|
|
const DEFAULT_ORGANIZATION_UUID = 'self'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
*/ |
28
|
|
|
public function setOrganizationUuid() |
29
|
|
|
{ |
30
|
|
|
$newOrganizationUuid = Uuid::uuid1(); |
31
|
|
|
$abstractClient = new TestClassForAbstractClient(new GuzzleClient(), $this->getAccessToken()); |
32
|
|
|
|
33
|
|
|
self::assertAttributeEquals(self::DEFAULT_ORGANIZATION_UUID, 'organizationUuid', $abstractClient); |
34
|
|
|
|
35
|
|
|
$abstractClient->setOrganizationUuid($newOrganizationUuid); |
36
|
|
|
|
37
|
|
|
self::assertAttributeEquals((string) $newOrganizationUuid, 'organizationUuid', $abstractClient); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
* @expectedException \LauLamanApps\IzettleApi\Client\Exceptions\AccessTokenExpiredException |
43
|
|
|
*/ |
44
|
|
|
public function validateAccessToken() |
45
|
|
|
{ |
46
|
|
|
$invalidAccessToken = new AccessToken('', new DateTime('-1 day'), ''); |
47
|
|
|
|
48
|
|
|
new TestClassForAbstractClient(new GuzzleClient(), $invalidAccessToken); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getGuzzleClient( |
52
|
|
|
string $method, |
53
|
|
|
string $url, |
54
|
|
|
array $options, |
55
|
|
|
?array $return = [] |
56
|
|
|
): GuzzleClient { |
57
|
|
|
$guzzleResponseMock = Mockery::mock(ResponseInterface::class); |
58
|
|
|
$guzzleResponseMock->shouldReceive('getBody')->andReturnSelf(); |
59
|
|
|
$guzzleResponseMock->shouldReceive('getContents')->andReturn(json_encode($return)); |
60
|
|
|
|
61
|
|
|
$guzzleClientMock = Mockery::mock(GuzzleClient::class); |
62
|
|
|
$guzzleClientMock->shouldReceive($method)->withArgs([$url, $options])->andReturn($guzzleResponseMock); |
63
|
|
|
|
64
|
|
|
return $guzzleClientMock; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getAccessToken() : AccessToken |
68
|
|
|
{ |
69
|
|
|
return new AccessToken(self::ACCESS_TOKEN, new DateTime('+ 1 day'), ''); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.