1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ozean12\WebTranslateItBundle\Tests\Service; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use JMS\Serializer\SerializerInterface; |
8
|
|
|
use Ozean12\WebTranslateItBundle\DTO\ProjectDTO; |
9
|
|
|
use Ozean12\WebTranslateItBundle\DTO\PullProjectResponseDTO; |
10
|
|
|
use Ozean12\WebTranslateItBundle\Service\WebTranslateItRepository; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class WebTranslateItRepositoryTest |
14
|
|
|
*/ |
15
|
|
|
class WebTranslateItRepositoryTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
const READ_KEY = 'test_key'; |
18
|
|
|
const BASE_URL = 'test_base_url/'; |
19
|
|
|
|
20
|
|
|
const FILE_URL = 'base_url/{token}/files/...?file_path={name}'; |
21
|
|
|
const FILE_NAME = 'test_messages.en.yml'; |
22
|
|
|
|
23
|
|
|
const PROJECT_URL_NORMALIZED = 'test_base_url/projects/test_key.json'; |
24
|
|
|
const FILE_URL_NORMALIZED = 'test_base_url/projects/test_key/files/...?file_path=test_messages.en.yml'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var WebTranslateItRepository |
28
|
|
|
*/ |
29
|
|
|
private $repository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject |
33
|
|
|
*/ |
34
|
|
|
private $serializer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Client|\PHPUnit_Framework_MockObject_MockObject |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* SetUp |
43
|
|
|
*/ |
44
|
|
|
public function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->serializer = $this->getMockBuilder(SerializerInterface::class)->getMock(); |
47
|
|
|
$this->client = $this->getMockBuilder(Client::class)->setMethods(['get'])->getMock(); |
48
|
|
|
|
49
|
|
|
$this->repository = new WebTranslateItRepository( |
50
|
|
|
self::READ_KEY, |
51
|
|
|
self::BASE_URL, |
52
|
|
|
$this->client, |
53
|
|
|
$this->serializer |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
parent::setUp(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Test project pull |
61
|
|
|
*/ |
62
|
|
|
public function testPullProject() |
63
|
|
|
{ |
64
|
|
|
$responseString = $this->getPullProjectResponseString(); |
65
|
|
|
$response = new Response(200, [], $responseString); |
66
|
|
|
|
67
|
|
|
$this->client |
68
|
|
|
->method('get') |
69
|
|
|
->willReturn($response) |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
$this->client |
|
|
|
|
73
|
|
|
->expects($this->once()) |
74
|
|
|
->method('get') |
75
|
|
|
->with(self::PROJECT_URL_NORMALIZED) |
76
|
|
|
; |
77
|
|
|
|
78
|
|
|
$this->serializer |
79
|
|
|
->method('deserialize') |
80
|
|
|
->willReturn($this->getProjectDTO()) |
81
|
|
|
; |
82
|
|
|
|
83
|
|
|
$this->serializer |
|
|
|
|
84
|
|
|
->expects($this->once()) |
85
|
|
|
->method('deserialize') |
86
|
|
|
->with($responseString, PullProjectResponseDTO::class, WebTranslateItRepository::FORMAT_JSON) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$this->assertEquals($this->getProjectDTO(), $this->repository->pullProject()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test pull file |
94
|
|
|
*/ |
95
|
|
|
public function testPullFile() |
96
|
|
|
{ |
97
|
|
|
$responseString = $this->getPullFileResponseString(); |
98
|
|
|
$response = new Response(200, [], $responseString); |
99
|
|
|
|
100
|
|
|
$this->client |
101
|
|
|
->method('get') |
102
|
|
|
->willReturn($response) |
103
|
|
|
; |
104
|
|
|
|
105
|
|
|
$this->client |
|
|
|
|
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('get') |
108
|
|
|
->with(self::FILE_URL_NORMALIZED) |
109
|
|
|
; |
110
|
|
|
|
111
|
|
|
$this->assertEquals($responseString, $this->repository->pullFile(self::FILE_NAME)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function getPullProjectResponseString() |
118
|
|
|
{ |
119
|
|
|
return '{"project":{"id":"45"}}'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return ProjectDTO |
124
|
|
|
*/ |
125
|
|
|
private function getProjectDTO() |
126
|
|
|
{ |
127
|
|
|
$projectDTO = new ProjectDTO(); |
128
|
|
|
|
129
|
|
|
return $projectDTO |
130
|
|
|
->setId(45) |
131
|
|
|
->setName('test') |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
private function getPullFileResponseString() |
139
|
|
|
{ |
140
|
|
|
return 'hello: Hello'; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: