Skip to content

Commit

Permalink
chore: add tests for context validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Mar 9, 2024
1 parent cdc6894 commit d3e3de0
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 2 deletions.
13 changes: 12 additions & 1 deletion bin/generate-tests.php
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;

use function Symfony\Component\String\u;
Expand Down Expand Up @@ -106,6 +107,15 @@
add_test($args, $class);
}

$dirs = (new Finder)
->in($basePath = __DIR__ . '/../tests/Examples/fixtures/broken')
->depth(1)
;
foreach ($dirs as $dir) {
$class = u($dir->getRelativePath())->camel()->title()->append('Test')->toString();
add_test([], $class, '{{ base }}/tests/Examples/fixtures/broken/' . $dir->getRelativePath());
}

add_test(['parallel:sleep', '--sleep5', '0', '--sleep7', '0', '--sleep10', '0'], 'ParallelSleepTest');
add_test(['context:context', '--context', 'run'], 'ContextContextRunTest');
add_test(['context:context', '--context', 'my_default', '-v'], 'ContextContextMyDefaultTest');
Expand All @@ -126,9 +136,10 @@ function add_test(array $args, string $class, ?string $cwd = null)
fseek($fp, __COMPILER_HALT_OFFSET__ + 1);
$template = stream_get_contents($fp);


$process = new Process(
[\PHP_BINARY, __DIR__ . '/castor', '--no-ansi', ...$args],
cwd: $cwd ?: __DIR__ . '/../',
cwd: $cwd ? str_replace('{{ base }}', __DIR__ . '/..', $cwd) : __DIR__ . '/..',
env: [
'COLUMNS' => 120,
'ENDPOINT' => $_SERVER['ENDPOINT'],
Expand Down
22 changes: 22 additions & 0 deletions tests/Examples/Generated/ContextGeneratorArg2Test.php
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Examples\Generated;

use Castor\Tests\TaskTestCase;

class ContextGeneratorArg2Test extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/Examples/fixtures/broken/context-generator-arg-2');

$this->assertSame(1, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
if (file_exists(__FILE__ . '.err.txt')) {
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
} else {
$this->assertSame('', $process->getErrorOutput());
}
}
}
8 changes: 8 additions & 0 deletions tests/Examples/Generated/ContextGeneratorArg2Test.php.err.txt
@@ -0,0 +1,8 @@

In FunctionFinder.php line 218:

Function "gen()" is not properly configured:
The context generator "foo" must not have arguments.
Defined in "castor.php" line 7.


Empty file.
22 changes: 22 additions & 0 deletions tests/Examples/Generated/ContextGeneratorArgTest.php
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Examples\Generated;

use Castor\Tests\TaskTestCase;

class ContextGeneratorArgTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/Examples/fixtures/broken/context-generator-arg');

$this->assertSame(1, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
if (file_exists(__FILE__ . '.err.txt')) {
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
} else {
$this->assertSame('', $process->getErrorOutput());
}
}
}
8 changes: 8 additions & 0 deletions tests/Examples/Generated/ContextGeneratorArgTest.php.err.txt
@@ -0,0 +1,8 @@

In FunctionFinder.php line 209:

Function "gen()" is not properly configured:
The contexts generator must not have arguments.
Defined in "castor.php" line 7.


Empty file.
22 changes: 22 additions & 0 deletions tests/Examples/Generated/ContextGeneratorNotCallableTest.php
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Examples\Generated;

use Castor\Tests\TaskTestCase;

class ContextGeneratorNotCallableTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/Examples/fixtures/broken/context-generator-not-callable');

$this->assertSame(1, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
if (file_exists(__FILE__ . '.err.txt')) {
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
} else {
$this->assertSame('', $process->getErrorOutput());
}
}
}
@@ -0,0 +1,8 @@

In FunctionFinder.php line 214:

Function "gen()" is not properly configured:
The context generator "foo" is not callable.
Defined in "castor.php" line 6.


Empty file.
22 changes: 22 additions & 0 deletions tests/Examples/Generated/NoDefaultContextTest.php
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Examples\Generated;

use Castor\Tests\TaskTestCase;

class NoDefaultContextTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/Examples/fixtures/broken/no-default-context');

$this->assertSame(1, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
if (file_exists(__FILE__ . '.err.txt')) {
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
} else {
$this->assertSame('', $process->getErrorOutput());
}
}
}
6 changes: 6 additions & 0 deletions tests/Examples/Generated/NoDefaultContextTest.php.err.txt
@@ -0,0 +1,6 @@

In ContextRegistry.php line XXXX:

Since there are multiple contexts "one", "two", you must set a "default: true" context.


Empty file.
22 changes: 22 additions & 0 deletions tests/Examples/Generated/TwoDefaultContextTest.php
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Examples\Generated;

use Castor\Tests\TaskTestCase;

class TwoDefaultContextTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/Examples/fixtures/broken/two-default-context');

$this->assertSame(1, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
if (file_exists(__FILE__ . '.err.txt')) {
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
} else {
$this->assertSame('', $process->getErrorOutput());
}
}
}
8 changes: 8 additions & 0 deletions tests/Examples/Generated/TwoDefaultContextTest.php.err.txt
@@ -0,0 +1,8 @@

In ContextRegistry.php line XXXX:

Function "two()" is not properly configured:
You cannot define two contexts with the same name "default". There is one already defined in "castor.php:7".
Defined in "castor.php" line 13.


Empty file.
10 changes: 10 additions & 0 deletions tests/Examples/fixtures/broken/context-generator-arg-2/castor.php
@@ -0,0 +1,10 @@
<?php

use Castor\Attribute\AsContextGenerator;
use Castor\Context;

#[AsContextGenerator()]
function gen(): iterable
{
yield 'foo' => fn($a) => new Context();;
}
10 changes: 10 additions & 0 deletions tests/Examples/fixtures/broken/context-generator-arg/castor.php
@@ -0,0 +1,10 @@
<?php

use Castor\Attribute\AsContextGenerator;
use Castor\Context;

#[AsContextGenerator()]
function gen($a): iterable
{
return new Context();
}
@@ -0,0 +1,9 @@
<?php

use Castor\Attribute\AsContextGenerator;

#[AsContextGenerator()]
function gen(): iterable
{
yield 'foo' => 'not a callable';
}
16 changes: 16 additions & 0 deletions tests/Examples/fixtures/broken/no-default-context/castor.php
@@ -0,0 +1,16 @@
<?php

use Castor\Attribute\AsContext;
use Castor\Context;

#[AsContext()]
function one(): Context
{
return new Context();
}

#[AsContext()]
function two(): Context
{
return new Context();
}
16 changes: 16 additions & 0 deletions tests/Examples/fixtures/broken/two-default-context/castor.php
@@ -0,0 +1,16 @@
<?php

use Castor\Attribute\AsContext;
use Castor\Context;

#[AsContext(default: true)]
function one(): Context
{
return new Context();
}

#[AsContext(default: true)]
function two(): Context
{
return new Context();
}
2 changes: 1 addition & 1 deletion tests/TaskTestCase.php
Expand Up @@ -36,7 +36,7 @@ public function runTask(array $args, ?string $cwd = null): Process

$process = new Process(
[$castorBin, '--no-ansi', ...$args],
cwd: $cwd ?? __DIR__ . '/..',
cwd: $cwd ? str_replace('{{ base }}', __DIR__ . '/..', $cwd) : __DIR__ . '/..' ,
env: [
'COLUMNS' => 120,
...$extraEnv,
Expand Down

0 comments on commit d3e3de0

Please sign in to comment.