-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly detect namespaced class in docblock
- Loading branch information
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/Integration/Mapping/Namespace/NamespacedInterfaceInferringTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping\Namespace; | ||
|
||
use CuyZ\Valinor\Tests\Integration\IntegrationTest; | ||
use SimpleNamespace\ImplementationOne; | ||
|
||
final class NamespacedInterfaceInferringTest extends IntegrationTest | ||
{ | ||
// @see https://github.com/CuyZ/Valinor/issues/394#issuecomment-1746722996 | ||
public function test_interface_inferred_from_same_namespace_as_file_runs_correctly(): void | ||
{ | ||
// This file is excluded from the test so that it doesn't get autoloaded | ||
// by the test suite; this allows testing a specific scenario where the | ||
// mapper infers an interface from the same namespace as the file it is | ||
// called from. | ||
$result = require_once 'mapper-inferring-interface-from-non-class.php'; | ||
|
||
self::assertInstanceOf(ImplementationOne::class, $result); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...Integration/Mapping/Namespace/RegisteredStaticConstructorWithReturnTypeInDocBlockTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping\Namespace; | ||
|
||
use CuyZ\Valinor\MapperBuilder; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTest; | ||
|
||
final class RegisteredStaticConstructorWithReturnTypeInDocBlockTest extends IntegrationTest | ||
{ | ||
// @see https://github.com/CuyZ/Valinor/issues/461 | ||
public function test_registered_static_constructor_with_return_type_in_doc_block_works_properly(): void | ||
{ | ||
$result = (new MapperBuilder()) | ||
->registerConstructor( | ||
// PHP8.1 first-class callable syntax | ||
[SomeClassWithStaticConstructor::class, 'staticConstructorWithReturnTypeInDocBlock'], | ||
) | ||
->mapper() | ||
->map(SomeClassWithStaticConstructor::class, 'foo'); | ||
|
||
self::assertSame('foo', $result->value); | ||
} | ||
} | ||
|
||
interface SomeSimpleInterface {} | ||
|
||
final class SomeClassWithStaticConstructor implements SomeSimpleInterface | ||
{ | ||
public function __construct(public string $value) {} | ||
|
||
/** | ||
* @return SomeSimpleInterface | ||
*/ | ||
public static function staticConstructorWithReturnTypeInDocBlock(string $value) | ||
{ | ||
return new self($value); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Integration/Mapping/Namespace/mapper-inferring-interface-from-non-class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace SimpleNamespace; | ||
|
||
use CuyZ\Valinor\MapperBuilder; | ||
|
||
require_once __DIR__ . '/../../../../vendor/autoload.php'; | ||
|
||
interface SomeInterface {} | ||
|
||
final class ImplementationOne implements SomeInterface {} | ||
|
||
final class ImplementationTwo implements SomeInterface {} | ||
|
||
return (new MapperBuilder()) | ||
->infer( | ||
SomeInterface::class, | ||
/** @return class-string<ImplementationOne|ImplementationTwo> */ | ||
static fn (string $type): string => match ($type) { | ||
'one' => ImplementationOne::class, | ||
'two' => ImplementationTwo::class, | ||
default => throw new \RuntimeException(), | ||
}, | ||
) | ||
->mapper() | ||
->map( | ||
SomeInterface::class, | ||
['type' => 'one'], | ||
); |