|
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
|
|
|
|
|
15
|
|
|
class TestClassForAbstractClient extends AbstractClient |
|
16
|
|
|
{ |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractClientTest extends TestCase |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
const ACCESS_TOKEN = 'access-token'; |
|
22
|
|
|
const DEFAULT_ORGANIZATION_UUID = 'self'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @test |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setOrganizationUuid() |
|
28
|
|
|
{ |
|
29
|
|
|
$newOrganizationUuid = 123456; |
|
30
|
|
|
$abstractClient = new TestClassForAbstractClient(new GuzzleClient(), $this->getAccessToken()); |
|
31
|
|
|
|
|
32
|
|
|
self::assertAttributeEquals(self::DEFAULT_ORGANIZATION_UUID, 'organizationUuid', $abstractClient); |
|
33
|
|
|
|
|
34
|
|
|
$abstractClient->setOrganizationUuid($newOrganizationUuid); |
|
35
|
|
|
|
|
36
|
|
|
self::assertAttributeEquals($newOrganizationUuid, 'organizationUuid', $abstractClient); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
* @expectedException \LauLamanApps\IzettleApi\Client\Exceptions\AccessTokenExpiredException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function validateAccessToken() |
|
44
|
|
|
{ |
|
45
|
|
|
$invalidAccessToken = new AccessToken('', new DateTime('-1 day'), ''); |
|
46
|
|
|
|
|
47
|
|
|
new TestClassForAbstractClient(new GuzzleClient(), $invalidAccessToken); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function getGuzzleClient( |
|
51
|
|
|
string $method, |
|
52
|
|
|
string $url, |
|
53
|
|
|
array $options, |
|
54
|
|
|
?array $return = [] |
|
55
|
|
|
): GuzzleClient { |
|
56
|
|
|
$guzzleResponseMock = Mockery::mock(ResponseInterface::class); |
|
57
|
|
|
$guzzleResponseMock->shouldReceive('getBody')->andReturnSelf(); |
|
58
|
|
|
$guzzleResponseMock->shouldReceive('getContents')->andReturn(json_encode($return)); |
|
59
|
|
|
|
|
60
|
|
|
$guzzleClientMock = Mockery::mock(GuzzleClient::class); |
|
61
|
|
|
$guzzleClientMock->shouldReceive($method)->withArgs([$url, $options])->andReturn($guzzleResponseMock); |
|
62
|
|
|
|
|
63
|
|
|
return $guzzleClientMock; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getAccessToken() : AccessToken |
|
67
|
|
|
{ |
|
68
|
|
|
return new AccessToken(self::ACCESS_TOKEN, new DateTime('+ 1 day'), ''); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
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.