|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace mglaman\PHPStanDrupal\Tests\Type; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\EntityTypeManager; |
| 6 | +use mglaman\PHPStanDrupal\Drupal\ServiceMap; |
| 7 | +use mglaman\PHPStanDrupal\Drupal\ServiceMapStaticAccessor; |
| 8 | +use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait; |
| 9 | +use PHPStan\Testing\PHPStanTestCase; |
| 10 | +use PHPStan\TrinaryLogic; |
| 11 | +use PHPStan\Type\Constant\ConstantStringType; |
| 12 | +use PHPStan\Type\StringType; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\VerbosityLevel; |
| 15 | + |
| 16 | +final class ServiceIdTypeTest extends PHPStanTestCase { |
| 17 | + |
| 18 | + use AdditionalConfigFilesTrait; |
| 19 | + |
| 20 | + public function dataAccepts(): iterable |
| 21 | + { |
| 22 | + yield 'self' => [ |
| 23 | + new \mglaman\PHPStanDrupal\Type\ServiceIdType(), |
| 24 | + new \mglaman\PHPStanDrupal\Type\ServiceIdType(), |
| 25 | + TrinaryLogic::createYes(), |
| 26 | + ]; |
| 27 | + yield 'valid service' => [ |
| 28 | + new \mglaman\PHPStanDrupal\Type\ServiceIdType(), |
| 29 | + new ConstantStringType('entity_type.manager'), |
| 30 | + TrinaryLogic::createYes(), |
| 31 | + ]; |
| 32 | + yield 'invalid service' => [ |
| 33 | + new \mglaman\PHPStanDrupal\Type\ServiceIdType(), |
| 34 | + new ConstantStringType('foo.manager'), |
| 35 | + TrinaryLogic::createMaybe(), |
| 36 | + ]; |
| 37 | + yield 'generic string' => [ |
| 38 | + new \mglaman\PHPStanDrupal\Type\ServiceIdType(), |
| 39 | + new StringType(), |
| 40 | + TrinaryLogic::createMaybe(), |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dataProvider dataAccepts |
| 46 | + */ |
| 47 | + public function testAccepts(\mglaman\PHPStanDrupal\Type\ServiceIdType $type, Type $otherType, TrinaryLogic $expectedResult): void |
| 48 | + { |
| 49 | + $serviceMap = new ServiceMap(); |
| 50 | + $serviceMap->setDrupalServices([ |
| 51 | + 'entity_type.manager' => [ |
| 52 | + 'class' => EntityTypeManager::class |
| 53 | + ], |
| 54 | + ]); |
| 55 | + ServiceMapStaticAccessor::registerInstance($serviceMap); |
| 56 | + $actualResult = $type->accepts($otherType, true); |
| 57 | + self::assertSame( |
| 58 | + $expectedResult->describe(), |
| 59 | + $actualResult->describe(), |
| 60 | + sprintf('%s -> accepts(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments