Skip to content

Commit ec00458

Browse files
committed
Simplfying IntegrationTest
1 parent ce6ccab commit ec00458

File tree

5 files changed

+72
-42
lines changed

5 files changed

+72
-42
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"SymfonyDocsBuilder\\": "src"
99
}
1010
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"SymfonyDocsBuilder\\Tests\\": "tests"
14+
}
15+
},
1116
"require": {
1217
"ext-json": "*",
1318
"ext-curl": "*",

src/BuildResult.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SymfonyDocsBuilder;
44

55
use Doctrine\RST\Builder;
6+
use Doctrine\RST\Meta\Metas;
67
use Symfony\Component\Console\Output\NullOutput;
78
use Symfony\Component\Filesystem\Filesystem;
89
use SymfonyDocsBuilder\BuildConfig;
@@ -15,10 +16,13 @@
1516
class BuildResult
1617
{
1718
private $errors;
19+
private $metas;
20+
private $jsonResults = [];
1821

19-
public function __construct(array $errors)
22+
public function __construct(array $errors, Metas $metas)
2023
{
2124
$this->errors = $errors;
25+
$this->metas = $metas;
2226
}
2327

2428
public function appendError(string $errorMessage): void
@@ -40,4 +44,24 @@ public function getErrors(): array
4044
{
4145
return $this->errors;
4246
}
47+
48+
public function getMetas(): Metas
49+
{
50+
return $this->metas;
51+
}
52+
53+
/**
54+
* Returns the JSON data generated for each file, keyed by the source filename.
55+
*
56+
* @return string[]
57+
*/
58+
public function getJsonResults(): array
59+
{
60+
return $this->jsonResults;
61+
}
62+
63+
public function setJsonResults(array $jsonResults): void
64+
{
65+
$this->jsonResults = $jsonResults;
66+
}
4367
}

src/DocBuilder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public function build(BuildConfig $config): BuildResult
2525
$builder = new Builder(KernelFactory::createKernel($config));
2626
$builder->build($config->getContentDir(), $config->getOutputDir());
2727

28-
$buildResult = new BuildResult($builder->getErrorManager()->getErrors());
28+
$buildResult = new BuildResult(
29+
$builder->getErrorManager()->getErrors(),
30+
$builder->getMetas()
31+
);
2932

3033
$missingFilesChecker = new MissingFilesChecker($config);
3134
$missingFiles = $missingFilesChecker->getMissingFiles();
@@ -38,13 +41,13 @@ public function build(BuildConfig $config): BuildResult
3841
$filesystem->dumpFile($config->getOutputDir().'/build_errors.txt', implode("\n", $buildResult->getErrors()));
3942
}
4043

41-
$metas = $builder->getMetas();
44+
$metas = $buildResult->getMetas();
4245
if ($config->getSubdirectoryToBuild()) {
4346
$htmlForPdfGenerator = new HtmlForPdfGenerator($metas, $config);
4447
$htmlForPdfGenerator->generateHtmlForPdf();
4548
} else {
4649
$jsonGenerator = new JsonGenerator($metas, $config);
47-
$jsonGenerator->generateJson();
50+
$buildResult->setJsonResults($jsonGenerator->generateJson());
4851
}
4952

5053
return $buildResult;

tests/AbstractIntegrationTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace SymfonyDocsBuilder\Tests;
4+
5+
use Doctrine\RST\Builder;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\Filesystem\Filesystem;
8+
use SymfonyDocsBuilder\BuildConfig;
9+
10+
class AbstractIntegrationTest extends TestCase
11+
{
12+
protected function createBuildConfig(string $sourceDir): BuildConfig
13+
{
14+
return (new BuildConfig())
15+
->setSymfonyVersion('4.0')
16+
->setContentDir($sourceDir)
17+
->disableBuildCache()
18+
->setOutputDir(__DIR__.'/_output')
19+
;
20+
}
21+
22+
/**
23+
* @after
24+
*/
25+
public function cleanUpOutput()
26+
{
27+
$filesystem = new Filesystem();
28+
$filesystem->remove(__DIR__.'/_output');
29+
}
30+
}

tests/IntegrationTest.php

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,25 @@
99

1010
namespace SymfonyDocsBuilder\Tests;
1111

12-
use Doctrine\RST\Builder;
1312
use Doctrine\RST\Configuration;
14-
use Doctrine\RST\Meta\CachedMetasLoader;
15-
use Doctrine\RST\Meta\Metas;
1613
use Doctrine\RST\Parser;
1714
use Gajus\Dindent\Indenter;
18-
use PHPUnit\Framework\TestCase;
19-
use Symfony\Component\Console\Helper\ProgressBar;
20-
use Symfony\Component\Console\Output\NullOutput;
2115
use Symfony\Component\DomCrawler\Crawler;
22-
use Symfony\Component\Filesystem\Filesystem;
2316
use Symfony\Component\Finder\Finder;
24-
use SymfonyDocsBuilder\BuildConfig;
17+
use SymfonyDocsBuilder\DocBuilder;
2518
use SymfonyDocsBuilder\Generator\JsonGenerator;
2619
use SymfonyDocsBuilder\KernelFactory;
2720

28-
class IntegrationTest extends TestCase
21+
class IntegrationTest extends AbstractIntegrationTest
2922
{
3023
/**
3124
* @dataProvider integrationProvider
3225
*/
3326
public function testIntegration(string $folder)
3427
{
35-
$fs = new Filesystem();
36-
$fs->remove([__DIR__.'/_output', __DIR__.'/_cache']);
37-
$fs->mkdir([__DIR__.'/_output', __DIR__.'/_cache']);
38-
3928
$buildConfig = $this->createBuildConfig(sprintf('%s/fixtures/source/%s', __DIR__, $folder));
40-
41-
$builder = new Builder(
42-
KernelFactory::createKernel($buildConfig)
43-
);
44-
45-
$builder->build(
46-
sprintf('%s/fixtures/source/%s', __DIR__, $folder),
47-
__DIR__.'/_output'
48-
);
29+
$builder = new DocBuilder();
30+
$buildResult = $builder->build($buildConfig);
4931

5032
$finder = new Finder();
5133
$finder->in(sprintf('%s/fixtures/expected/%s', __DIR__, $folder))
@@ -55,7 +37,7 @@ public function testIntegration(string $folder)
5537
$indenter = $this->createIndenter();
5638
foreach ($finder as $expectedFile) {
5739
$relativePath = $expectedFile->getRelativePathname();
58-
$actualFilename = __DIR__.'/_output/'.$relativePath;
40+
$actualFilename = $buildConfig->getOutputDir().'/'.$relativePath;
5941
$this->assertFileExists($actualFilename);
6042

6143
$this->assertSame(
@@ -66,13 +48,9 @@ public function testIntegration(string $folder)
6648
);
6749
}
6850

69-
$metas = $builder->getMetas();
70-
$jsonGenerator = new JsonGenerator($metas, $buildConfig);
71-
$jsonGenerator->generateJson();
72-
7351
foreach ($finder as $htmlFile) {
7452
$relativePath = $htmlFile->getRelativePathname();
75-
$actualFilename = __DIR__.'/_output/'.str_replace('.html', '.fjson', $relativePath);
53+
$actualFilename = $buildConfig->getOutputDir().'/'.str_replace('.html', '.fjson', $relativePath);
7654
$this->assertFileExists($actualFilename);
7755

7856
$jsonData = json_decode(file_get_contents($actualFilename), true);
@@ -269,16 +247,6 @@ public function parserUnitBlockProvider()
269247
];
270248
}
271249

272-
private function createBuildConfig(string $sourceDir): BuildConfig
273-
{
274-
return (new BuildConfig())
275-
->setSymfonyVersion('4.0')
276-
->setContentDir($sourceDir)
277-
->disableBuildCache()
278-
->setOutputDir(__DIR__.'/_output')
279-
;
280-
}
281-
282250
private function createIndenter(): Indenter
283251
{
284252
$indenter = new Indenter();

0 commit comments

Comments
 (0)