Skip to content

Commit ace9a0e

Browse files
committed
move the BuilderTestCase from akeneo-plugin to phpunit-extension
1 parent 0ec5aa0 commit ace9a0e

File tree

5 files changed

+173
-124
lines changed

5 files changed

+173
-124
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"php-http/mock-client": "^1.4@dev",
1313
"fakerphp/faker": "^1.19",
1414
"laminas/laminas-diactoros": "^2.6",
15-
"akeneo/api-php-client": "^9.0"
15+
"mikey179/vfsstream": "^1.6"
1616
},
1717
"license": "MIT",
1818
"authors": [

composer.lock

Lines changed: 52 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BuilderTestCase.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Component\PHPUnitExtension;
4+
5+
use Kiboko\Contract\Pipeline\PipelineRunnerInterface;
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\vfsStreamDirectory;
8+
use PhpParser\Builder as DefaultBuilder;
9+
use PhpParser\Node;
10+
use PhpParser\PrettyPrinter;
11+
use PHPUnit\Framework\TestCase;
12+
13+
abstract class BuilderTestCase extends TestCase
14+
{
15+
private ?vfsStreamDirectory $fs = null;
16+
17+
protected function setUp(): void
18+
{
19+
$this->fs = vfsStream::setup();
20+
}
21+
22+
protected function tearDown(): void
23+
{
24+
$this->fs = null;
25+
}
26+
27+
protected function getBuilderCompilePath(): string
28+
{
29+
return $this->fs->url();
30+
}
31+
32+
public function pipelineRunner(): PipelineRunnerInterface
33+
{
34+
return new PipelineRunner();
35+
}
36+
37+
protected function assertNodeIsInstanceOf(string $expected, DefaultBuilder $builder, string $message = '')
38+
{
39+
$printer = new PrettyPrinter\Standard();
40+
41+
try {
42+
$filename = sha1(random_bytes(128)) .'.php';
43+
file_put_contents($this->fs->url() . '/' . $filename, $printer->prettyPrintFile([
44+
new Node\Stmt\Return_($builder->getNode()),
45+
]));
46+
47+
$actual = include $this->fs->url().'/'.$filename;
48+
} catch (\ParseError $exception) {
49+
echo $printer->prettyPrintFile([$builder->getNode()]);
50+
$this->fail($exception->getMessage());
51+
}
52+
53+
$this->assertInstanceOf($expected, $actual, $message);
54+
}
55+
56+
protected function assertNodeIsNotInstanceOf(string $expected, DefaultBuilder $builder, string $message = '')
57+
{
58+
$printer = new PrettyPrinter\Standard();
59+
60+
try {
61+
$filename = sha1(random_bytes(128)) .'.php';
62+
file_put_contents($this->fs->url() . '/' . $filename, $printer->prettyPrintFile([
63+
new Node\Stmt\Return_($builder->getNode()),
64+
]));
65+
66+
$actual = include $this->fs->url().'/'.$filename;
67+
} catch (\ParseError $exception) {
68+
echo $printer->prettyPrintFile([$builder->getNode()]);
69+
$this->fail($exception->getMessage());
70+
}
71+
72+
$this->assertNotInstanceOf($expected, $actual, $message);
73+
}
74+
}

src/Mock/ApiClientMockBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class: new Node\Name\FullyQualified($this->mockedClientBuilder),
2323
new Node\Scalar\String_('https://akeneo.'.$faker->safeEmailDomain()),
2424
),
2525
],
26-
);;
26+
);
2727
}
2828

2929
public function withHttpClient(Mock\HttpClientBuilder $httpClient): self

src/PipelineRunner.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Component\PHPUnitExtension;
4+
5+
use Kiboko\Contract\Bucket\AcceptanceResultBucketInterface;
6+
use Kiboko\Contract\Bucket\RejectionResultBucketInterface;
7+
use Kiboko\Contract\Pipeline\PipelineRunnerInterface;
8+
use Kiboko\Contract\Pipeline\RejectionInterface;
9+
use Kiboko\Contract\Pipeline\StateInterface;
10+
11+
final class PipelineRunner implements PipelineRunnerInterface
12+
{
13+
public function run(
14+
\Iterator $source,
15+
\Generator $async,
16+
RejectionInterface $rejection,
17+
StateInterface $state,
18+
): \Iterator {
19+
$state->initialize();
20+
$rejection->initialize();
21+
22+
$source->rewind();
23+
$async->rewind();
24+
25+
while ($source->valid() && $async->valid()) {
26+
$bucket = $async->send($source->current());
27+
28+
if ($bucket instanceof RejectionResultBucketInterface) {
29+
foreach ($bucket->walkRejection() as $line) {
30+
$rejection->reject($line);
31+
$state->reject();
32+
}
33+
}
34+
if ($bucket instanceof AcceptanceResultBucketInterface) {
35+
yield from $bucket->walkAcceptance();
36+
$state->accept();
37+
}
38+
39+
$source->next();
40+
}
41+
42+
$rejection->teardown();
43+
$state->teardown();
44+
}
45+
}

0 commit comments

Comments
 (0)