1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dolibarr\Client\Tests\Service; |
4
|
|
|
|
5
|
|
|
use Dolibarr\Client\HttpClient\HttpClientInterface; |
6
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
7
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
8
|
|
|
use JMS\Serializer\Serializer; |
9
|
|
|
use JMS\Serializer\SerializerBuilder; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
|
15
|
|
|
abstract class ServiceTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var HttpClientInterface | MockObject |
19
|
|
|
*/ |
20
|
|
|
private $mockClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Set up the client. |
24
|
|
|
*/ |
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->mockClient = $this->createMock(HttpClientInterface::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return HttpClientInterface|MockObject |
33
|
|
|
*/ |
34
|
|
|
protected function mockClient() |
35
|
|
|
{ |
36
|
|
|
return $this->mockClient; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* |
42
|
|
|
* @return ResponseInterface |
43
|
|
|
*/ |
44
|
|
|
protected function buildResponse($name = null) |
45
|
|
|
{ |
46
|
|
|
$responseBody = $this->createMock(StreamInterface::class); |
47
|
|
|
|
48
|
|
|
/** @var ResponseInterface|MockObject $response */ |
49
|
|
|
$response = $this->createMock(ResponseInterface::class); |
50
|
|
|
$response->expects($this->once())->method("getBody") |
51
|
|
|
->willReturn($responseBody); |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
$responseBody->expects($this->once())->method("getContents") |
55
|
|
|
->willReturn($name ? $this->getExpectedResponse($name) : null); |
56
|
|
|
|
57
|
|
|
return $response; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $name |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
private function getExpectedResponse($name) |
66
|
|
|
{ |
67
|
|
|
return file_get_contents(__DIR__ . "/Data/Response/" . $name . ".json"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getExpectedPayload($name) |
76
|
|
|
{ |
77
|
|
|
return file_get_contents(__DIR__ . "/Data/Request/" . $name . ".json"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Serializer |
82
|
|
|
*/ |
83
|
|
|
protected function serializer() |
84
|
|
|
{ |
85
|
|
|
return SerializerBuilder::create() |
86
|
|
|
->addDefaultHandlers() |
87
|
|
|
->build(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|