Skip to content

Commit

Permalink
There is a namespace but the directory does not exist (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 28, 2024
1 parent 56d4069 commit f0dd286
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.2.1 under development

- Enh #274: Refactor for compatibility with `yiisoft/db` package (@Tigrov)
- Bug #277: Fix when there is a namespace but the directory does not exist (@Tigrov)

## 1.2.0 November 27, 2024

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@
"autoload-dev": {
"psr-4": {
"Yiisoft\\Db\\Migration\\Tests\\": "tests",
"Yiisoft\\Db\\Migration\\Tests\\Support\\": "tests/Support",
"Yiisoft\\Db\\Migration\\Tests\\NonExistsDirectory\\": "tests/non-exists-directory",
"Yiisoft\\Db\\Migration\\Tests\\ForTest\\": "tests/Support",
"Yiisoft\\Db\\Migration\\Tests\\Support\\": "tests/Support",
"Yiisoft\\Db\\Migration\\Tests\\Support\\MigrationsExtra\\": [
"tests/Support/MigrationsExtra",
"tests/Support/MigrationsExtra2"
Expand Down
4 changes: 4 additions & 0 deletions src/Service/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ private function getNamespacesFromPath(string $path): array

foreach ($map as $namespace => $directories) {
foreach ($directories as $directory) {
if (!is_dir($directory)) {
continue;
}

$directory = realpath($directory) . DIRECTORY_SEPARATOR;

if (str_starts_with($path, $directory)) {
Expand Down
14 changes: 12 additions & 2 deletions tests/Common/Service/AbstractMigrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,25 @@ public function testGetNamespacesFromPathForNoHavingNamespacePath(): void
$migrationService = $this->container->get(MigrationService::class);

$getNamespaceFromPath = new ReflectionMethod($migrationService, 'getNamespacesFromPath');
$getNamespaceFromPath->setAccessible(true);

// No having namespace path
$path = dirname(__DIR__, 3) . '/config';

$this->assertSame([], $getNamespaceFromPath->invoke($migrationService, $path));
}

public function testGetNamespacesFromPathForNoExistsDirectory(): void
{
$migrationService = $this->container->get(MigrationService::class);

$getNamespaceFromPath = new ReflectionMethod($migrationService, 'getNamespacesFromPath');

// There is a path to the namespace, but the directory does not exist
$path = dirname(__DIR__, 2) . '/non-exists-directory';

$this->assertSame([], $getNamespaceFromPath->invoke($migrationService, $path));
}

/**
* Test MigrationService::getNamespacesFromPath() returns namespaces corresponding to the longest subdirectory of a path.
* One path can match to several namespaces.
Expand All @@ -79,7 +90,6 @@ public function testGetNamespacesFromPathForLongestPath(): void
$migrationService = $this->container->get(MigrationService::class);

$getNamespaceFromPath = new ReflectionMethod($migrationService, 'getNamespacesFromPath');
$getNamespaceFromPath->setAccessible(true);

/**
* Path corresponding to three namespaces:
Expand Down

0 comments on commit f0dd286

Please sign in to comment.