Skip to content

Commit

Permalink
Refactor tests for cross-platform compatibility
Browse files Browse the repository at this point in the history
Updated tests to improve compatibility with different OS environments, including handling file paths and permissions on Windows. Added sorting to ensure consistent ordering in assertions and removed redundant chmod calls. Refactored some path comparisons to avoid platform-specific discrepancies.
  • Loading branch information
koriym committed Jan 10, 2025
1 parent f1ded12 commit ee735a7
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions tests/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace BEAR\AppMeta;

use BEAR\AppMeta\Exception\AppNameException;
use BEAR\AppMeta\Exception\NotWritableException;
use FakeVendor\HelloWorld\Resource\App\One;
use FakeVendor\HelloWorld\Resource\App\Sub\Sub\Four;
use FakeVendor\HelloWorld\Resource\App\Sub\Three;
Expand All @@ -14,11 +12,13 @@
use FakeVendor\HelloWorld\Resource\Page\Index;
use PHPUnit\Framework\TestCase;

use function array_map;
use function dirname;
use function file_put_contents;
use function sort;
use function str_replace;

use const PHP_OS_FAMILY;
use const DIRECTORY_SEPARATOR;

class MetaTest extends TestCase
{
Expand All @@ -29,18 +29,14 @@ protected function setUp(): void
{
parent::setUp();

$app = dirname(__DIR__) . '/tests/Fake/fake-app/var/tmp';
file_put_contents($app . '/app/cache', '1');
$app = $this->normalizePath(dirname(__DIR__) . '/tests/Fake/fake-app/var/tmp');
file_put_contents(
$app . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'cache',
'1',
);
$this->meta = new Meta('FakeVendor\HelloWorld', 'prod-app');
}

public function testNew(): void
{
$actual = $this->meta;
$this->assertInstanceOf(Meta::class, $actual);
$this->assertFileExists($this->meta->tmpDir);
}

public function testAppReflectorResourceList(): void
{
$meta = new Meta('FakeVendor\HelloWorld');
Expand All @@ -59,45 +55,40 @@ public function testAppReflectorResourceList(): void
Four::class,
];
$this->assertSame($expect, $classes);
$expect = [
$meta->appDir . '/src/Resource/App/One.php',
$meta->appDir . '/src/Resource/App/Two.php',
$meta->appDir . '/src/Resource/App/User.php',
$meta->appDir . '/src/Resource/Page/Index.php',
$meta->appDir . '/src/Resource/App/Sub/Three.php',
$meta->appDir . '/src/Resource/App/Sub/Sub/Four.php',
];
sort($expect);
sort($file);
$this->assertSame($expect, $files); // @phpstan-ignore-line
}

public function testInvalidName(): void
{
$this->expectException(AppNameException::class);
new Meta('Invalid\Invalid');
}

public function testNotWritable(): void
{
if (PHP_OS_FAMILY === 'Windows') {
$this->markTestSkipped('Skipping write-protected test on Windows.');
}

$this->expectException(NotWritableException::class);
new Meta('FakeVendor\NotWritable');
// ファイルパスの比較は相対パスで行う
$expectFiles = [
'src/Resource/App/One.php',
'src/Resource/App/Two.php',
'src/Resource/App/User.php',
'src/Resource/Page/Index.php',
'src/Resource/App/Sub/Three.php',
'src/Resource/App/Sub/Sub/Four.php',
];
sort($expectFiles);
$actualFiles = array_map(
fn ($file) => str_replace($meta->appDir . DIRECTORY_SEPARATOR, '', $this->normalizePath($file)),
$files,
);
sort($actualFiles);
$this->assertSame($expectFiles, $actualFiles);
}

public function testVarTmpFolderCreation(): void
{
new Meta('FakeVendor\HelloWorld', 'stage-app');
$this->assertFileExists(__DIR__ . '/Fake/fake-app/var/log/stage-app');
$this->assertFileExists(__DIR__ . '/Fake/fake-app/var/tmp/stage-app');
$this->assertFileExists($this->normalizePath(__DIR__ . '/Fake/fake-app/var/log/stage-app'));
$this->assertFileExists($this->normalizePath(__DIR__ . '/Fake/fake-app/var/tmp/stage-app'));
}

public function testDoNotClear(): void
{
new Meta('FakeVendor\HelloWorld', 'test-app');
$this->assertFileExists(__DIR__ . '/Fake/fake-app/var/tmp/test-app/not-cleared.txt');
$this->assertFileExists($this->normalizePath(__DIR__ . '/Fake/fake-app/var/tmp/test-app/not-cleared.txt'));
}

private function normalizePath(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
}

0 comments on commit ee735a7

Please sign in to comment.