Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ protected function startPhpServer(string $host, int $port)
'public',
];

$descriptors = [
0 => STDIN,
1 => STDOUT,
2 => STDERR,
];
$descriptors = $this->serverProcessDescriptors();

$process = proc_open($cmd, $descriptors, $pipes);

Expand All @@ -195,6 +191,19 @@ protected function startPhpServer(string $host, int $port)
return $process;
}

/**
* Descriptors used to start the PHP built-in server process.
* @return array<int, resource|array<int, string>|mixed>
*/
protected function serverProcessDescriptors(): array
{
return [
0 => STDIN,
1 => STDOUT,
2 => STDERR,
];
}

/**
* Wait until the PHP server is ready to accept connections.
* @param resource $process
Expand Down
33 changes: 32 additions & 1 deletion tests/Unit/Console/Commands/DebugBarCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Symfony\Component\Console\Tester\CommandTester;
use Quantum\Console\Commands\DebugBarCommand;
use Quantum\Storage\FileSystem;
use Quantum\Tests\Unit\AppTestCase;
use Quantum\Storage\FileSystem;
use ReflectionMethod;

class DebugBarCommandTest extends AppTestCase
{
Expand Down Expand Up @@ -50,4 +51,34 @@ public function testExecShowsErrorWhenAlreadyInstalled(): void
@rmdir($assetsPath);
@rmdir(assets_dir() . DS . 'DebugBar');
}

public function testCopyResourcesRecursivelyCopiesFiles(): void
{
$sourceDir = base_dir() . DS . 'var' . DS . 'tmp_debugbar_src_' . uniqid();
$targetDir = base_dir() . DS . 'var' . DS . 'tmp_debugbar_dst_' . uniqid();
$nestedSourceDir = $sourceDir . DS . 'nested';

mkdir($nestedSourceDir, 0777, true);
mkdir($targetDir, 0777, true);
file_put_contents($sourceDir . DS . 'root.css', 'root');
file_put_contents($nestedSourceDir . DS . 'nested.js', 'nested');

$this->setPrivateProperty($this->command, 'publicDebugBarFolderPath', $targetDir);

$method = new ReflectionMethod($this->command, 'copyResources');
$method->setAccessible(true);
$method->invoke($this->command, $sourceDir, $targetDir);

$this->assertFileExists($targetDir . DS . 'root.css');
$this->assertFileExists($targetDir . DS . 'nested' . DS . 'nested.js');

@unlink($targetDir . DS . 'nested' . DS . 'nested.js');
@unlink($targetDir . DS . 'root.css');
@rmdir($targetDir . DS . 'nested');
@rmdir($targetDir);
@unlink($nestedSourceDir . DS . 'nested.js');
@unlink($sourceDir . DS . 'root.css');
@rmdir($nestedSourceDir);
@rmdir($sourceDir);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Console/Commands/MigrationMigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Quantum\Tests\Unit\Console\Commands;

use Quantum\Console\Commands\MigrationMigrateCommand;
use Quantum\Migration\Enums\ExceptionMessages;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Helper\HelperSet;
Expand Down Expand Up @@ -48,4 +49,17 @@ public function testExecCancelsDownDirectionWhenNotConfirmed(): void
$output = $tester->getDisplay();
$this->assertStringContainsString('Operation was canceled!', $output);
}

public function testExecWithInvalidDirectionShowsMigrationMessage(): void
{
$tester = new CommandTester($this->command);
$tester->execute([
'direction' => 'invalid',
]);

$this->assertStringContainsString(
ExceptionMessages::WRONG_MIGRATION_DIRECTION,
$tester->getDisplay()
);
}
Comment thread
armanist marked this conversation as resolved.
}
83 changes: 83 additions & 0 deletions tests/Unit/Console/Commands/OpenApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Quantum\Console\Commands\OpenApiCommand;
use Quantum\Tests\Unit\AppTestCase;
use Quantum\Storage\FileSystem;
use ReflectionMethod;

class OpenApiCommandTest extends AppTestCase
{
Expand Down Expand Up @@ -78,4 +79,86 @@ public function testExecShowsErrorWhenModuleIsMissing(): void
}
}
}

public function testOpenapiRoutesContainsModuleSpecPath(): void
{
$method = new ReflectionMethod($this->command, 'openapiRoutes');
$method->setAccessible(true);

$routes = $method->invoke($this->command, 'Blog');

$this->assertStringContainsString('"openapi"', $routes);
$this->assertStringContainsString('Blog', $routes);
$this->assertTrue(
strpos($routes, 'resources/openapi/spec.json') !== false
|| strpos($routes, 'resources\\openapi\\spec.json') !== false
);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public function testCopyResourcesSkipsExcludedFiles(): void
{
$sourceDir = base_dir() . DS . 'var' . DS . 'tmp_openapi_src_' . uniqid();
$targetDir = base_dir() . DS . 'var' . DS . 'tmp_openapi_dst_' . uniqid();

mkdir($sourceDir, 0777, true);
mkdir($targetDir, 0777, true);

file_put_contents($sourceDir . DS . 'swagger-ui.css', 'body{}');
file_put_contents($sourceDir . DS . 'index.html', '<html></html>');

$this->setPrivateProperty($this->command, 'vendorOpenApiFolderPath', $sourceDir);
$this->setPrivateProperty($this->command, 'publicOpenApiFolderPath', $targetDir);

$method = new ReflectionMethod($this->command, 'copyResources');
$method->setAccessible(true);
$method->invoke($this->command);

$this->assertFileExists($targetDir . DS . 'swagger-ui.css');
$this->assertFileDoesNotExist($targetDir . DS . 'index.html');

@unlink($targetDir . DS . 'swagger-ui.css');
@rmdir($targetDir);
@unlink($sourceDir . DS . 'swagger-ui.css');
@unlink($sourceDir . DS . 'index.html');
@rmdir($sourceDir);
}

public function testGenerateOpenapiSpecificationCreatesSpecFile(): void
{
$command = new class () extends OpenApiCommand {
public function info(string $message): void
{
}

public function error(string $message): void
{
}
};

$moduleName = 'OpenApiSpec' . uniqid();
$annotationDir = modules_dir() . DS . $moduleName . DS . 'Controllers' . DS . 'OpenApi';
$specDir = modules_dir() . DS . $moduleName . DS . 'resources' . DS . 'openapi';
$specPath = $specDir . DS . 'spec.json';

mkdir($annotationDir, 0777, true);
mkdir($specDir, 0777, true);
file_put_contents($annotationDir . DS . 'SpecController.php', "<?php\n/**\n * @OA\\Info(title=\"Spec\", version=\"1.0.0\")\n */\nclass SpecController {}\n");

try {
$method = new ReflectionMethod($command, 'generateOpenapiSpecification');
$method->setAccessible(true);
$method->invoke($command, $moduleName);

$this->assertFileExists($specPath);
$this->assertNotSame('', trim((string) file_get_contents($specPath)));
} finally {
@unlink($annotationDir . DS . 'SpecController.php');
@rmdir($annotationDir);
@unlink($specPath);
@rmdir($specDir);
@rmdir(modules_dir() . DS . $moduleName . DS . 'Controllers');
@rmdir(modules_dir() . DS . $moduleName . DS . 'resources');
@rmdir(modules_dir() . DS . $moduleName);
}
}
Comment thread
armanist marked this conversation as resolved.
}
71 changes: 70 additions & 1 deletion tests/Unit/Console/Commands/ResourceCacheClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Quantum\Console\Commands\ResourceCacheClearCommand;
use Symfony\Component\Console\Tester\CommandTester;
use Quantum\Storage\FileSystem;
use Quantum\Tests\Unit\AppTestCase;
use Quantum\Storage\FileSystem;
use ReflectionMethod;

class ResourceCacheClearCommandTest extends AppTestCase
{
Expand Down Expand Up @@ -59,4 +60,72 @@ public function testExecShowsErrorWhenNoOptionsProvided(): void

@rmdir($cacheDir);
}

public function testInitTypeAcceptsKnownType(): void
{
$method = new ReflectionMethod($this->command, 'initType');
$method->setAccessible(true);
$method->invoke($this->command, 'views');

$this->assertSame('views', $this->getPrivateProperty($this->command, 'type'));
}

public function testInitTypeThrowsForInvalidType(): void
{
$method = new ReflectionMethod($this->command, 'initType');
$method->setAccessible(true);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cache type {invalid} is invalid.');
$method->invoke($this->command, 'invalid');
}

public function testInitModuleAcceptsKnownModule(): void
{
$this->setPrivateProperty($this->command, 'modules', ['blog', 'shop']);

$method = new ReflectionMethod($this->command, 'initModule');
$method->setAccessible(true);
$method->invoke($this->command, 'BLOG');

$this->assertSame('blog', $this->getPrivateProperty($this->command, 'module'));
}

public function testClearResourceModuleAndTypeRemovesMatchedFiles(): void
{
config()->set('view_cache', ['cache_dir' => 'cache']);

$cacheDir = base_dir() . DS . 'var' . DS . 'tmp_cache_' . uniqid();
$moduleDir = $cacheDir . DS . 'views' . DS . 'blog';
mkdir($moduleDir, 0777, true);
file_put_contents($moduleDir . DS . 'view.php', 'cached');

$this->setPrivateProperty($this->command, 'cacheDir', $cacheDir);

$method = new ReflectionMethod($this->command, 'clearResourceModuleAndType');
$method->setAccessible(true);
$method->invoke($this->command, 'blog', 'views');

$this->assertFileDoesNotExist($moduleDir . DS . 'view.php');

@rmdir($moduleDir);
@rmdir($cacheDir . DS . 'views');
@rmdir($cacheDir);
}

public function testExecClearsAllCacheWhenAllOptionProvided(): void
{
config()->set('view_cache', ['cache_dir' => 'tmp_cache_' . uniqid()]);

$cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir');
mkdir($cacheDir, 0777, true);
file_put_contents($cacheDir . DS . 'a.cache', 'value');

$this->tester->execute(['--all' => true]);

$this->assertFileDoesNotExist($cacheDir . DS . 'a.cache');
$this->assertStringContainsString('Resource cache cleared successfully.', $this->tester->getDisplay());

@rmdir($cacheDir);
}
}
Loading
Loading