Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more tests #317

Merged
merged 4 commits into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions bin/generate-tests.php
Expand Up @@ -5,11 +5,12 @@

use Castor\Console\ApplicationFactory;
use Castor\PlatformUtil;
use Castor\Tests\Helper\OutputCleaner;
use Castor\Tests\Helper\WebServerHelper;
use Castor\Tests\OutputCleaner;
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 @@ -60,7 +61,6 @@
'castor:watcher:linux',
'castor:watcher:windows',
// Customized tests
'cd:directory',
'fingerprint:task-with-a-fingerprint-and-force',
'fingerprint:task-with-a-fingerprint',
'fingerprint:task-with-complete-fingerprint-check',
Expand Down Expand Up @@ -107,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 @@ -129,7 +138,7 @@ function add_test(array $args, string $class, ?string $cwd = null)

$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
@@ -1,21 +1,20 @@
<?php

namespace Castor\Tests\Examples;
namespace Castor\Tests\Examples\Generated;

use Castor\Tests\OutputCleaner;
use Castor\Tests\TaskTestCase;

class CdDirectoryTest extends TaskTestCase
{
// cd:directory
public function testCdDirectory(): void
public function test(): void
{
$process = $this->runTask(['cd:directory']);

$this->assertSame(0, $process->getExitCode());
$output = OutputCleaner::cleanOutput($process->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $output);
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
if (file_exists(__FILE__ . '.err.txt')) {
$this->assertStringEqualsFile(__FILE__ . '.err.txt', OutputCleaner::cleanOutput($process->getErrorOutput()));
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
} else {
$this->assertSame('', $process->getErrorOutput());
}
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();
}
4 changes: 2 additions & 2 deletions tests/OutputCleaner.php → tests/Helper/OutputCleaner.php
@@ -1,6 +1,6 @@
<?php

namespace Castor\Tests;
namespace Castor\Tests\Helper;

final class OutputCleaner
{
Expand All @@ -16,6 +16,6 @@ public static function cleanOutput(string $string): string
$string = preg_replace('{you are using v\d+.\d+.\d+.}m', 'you are using vX.Y.Z.', $string);
$string = preg_replace('{you are using v\d+.\d+.\d+.}m', 'you are using vX.Y.Z.', $string);

return str_replace(\dirname(__DIR__, 1), '...', $string);
return str_replace(\dirname(__DIR__, 2), '...', $string);
}
}
@@ -1,6 +1,6 @@
<?php

namespace Castor\Tests;
namespace Castor\Tests\Slow;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace Castor\Tests;
namespace Castor\Tests\Slow;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
Expand Down
3 changes: 2 additions & 1 deletion tests/TaskTestCase.php
Expand Up @@ -2,6 +2,7 @@

namespace Castor\Tests;

use Castor\Tests\Helper\OutputCleaner;
use Castor\Tests\Helper\WebServerHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -35,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