|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ozean12\WebTranslateItBundle\Tests\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Ozean12\WebTranslateItBundle\DTO\ProjectFileDTO; |
|
6
|
|
|
use Ozean12\WebTranslateItBundle\Service\WebTranslateItFileService; |
|
7
|
|
|
use Ozean12\WebTranslateItBundle\Service\WebTranslateItRepository; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class WebTranslateItFileServiceTest |
|
11
|
|
|
*/ |
|
12
|
|
|
class WebTranslateItFileServiceTest extends \PHPUnit_Framework_TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var WebTranslateItFileService |
|
16
|
|
|
*/ |
|
17
|
|
|
private $service; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var WebTranslateItRepository|\PHPUnit_Framework_MockObject_MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $translationRepository; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Set up |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->translationRepository = $this |
|
30
|
|
|
->getMockBuilder(WebTranslateItRepository::class) |
|
31
|
|
|
->disableOriginalConstructor() |
|
32
|
|
|
->getMock() |
|
33
|
|
|
; |
|
34
|
|
|
|
|
35
|
|
|
$this->service = new WebTranslateItFileService($this->translationRepository); |
|
36
|
|
|
|
|
37
|
|
|
parent::setUp(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Tear down |
|
42
|
|
|
*/ |
|
43
|
|
|
public function tearDown() |
|
44
|
|
|
{ |
|
45
|
|
|
$testDirectory = $this->getTranslationDirectory(); |
|
46
|
|
|
if (file_exists($testDirectory)) { |
|
47
|
|
|
$testFile = sprintf('%s/%s', $testDirectory, $this->getProjectFileDTO()->getName()); |
|
48
|
|
|
if (file_exists($testFile)) { |
|
49
|
|
|
unlink($testFile); |
|
50
|
|
|
} |
|
51
|
|
|
rmdir($testDirectory); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
parent::tearDown(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Test should be updated with local file not present |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testShouldBeUpdatedWithNoLocalFile() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->service->prepare($this->getTranslationDirectory()); |
|
63
|
|
|
|
|
64
|
|
|
$projectFile = new ProjectFileDTO(); |
|
65
|
|
|
$filePath = sprintf('%s/%s', $this->getTranslationDirectory(), 'some_other_file.yml'); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals(true, $this->service->shouldBeUpdated($filePath, $projectFile)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Test should be updated |
|
72
|
|
|
* |
|
73
|
|
|
* @dataProvider shouldBeUpdatedDataProvider |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $localTimestamp |
|
76
|
|
|
* @param string $remoteTimestamp |
|
77
|
|
|
* @param string $localContent |
|
78
|
|
|
* @param string $remoteContent |
|
79
|
|
|
* @param bool $expectedResult |
|
80
|
|
|
* @param string $message |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testShouldBeUpdated( |
|
83
|
|
|
$localTimestamp, |
|
84
|
|
|
$remoteTimestamp, |
|
85
|
|
|
$localContent, |
|
86
|
|
|
$remoteContent, |
|
87
|
|
|
$expectedResult, |
|
88
|
|
|
$message |
|
89
|
|
|
) { |
|
90
|
|
|
$this->service->prepare($this->getTranslationDirectory()); |
|
91
|
|
|
$filePath = sprintf('%s/%s', $this->getTranslationDirectory(), $this->getProjectFileDTO()->getName()); |
|
92
|
|
|
file_put_contents($filePath, $localContent); |
|
93
|
|
|
touch($filePath, (new \DateTime($localTimestamp))->getTimestamp()); |
|
94
|
|
|
|
|
95
|
|
|
$projectFile = new ProjectFileDTO(); |
|
96
|
|
|
$projectFile |
|
97
|
|
|
->setUpdatedAt(new \DateTime($remoteTimestamp)) |
|
98
|
|
|
->setHashFile(sha1($remoteContent)) |
|
99
|
|
|
; |
|
100
|
|
|
|
|
101
|
|
|
$this->assertEquals($expectedResult, $this->service->shouldBeUpdated($filePath, $projectFile), $message); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function shouldBeUpdatedDataProvider() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
['today', 'today', 'hello', 'hello', false, 'Everything equal - skip'], |
|
111
|
|
|
['yesterday', 'today', 'hello', 'hello', true, 'Local file is older - pull'], |
|
112
|
|
|
['today', 'yesterday', 'hello', 'hello', false, 'Local file is newer - skip'], |
|
113
|
|
|
['today', 'today', 'hello', 'hello1', true, 'Hash doesn\'t match - pull'], |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Test update |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testUpdate() |
|
121
|
|
|
{ |
|
122
|
|
|
$projectFile = $this->getProjectFileDTO(); |
|
123
|
|
|
$filePath = sprintf('%s/%s', $this->getTranslationDirectory(), $projectFile->getName()); |
|
124
|
|
|
$this->translationRepository->method('pullFile')->willReturn($this->getPullFileContent()); |
|
125
|
|
|
$this->translationRepository->expects($this->once())->method('pullFile')->with($projectFile->getName()); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$this->service->prepare($this->getTranslationDirectory()); |
|
128
|
|
|
$this->service->update($filePath, $projectFile); |
|
129
|
|
|
$this->assertFileExists($filePath); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Test prepare |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testPrepare() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->service->prepare($this->getTranslationDirectory()); |
|
138
|
|
|
$this->assertFileExists($this->getTranslationDirectory()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
private function getTranslationDirectory() |
|
145
|
|
|
{ |
|
146
|
|
|
return __DIR__.'/translations'; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
private function getPullFileContent() |
|
153
|
|
|
{ |
|
154
|
|
|
return 'hello: Hello'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return ProjectFileDTO |
|
159
|
|
|
*/ |
|
160
|
|
|
private function getProjectFileDTO() |
|
161
|
|
|
{ |
|
162
|
|
|
$file = new ProjectFileDTO(); |
|
163
|
|
|
|
|
164
|
|
|
return $file |
|
165
|
|
|
->setId(43) |
|
166
|
|
|
->setName('test_messages.yml') |
|
167
|
|
|
; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
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: