|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the trefoil application. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Miguel Angel Gabriel <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Trefoil\Tests; |
|
11
|
|
|
|
|
12
|
|
|
use Easybook\Tests\TestCase; |
|
13
|
|
|
use Easybook\Util\Toolkit; |
|
14
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
15
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
16
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
19
|
|
|
use Symfony\Component\Finder\Finder; |
|
20
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
21
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
22
|
|
|
use Trefoil\Console\ConsoleApplication; |
|
23
|
|
|
use Trefoil\DependencyInjection\Application; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* PHPUnit test case for trefoil books. |
|
27
|
|
|
* |
|
28
|
|
|
* To use it, extend from this class and set the fixtures directory in the |
|
29
|
|
|
* constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* Usage: |
|
32
|
|
|
* "phpunit my/path/to/MyTest.php [arguments]" |
|
33
|
|
|
* |
|
34
|
|
|
* where: |
|
35
|
|
|
* - "MyTest" is a class that extends this. |
|
36
|
|
|
* - [arguments] are optional: |
|
37
|
|
|
* - "--debug" will show verbose messages and leave the test output |
|
38
|
|
|
* files for inspection after execution (such as current results). |
|
39
|
|
|
* - ":fixture-name" will execute only one fixture instead of all. |
|
40
|
|
|
* |
|
41
|
|
|
* Example: |
|
42
|
|
|
* |
|
43
|
|
|
* "phpunit --debug src/Trefoil/Tests/Plugins/PluginsTest.php :book-test-LinkCheckPlugin" |
|
44
|
|
|
* |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract class BookPublishingAllTestCase extends TestCase |
|
47
|
|
|
{ |
|
48
|
|
|
protected $tmpDirBase; |
|
49
|
|
|
protected $tmpDir; |
|
50
|
|
|
protected $fixturesDir; |
|
51
|
|
|
protected $app; |
|
52
|
|
|
protected $filesystem; |
|
53
|
|
|
protected $console; |
|
54
|
|
|
protected $isDebug; |
|
55
|
|
|
|
|
56
|
|
|
protected static $currentBook; |
|
57
|
|
|
protected static $lastBook; |
|
58
|
|
|
protected static $someEditionOfSameBookHasErrors = null; |
|
59
|
|
|
|
|
60
|
|
|
public function __construct($name = null, array $data = array(), $dataName = '') |
|
61
|
|
|
{ |
|
62
|
|
|
parent::__construct($name, $data, $dataName); |
|
63
|
|
|
|
|
64
|
|
|
$this->app = new Application(); |
|
65
|
|
|
$this->filesystem = new Filesystem(); |
|
66
|
|
|
$this->console = new ConsoleApplication($this->app); |
|
67
|
|
|
|
|
68
|
|
|
$this->isDebug = array_key_exists('debug', getopt('', array('debug'))); |
|
69
|
|
|
|
|
70
|
|
|
$className = basename(str_replace('\\', '/', get_called_class())); |
|
71
|
|
|
$this->tmpDirBase = $this->app['app.dir.cache'] . '/' . 'phpunit_debug/' . $className; |
|
72
|
|
|
|
|
73
|
|
|
if ($this->filesystem->exists($this->tmpDirBase)) { |
|
74
|
|
|
$this->filesystem->remove($this->tmpDirBase); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function tearDown() |
|
79
|
|
|
{ |
|
80
|
|
|
$delete = true; |
|
81
|
|
|
|
|
82
|
|
|
if ($this->hasFailed()) { |
|
83
|
|
|
|
|
84
|
|
|
if (static::$currentBook == static::$lastBook) { |
|
85
|
|
|
static::$someEditionOfSameBookHasErrors = true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($this->isDebug) { |
|
89
|
|
|
echo ">>> Actual and expected results not deleted: " . $this->tmpDir; |
|
90
|
|
|
$delete = false; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($delete && !static::$someEditionOfSameBookHasErrors) { |
|
95
|
|
|
$this->filesystem->remove($this->tmpDir); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
parent::tearDown(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Fixtures provider. |
|
103
|
|
|
* |
|
104
|
|
|
* @throws \Exception |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function bookProvider() |
|
108
|
|
|
{ |
|
109
|
|
|
if (empty($this->fixturesDir) || !file_exists($this->fixturesDir)) { |
|
110
|
|
|
throw new \Exception('[ERROR] Please provide a value for $this->fixturesDir'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($this->isDebug) { |
|
114
|
|
|
echo '> Using fixtures from ' . $this->fixturesDir . "\n"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// find the test books |
|
118
|
|
|
$fixtures = Finder::create() |
|
119
|
|
|
->directories() |
|
120
|
|
|
->name('book*') |
|
121
|
|
|
->depth(0) |
|
122
|
|
|
->sortByName() |
|
123
|
|
|
->in($this->fixturesDir); |
|
124
|
|
|
|
|
125
|
|
|
$books = array(); |
|
126
|
|
|
|
|
127
|
|
|
// look if only one fixture should be tested |
|
128
|
|
|
global $argv; |
|
129
|
|
|
$fixtureName = end($argv); |
|
130
|
|
|
$fixtureName = substr($fixtureName, 0, 1) == ':' ? substr($fixtureName, 1) : ''; |
|
131
|
|
|
|
|
132
|
|
|
if ($fixtureName) { |
|
133
|
|
|
echo sprintf('> Testing only fixture "%s"', $fixtureName) . "\n"; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
foreach ($fixtures as $fixture) { |
|
137
|
|
|
$slug = $fixture->getFileName(); |
|
138
|
|
|
|
|
139
|
|
|
if ($fixtureName && $slug != $fixtureName) { |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// look for and publish all the book editions |
|
144
|
|
|
$bookConfigFile = $this->fixturesDir . '/' . $slug . '/input/config.yml'; |
|
145
|
|
|
|
|
146
|
|
|
$bookConfig = Yaml::parse($bookConfigFile); |
|
147
|
|
|
$editions = $bookConfig['book']['editions']; |
|
148
|
|
|
|
|
149
|
|
|
foreach ($editions as $editionName => $editionConfig) { |
|
150
|
|
|
$books[] = array( |
|
151
|
|
|
$slug, |
|
152
|
|
|
$editionName |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $books; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Publish and test one book. |
|
162
|
|
|
* |
|
163
|
|
|
* @dataProvider bookProvider |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testBookPublish($bookName, $editionName) |
|
166
|
|
|
{ |
|
167
|
|
|
$slug = $bookName; |
|
168
|
|
|
|
|
169
|
|
|
static::$lastBook = static::$currentBook ?: $bookName; |
|
170
|
|
|
static::$currentBook = $bookName; |
|
171
|
|
|
if (static::$currentBook != static::$lastBook) { |
|
172
|
|
|
static::$someEditionOfSameBookHasErrors = false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$this->tmpDir = $this->tmpDirBase . '/' . $slug; |
|
176
|
|
|
|
|
177
|
|
|
if ($this->isDebug) { |
|
178
|
|
|
echo sprintf("\n" . '- Processing test "%s"' . "\n", $slug); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$thisBookDir = $this->fixturesDir . $slug; |
|
182
|
|
|
|
|
183
|
|
|
// mirror test book contents in temp dir |
|
184
|
|
|
$this->filesystem->mirror( |
|
185
|
|
|
$thisBookDir . '/input', |
|
186
|
|
|
$this->tmpDir |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
// look for and publish the book edition |
|
190
|
|
|
$bookConfigFile = $this->tmpDir . '/config.yml'; |
|
191
|
|
|
|
|
192
|
|
|
// publish the book edition |
|
193
|
|
|
$input = new ArrayInput(array( |
|
194
|
|
|
'command' => 'publish', |
|
195
|
|
|
'slug' => $slug, |
|
196
|
|
|
'edition' => $editionName, |
|
197
|
|
|
'--dir' => $this->tmpDirBase, |
|
198
|
|
|
'--themes_dir' => $this->fixturesDir . 'Themes' |
|
199
|
|
|
)); |
|
200
|
|
|
|
|
201
|
|
|
$output = new NullOutput(); |
|
202
|
|
|
if ($this->isDebug) { |
|
203
|
|
|
// we want the full output in debug mode |
|
204
|
|
|
$output = new ConsoleOutput(OutputInterface::VERBOSITY_NORMAL, true); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$this->console->find('publish')->run($input, $output); |
|
208
|
|
|
|
|
209
|
|
|
// look for config.yml modification |
|
210
|
|
|
$expectedBookConfigFile = $thisBookDir . '/expected/config.yml'; |
|
211
|
|
|
if (file_exists($expectedBookConfigFile)) { |
|
212
|
|
|
$this->assertFileEquals( |
|
213
|
|
|
$expectedBookConfigFile, |
|
214
|
|
|
$bookConfigFile, |
|
215
|
|
|
'Book config.yml not modified correctly' |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
// assert that generated files are exactly the same as expected |
|
220
|
|
|
$generatedFiles = Finder::create() |
|
221
|
|
|
->files() |
|
222
|
|
|
->notName('.gitignore') |
|
223
|
|
|
->in($this->tmpDir . '/Output/' . $editionName); |
|
224
|
|
|
|
|
225
|
|
|
foreach ($generatedFiles as $file) { |
|
226
|
|
|
/* @var $file SplFileInfo */ |
|
227
|
|
|
|
|
228
|
|
|
if ('epub' == $file->getExtension()) { |
|
229
|
|
|
// unzip both files to compare its contents |
|
230
|
|
|
$workDir = $this->tmpDir . '/unzip/' . $editionName; |
|
231
|
|
|
$generated = $workDir . '/generated'; |
|
232
|
|
|
$expected = $workDir . '/expected'; |
|
233
|
|
|
|
|
234
|
|
|
Toolkit::unzip($file->getRealPath(), $generated); |
|
235
|
|
|
Toolkit::unzip( |
|
236
|
|
|
$thisBookDir . '/expected/' . |
|
237
|
|
|
$editionName . '/' . $file->getRelativePathname(), |
|
238
|
|
|
$expected |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
// assert that generated files insize EPUB are exactly the same as expected |
|
242
|
|
|
$this->checkGeneratedFiles($expected, $generated, $file->getPathName()); |
|
243
|
|
|
|
|
244
|
|
|
// assert that all required files inside EPUB are generated |
|
245
|
|
|
$this->checkForMissingFiles($expected, $generated); |
|
246
|
|
|
|
|
247
|
|
|
} elseif ('mobi' == $file->getExtension()) { |
|
248
|
|
|
// mobi files cannot be compared to expected results |
|
249
|
|
|
// because kindlegen does funny things with the contents |
|
250
|
|
|
// so do nothing |
|
251
|
|
|
|
|
252
|
|
|
} else { |
|
253
|
|
|
$this->assertFileEquals( |
|
254
|
|
|
$thisBookDir . '/expected/' . $editionName . '/' . $file->getRelativePathname(), |
|
255
|
|
|
$file->getPathname(), |
|
256
|
|
|
sprintf("'%s' file not properly generated", $file->getPathname()) |
|
257
|
|
|
); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// assert that all required files for this edition are generated |
|
261
|
|
|
$this->checkForMissingFiles( |
|
262
|
|
|
$thisBookDir . '/expected/' . $editionName, |
|
263
|
|
|
$this->tmpDir . '/Output/' . $editionName |
|
264
|
|
|
); |
|
265
|
|
|
|
|
266
|
|
|
// assert that book publication took less than 5 seconds |
|
267
|
|
|
$this->assertLessThan( |
|
268
|
|
|
10, |
|
269
|
|
|
$this->app['app.timer.finish'] - $this->app['app.timer.start'], |
|
270
|
|
|
sprintf("Publication of '%s' edition for '%s' book took more than 10 seconds", $editionName, $slug) |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Assert that all generated files have the expected contents. |
|
277
|
|
|
* |
|
278
|
|
|
* @param string $dirExpected |
|
279
|
|
|
* @param string $dirGenerated |
|
280
|
|
|
* @param string $zipName |
|
281
|
|
|
* |
|
282
|
|
|
* @apram string $zipName |
|
283
|
|
|
*/ |
|
284
|
|
View Code Duplication |
protected function checkGeneratedFiles($dirExpected, $dirGenerated, $zipName) |
|
285
|
|
|
{ |
|
286
|
|
|
$genFiles = Finder::create() |
|
287
|
|
|
->files() |
|
288
|
|
|
->notName('.gitignore') |
|
289
|
|
|
->in($dirGenerated); |
|
290
|
|
|
|
|
291
|
|
|
foreach ($genFiles as $genFile) { |
|
292
|
|
|
$this->assertFileEquals( |
|
293
|
|
|
$dirExpected . '/' . $genFile->getRelativePathname(), |
|
294
|
|
|
$genFile->getPathname(), |
|
295
|
|
|
sprintf( |
|
296
|
|
|
"'%s' file (into ZIP file '%s') not properly generated", |
|
297
|
|
|
$genFile->getRelativePathname(), |
|
298
|
|
|
$zipName |
|
299
|
|
|
) |
|
300
|
|
|
); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Assert that all expected files were generated. |
|
306
|
|
|
* |
|
307
|
|
|
* @param string $dirExpected |
|
308
|
|
|
* @param string $dirGenerated |
|
309
|
|
|
*/ |
|
310
|
|
View Code Duplication |
protected function checkForMissingFiles($dirExpected, $dirGenerated) |
|
311
|
|
|
{ |
|
312
|
|
|
$expectedFiles = Finder::create() |
|
313
|
|
|
->files() |
|
314
|
|
|
->notName('.gitignore') |
|
315
|
|
|
->in($dirExpected); |
|
316
|
|
|
|
|
317
|
|
|
foreach ($expectedFiles as $file) { |
|
318
|
|
|
$this->assertFileExists( |
|
319
|
|
|
$dirGenerated . '/' . $file->getRelativePathname(), |
|
320
|
|
|
sprintf("'%s' file has not been generated", $file->getPathname()) |
|
321
|
|
|
); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
|