/
MatejTest.php
81 lines (67 loc) · 2.66 KB
/
MatejTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php declare(strict_types=1);
namespace Lmc\Matej;
use GuzzleHttp\Psr7\Request;
use Http\Discovery\Psr18Client;
use Lmc\Matej\Model\Command\ItemPropertySetup;
use Lmc\Matej\Model\CommandResponse;
/**
* @covers \Lmc\Matej\Matej
*/
class MatejTest extends UnitTestCase
{
/** @test */
public function shouldBeInstantiable(): void
{
$matej = new Matej('account-id', 'apiKey');
$this->assertInstanceOf(Matej::class, $matej);
}
/** @test */
public function shouldExecuteRequestViaBuilder(): void
{
$dummyHttpResponse = $this->createJsonResponseFromFile(
__DIR__ . '/Http/Fixtures/response-one-successful-command.json'
);
$mockClient = $this->createMock(Psr18Client::class);
$mockClient->expects($this->once())
->method('sendRequest')
->willReturn($dummyHttpResponse);
$matej = new Matej('account-id', 'apiKey');
$matej->setHttpClient($mockClient);
$response = $matej->request()
->setupItemProperties()
->addProperty(ItemPropertySetup::timestamp('valid_from'))
->send();
$this->assertSame(1, $response->getNumberOfCommands());
$this->assertSame(1, $response->getNumberOfSuccessfulCommands());
$this->assertSame(0, $response->getNumberOfSkippedCommands());
$this->assertSame(0, $response->getNumberOfFailedCommands());
$this->assertCount(1, $response->getCommandResponses());
$this->assertInstanceOf(CommandResponse::class, $response->getCommandResponses()[0]);
$this->assertSame(CommandResponse::STATUS_OK, $response->getCommandResponses()[0]->getStatus());
}
/** @test */
public function shouldOverwriteBaseUrl(): void
{
$dummyHttpResponse = $this->createJsonResponseFromFile(
__DIR__ . '/Http/Fixtures/response-one-successful-command.json'
);
$mockClient = $this->createMock(Psr18Client::class);
$mockClient->expects($this->once())
->method('sendRequest')
->with($this->callback(function (Request $request) {
$this->assertStringStartsWith(
'https://nobody.nowhere.com/account-id',
$request->getUri()->__toString()
);
return true;
}))
->willReturn($dummyHttpResponse);
$matej = new Matej('account-id', 'apiKey');
$matej->setHttpClient($mockClient);
$matej->setBaseUrl('https://nobody.nowhere.com/%s');
$matej->request()
->setupItemProperties()
->addProperty(ItemPropertySetup::timestamp('valid_from'))
->send();
}
}