|
| 1 | +<?php |
| 2 | +declare( strict_types = 1 ); |
| 3 | + |
| 4 | +namespace TheWebSolver\Codegarage\Integration; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Nette\PhpGenerator\PhpNamespace; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use TheWebSolver\Codegarage\Generator\Traits\ImportResolver; |
| 10 | + |
| 11 | +class ImportResolverTest extends TestCase { |
| 12 | + #[Test] |
| 13 | + public function itEnsuresSetterGetterWorks(): void { |
| 14 | + $import = new class() { |
| 15 | + use ImportResolver; |
| 16 | + }; |
| 17 | + |
| 18 | + $import->setNamespace( $namespace = new PhpNamespace( '' ) ); |
| 19 | + |
| 20 | + $this->assertSame( $namespace, $import->getNamespace() ); |
| 21 | + } |
| 22 | + |
| 23 | + #[Test] |
| 24 | + public function itEnsuresAliasesAreCreatedWhenImporting(): void { |
| 25 | + $import = new class() { |
| 26 | + use ImportResolver; |
| 27 | + }; |
| 28 | + |
| 29 | + $import->setNamespace( new PhpNamespace( '' ) ); |
| 30 | + |
| 31 | + $import->addUseStatementOf( ImportResolver::class ); |
| 32 | + $import->addUseStatementOf( TestCase::class ); |
| 33 | + |
| 34 | + $this->assertCount( 2, $imports = $import->getNamespace()->getUses() ); |
| 35 | + |
| 36 | + foreach ( array( ImportResolver::class, TestCase::class ) as $item ) { |
| 37 | + $this->assertContains( $item, $imports ); |
| 38 | + } |
| 39 | + |
| 40 | + $import->addUseStatementOf( ImportResolver::class ); |
| 41 | + |
| 42 | + $this->assertCount( 2, $import->getNamespace()->getUses(), 'Must not add same import again.' ); |
| 43 | + |
| 44 | + $import->addUseStatementOf( 'DifferentNamespaced\\' . ImportResolver::class ); |
| 45 | + |
| 46 | + $this->assertCount( 3, $uses = $import->getNamespace()->getUses() ); |
| 47 | + $this->assertContains( 'DifferentNamespaced\\' . ImportResolver::class, $uses ); |
| 48 | + $this->assertArrayHasKey( |
| 49 | + 'TraitsImportResolver', |
| 50 | + $uses, |
| 51 | + 'Must alias if already imported same classname that exists in another namespace.' |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
0 commit comments