Skip to content

Commit c449e15

Browse files
committed
feat: add file source and URL source tests with various configurations
1 parent d638898 commit c449e15

23 files changed

+792
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
class TestClass
6+
{
7+
private string $property = 'test';
8+
9+
public function testMethod(): string
10+
{
11+
return $this->property;
12+
}
13+
14+
public function anotherMethod(): void
15+
{
16+
// Just a comment
17+
echo "Hello World";
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
documents:
2+
- description: "Basic File Source Test"
3+
outputPath: "file-source.md"
4+
sources:
5+
- type: file
6+
description: "Basic PHP Files"
7+
sourcePaths: "./"
8+
filePattern: "*.php"
9+
notPath: ["nested"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
documents:
2+
- description: "Content Filter Test"
3+
outputPath: "content-filter.md"
4+
sources:
5+
- type: file
6+
description: "Files Filtered by Content"
7+
sourcePaths: "./"
8+
filePattern: "*.php"
9+
contains: "testMethod"
10+
notContains: "This is a nested class"
11+
notPath: ["nested"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
documents:
2+
- description: "Filtered File Source Test"
3+
outputPath: "filtered-files.md"
4+
sources:
5+
- type: file
6+
description: "Filtered PHP Files"
7+
sourcePaths: "./"
8+
filePattern: "*.php"
9+
contains: "testMethod"
10+
notPath: ["nested"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
documents:
2+
- description: "Multiple File Patterns Test"
3+
outputPath: "multiple-patterns.md"
4+
sources:
5+
- type: file
6+
description: "Multiple File Types"
7+
sourcePaths: "./"
8+
filePattern: ["*.php", "*.js", "*.txt"]
9+
notPath: ["nested"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Nested;
4+
5+
class NestedClass
6+
{
7+
public function nestedMethod(): string
8+
{
9+
return 'This is a nested class method';
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
documents:
2+
- description: "Path Filter Test"
3+
outputPath: "path-filter.md"
4+
sources:
5+
- type: file
6+
description: "Files Filtered by Path"
7+
sourcePaths: "./"
8+
filePattern: "*.php"
9+
path: "nested"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a sample text file.
2+
It contains multiple lines.
3+
Used for testing file source functionality.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function helloWorld() {
2+
console.log("Hello World!");
3+
}
4+
5+
const add = (a, b) => a + b;
6+
7+
document.addEventListener('DOMContentLoaded', helloWorld);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
documents:
2+
- description: "Tree View File Source Test"
3+
outputPath: "tree-view.md"
4+
sources:
5+
- type: file
6+
description: "Files with Tree View"
7+
sourcePaths: "./"
8+
filePattern: "*.php"
9+
treeView:
10+
enabled: true
11+
showSize: true
12+
showLastModified: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
documents:
2+
- description: "Basic URL Source Test"
3+
outputPath: "url-source.md"
4+
sources:
5+
- type: url
6+
description: "Basic URL Source"
7+
urls:
8+
- "https://example.com/api"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
documents:
2+
- description: "Failed Request Test"
3+
outputPath: "failed-request.md"
4+
sources:
5+
- type: url
6+
description: "Failed Request Source"
7+
urls:
8+
- "https://example.com/not-found"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
documents:
2+
- description: "Multiple URLs Test"
3+
outputPath: "multiple-urls.md"
4+
sources:
5+
- type: url
6+
description: "Multiple URLs Source"
7+
urls:
8+
- "https://example.com/page1"
9+
- "https://example.com/page2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
documents:
2+
- description: "URL Source with Headers"
3+
outputPath: "headers-test.md"
4+
sources:
5+
- type: url
6+
description: "URL Source with Headers"
7+
urls:
8+
- "https://api.example.com/data"
9+
headers:
10+
Authorization: "Bearer test-token"
11+
Accept: "application/json"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
documents:
2+
- description: "URL Source with Selector"
3+
outputPath: "selector-test.md"
4+
sources:
5+
- type: url
6+
description: "URL Source with Selector"
7+
urls:
8+
- "https://example.com/docs"
9+
selector: ".content"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
documents:
2+
- description: "URL Source with Variables"
3+
outputPath: "variables-test.md"
4+
sources:
5+
- type: url
6+
description: "URL Source with Variables"
7+
urls:
8+
- "https://api.${ENV_NAME}.example.com/data"
9+
headers:
10+
Authorization: "Bearer ${API_TOKEN}"
11+
Accept: "application/json"

tests/src/Feature/Console/GenerateCommand/CompilingResult.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function assertNoDocumentsToCompile(): self
4141
return $this;
4242
}
4343

44-
public function assertContextContains(string $document, array $strings): self
44+
public function assertContext(string $document, array $contains, array $notContains = []): self
4545
{
4646
foreach ($this->result['result'] as $documentData) {
4747
if ($documentData['context_path'] === $document) {
@@ -50,7 +50,7 @@ public function assertContextContains(string $document, array $strings): self
5050
);
5151

5252
$content = \file_get_contents($contextPath);
53-
foreach ($strings as $string) {
53+
foreach ($contains as $string) {
5454
TestCase::assertStringContainsString(
5555
$string,
5656
$content,
@@ -62,6 +62,18 @@ public function assertContextContains(string $document, array $strings): self
6262
);
6363
}
6464

65+
foreach ($notContains as $string) {
66+
TestCase::assertStringNotContainsString(
67+
$string,
68+
$content,
69+
\sprintf(
70+
'Context file [%s] should not contain string [%s]',
71+
$documentData['context_path'],
72+
$string,
73+
),
74+
);
75+
}
76+
6577
return $this;
6678
}
6779
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature\Console\GenerateCommand;
6+
7+
use Spiral\Console\Console;
8+
use Symfony\Component\Console\Input\ArrayInput;
9+
use Symfony\Component\Console\Output\BufferedOutput;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
final readonly class ContextBuilder
13+
{
14+
public function __construct(
15+
private Console $console,
16+
public int $defaultVerbosityLevel = OutputInterface::VERBOSITY_NORMAL,
17+
) {}
18+
19+
public function build(
20+
string $workDir,
21+
?string $configPath = null,
22+
?string $inlineJson = null,
23+
?string $envFile = null,
24+
string $command = 'generate',
25+
bool $asJson = true,
26+
): CompilingResult {
27+
$args = [];
28+
29+
if ($configPath !== null) {
30+
$args['--config-file'] = $configPath;
31+
}
32+
33+
if ($inlineJson !== null) {
34+
$args['--inline'] = $inlineJson;
35+
}
36+
37+
if ($workDir !== null) {
38+
$args['--work-dir'] = $workDir;
39+
}
40+
41+
if ($envFile !== null) {
42+
$args['--env'] = $envFile;
43+
}
44+
45+
if ($asJson) {
46+
$args['--json'] = true;
47+
}
48+
49+
$output = $this->runCommand(
50+
command: $command,
51+
args: $args,
52+
);
53+
54+
$output = \trim($output);
55+
$data = \json_decode($output, true);
56+
57+
if (!$data) {
58+
throw new \RuntimeException('Failed to decode JSON output: ' . \json_last_error_msg());
59+
}
60+
61+
return new CompilingResult($data);
62+
}
63+
64+
private function runCommand(
65+
string $command,
66+
array $args = [],
67+
?OutputInterface $output = null,
68+
?int $verbosityLevel = null,
69+
): string {
70+
$input = new ArrayInput($args);
71+
$input->setInteractive(false);
72+
$output = $output ?? new BufferedOutput();
73+
/** @psalm-suppress ArgumentTypeCoercion */
74+
$output->setVerbosity($verbosityLevel ?? $this->defaultVerbosityLevel);
75+
76+
$this->console->run($command, $input, $output);
77+
78+
return $output->fetch();
79+
}
80+
}

0 commit comments

Comments
 (0)