Skip to content

Commit ecc8909

Browse files
authored
Merge pull request #155 from context-hub/maintenance/feature-tests
tests: added feature tests for document compiler
2 parents 50be967 + 4afebaa commit ecc8909

File tree

62 files changed

+585
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+585
-96
lines changed

src/Application/Kernel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
use Spiral\Boot\AbstractKernel;
3434
use Spiral\Boot\Exception\BootException;
3535

36-
final class Kernel extends AbstractKernel
36+
class Kernel extends AbstractKernel
3737
{
3838
#[\Override]
3939
protected function defineSystemBootloaders(): array

src/Document/Compiler/DocumentCompiler.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Butschster\ContextGenerator\Document\Compiler;
66

7+
use Butschster\ContextGenerator\Application\FSPath;
78
use Butschster\ContextGenerator\Application\Logger\LoggerPrefix;
89
use Butschster\ContextGenerator\Document\Compiler\Error\ErrorCollection;
910
use Butschster\ContextGenerator\Document\Compiler\Error\SourceError;
@@ -50,7 +51,7 @@ public function compile(Document $document): CompiledDocument
5051
]);
5152

5253
$errors = new ErrorCollection();
53-
$resultPath = \rtrim($this->basePath, '/') . '/' . \ltrim($outputPath, '/');
54+
$resultPath = (string) FSPath::create($this->basePath)->join($outputPath);
5455

5556
if (!$document->overwrite && $this->files->exists($resultPath)) {
5657
$this->logger?->notice('Document already exists and overwrite is disabled', [

src/Lib/Content/ContentBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,6 @@ public function build(): string
168168

169169
public function __toString(): string
170170
{
171-
return $this->build();
171+
return (string) \preg_replace("/(\r\n|\n)+$/", '', $this->build());
172172
}
173173
}

src/Lib/Content/Renderer/AbstractRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function renderContent(array $blocks): string
2020
continue;
2121
}
2222

23-
$content .= $block->render($this);
23+
$content .= \preg_replace("/(\r\n|\n)+$/", "\n", $block->render($this));
2424
}
2525

2626
return $content;

tests/fixtures/Compiler/context.json

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/context-hub/generator/refs/heads/main/json-schema.json",
3+
"documents": [
4+
{
5+
"description": "Project structure",
6+
"outputPath": "structure.md",
7+
"sources": [
8+
{
9+
"type": "tree",
10+
"sourcePaths": [
11+
"src"
12+
],
13+
"showCharCount": true,
14+
"showSize": true
15+
}
16+
]
17+
},
18+
{
19+
"description": "This is a test document",
20+
"outputPath": "test-document.md",
21+
"sources": [
22+
{
23+
"type": "file",
24+
"sourcePaths": [
25+
"src/dir1"
26+
],
27+
"filePattern": "*.php"
28+
},
29+
{
30+
"type": "text",
31+
"content": "Foo Bar"
32+
}
33+
]
34+
},
35+
{
36+
"description": "This is a test document 1",
37+
"outputPath": "test-document1.md",
38+
"sources": [
39+
{
40+
"type": "file",
41+
"sourcePaths": [
42+
"src/dir2"
43+
],
44+
"filePattern": "*.php"
45+
},
46+
{
47+
"type": "text",
48+
"content": "Foo Bar"
49+
}
50+
]
51+
},
52+
{
53+
"description": "This is a test document 2",
54+
"outputPath": "test-document2.md",
55+
"sources": [
56+
{
57+
"type": "file",
58+
"sourcePaths": [
59+
"src"
60+
],
61+
"filePattern": "*.txt"
62+
},
63+
{
64+
"type": "text",
65+
"content": "Foo Bar"
66+
}
67+
]
68+
}
69+
]
70+
}

tests/fixtures/Compiler/context.yaml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"$schema": https://raw.githubusercontent.com/context-hub/generator/refs/heads/main/json-schema.json
2+
3+
documents:
4+
- description: Project structure
5+
outputPath: structure.md
6+
sources:
7+
- type: tree
8+
sourcePaths:
9+
- src
10+
showCharCount: true
11+
showSize: true
12+
13+
- description: This is a test document
14+
outputPath: test-document.md
15+
sources:
16+
- type: file
17+
sourcePaths:
18+
- src/dir1
19+
filePattern: "*.php"
20+
- type: text
21+
content: Foo Bar
22+
23+
- description: This is a test document 1
24+
outputPath: test-document1.md
25+
sources:
26+
- type: file
27+
sourcePaths:
28+
- src/dir2
29+
filePattern: "*.php"
30+
- type: text
31+
content: Foo Bar
32+
33+
- description: This is a test document 2
34+
outputPath: test-document2.md
35+
sources:
36+
- type: file
37+
sourcePaths:
38+
- src
39+
filePattern: "*.txt"
40+
- type: text
41+
content: Foo Bar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
final readonly class Test1Class {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
final readonly class TestClass {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo baf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
final readonly class Test3Class {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo bar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace dir2;
6+
7+
final readonly class Test2Class {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo baz

tests/src/AppTestCase.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Butschster\ContextGenerator\Application\Application;
8+
use Butschster\ContextGenerator\Application\Kernel;
9+
use Spiral\Boot\Environment;
10+
use Spiral\Core\Container;
11+
12+
abstract class AppTestCase extends TestCase
13+
{
14+
private TestableKernelInterface&Kernel $app;
15+
16+
public function defineDirectories(string $root): array
17+
{
18+
return [
19+
'root' => $root,
20+
'output' => $root . '/.context',
21+
'config' => $root,
22+
'json-schema' => $root,
23+
];
24+
}
25+
26+
public function rootDirectory(): string
27+
{
28+
return \dirname(__DIR__);
29+
}
30+
31+
public function getApp(): TestableKernelInterface
32+
{
33+
if (!isset($this->app)) {
34+
$this->app = $this->initApp();
35+
}
36+
37+
return $this->app;
38+
}
39+
40+
public function createAppInstance(Container $container = new Container()): TestableKernelInterface
41+
{
42+
return TestApp::create(
43+
directories: $this->defineDirectories(
44+
$this->rootDirectory(),
45+
),
46+
handleErrors: false,
47+
container: $container,
48+
);
49+
}
50+
51+
/**
52+
* @param array<non-empty-string,mixed> $env
53+
*/
54+
public function makeApp(array $env = [], Container $container = new Container()): Kernel&TestableKernelInterface
55+
{
56+
$environment = new Environment($env);
57+
58+
$app = $this->createAppInstance($container);
59+
$app->run($environment);
60+
61+
return $app;
62+
}
63+
64+
public function initApp(array $env = [], Container $container = new Container()): Kernel&TestableKernelInterface
65+
{
66+
$container->bindSingleton(
67+
Application::class,
68+
new Application(
69+
version: '1.0.0',
70+
name: 'Context Generator',
71+
isBinary: true,
72+
),
73+
);
74+
75+
return $this->makeApp($env, $container);
76+
}
77+
78+
public function getContainer(): Container
79+
{
80+
return $this->getApp()->getContainer();
81+
}
82+
83+
/**
84+
* @template T of object
85+
* @param class-string<T> $id
86+
* @return T
87+
*/
88+
public function get(string $id): mixed
89+
{
90+
return $this->getApp()->getContainer()->get($id);
91+
}
92+
93+
protected function setUp(): void
94+
{
95+
parent::setUp();
96+
97+
$this->initApp();
98+
}
99+
}

0 commit comments

Comments
 (0)