|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\SchemaGenerator\Tests\ClassMutator; |
| 15 | + |
| 16 | +use ApiPlatform\SchemaGenerator\ClassMutator\ClassParentMutator; |
| 17 | +use ApiPlatform\SchemaGenerator\Model\Class_; |
| 18 | +use ApiPlatform\SchemaGenerator\Model\Use_; |
| 19 | +use ApiPlatform\SchemaGenerator\PhpTypeConverter; |
| 20 | +use ApiPlatform\SchemaGenerator\TypesGeneratorConfiguration; |
| 21 | +use EasyRdf\Graph as RdfGraph; |
| 22 | +use EasyRdf\Resource as RdfResource; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 25 | +use Prophecy\Prophecy\ObjectProphecy; |
| 26 | +use Psr\Log\LoggerInterface; |
| 27 | +use Symfony\Component\Config\Definition\Processor; |
| 28 | + |
| 29 | +class ClassParentMutatorTest extends TestCase |
| 30 | +{ |
| 31 | + use ProphecyTrait; |
| 32 | + |
| 33 | + private ObjectProphecy $loggerProphecy; |
| 34 | + private ClassParentMutator $classParentMutator; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->loggerProphecy = $this->prophesize(LoggerInterface::class); |
| 39 | + |
| 40 | + $configuration = new TypesGeneratorConfiguration(); |
| 41 | + /** @var Configuration $processedConfiguration */ |
| 42 | + $processedConfiguration = (new Processor())->processConfiguration($configuration, [[ |
| 43 | + 'types' => [ |
| 44 | + 'BlogPosting' => ['parent' => 'SocialMediaPosting'], |
| 45 | + 'SocialMediaPosting' => ['namespaces' => ['class' => 'socialMediaNamespace']], |
| 46 | + ], |
| 47 | + ]]); |
| 48 | + |
| 49 | + $this->classParentMutator = new ClassParentMutator($processedConfiguration, new PhpTypeConverter(), $this->loggerProphecy->reveal()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider provideInvokeTestCases |
| 54 | + */ |
| 55 | + public function testInvoke(Class_ $class, Class_ $expectedClass, ?string $loggerMessage = null): void |
| 56 | + { |
| 57 | + if ($loggerMessage) { |
| 58 | + $this->loggerProphecy->warning($loggerMessage)->shouldBeCalled(); |
| 59 | + } |
| 60 | + |
| 61 | + $this->assertEquals($expectedClass, ($this->classParentMutator)($class)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return \Generator<array{0: Class_, 1: Class_, 2?: string}> |
| 66 | + */ |
| 67 | + public function provideInvokeTestCases(): \Generator |
| 68 | + { |
| 69 | + $graph = new RdfGraph(); |
| 70 | + $product = new Class_('Product', new RdfResource('https://schema.org/Product', $graph)); |
| 71 | + yield 'no parent' => [clone $product, clone $product]; |
| 72 | + |
| 73 | + $graph = new RdfGraph(); |
| 74 | + $graph->addResource('https://schema.org/CreativeWork', 'rdfs:subClassOf', 'https://schema.org/Thing'); |
| 75 | + $creativeWork = new Class_('CreativeWork', new RdfResource('https://schema.org/CreativeWork', $graph)); |
| 76 | + yield 'with subclass' => [clone $creativeWork, (clone $creativeWork)->withParent('Thing')]; |
| 77 | + |
| 78 | + $graph = new RdfGraph(); |
| 79 | + $graph->addResource('https://schema.org/CreativeWork', 'rdfs:subClassOf', 'https://schema.org/Work'); |
| 80 | + $graph->addResource('https://schema.org/CreativeWork', 'rdfs:subClassOf', 'https://schema.org/Thing'); |
| 81 | + $creativeWork = new Class_('CreativeWork', new RdfResource('https://schema.org/CreativeWork', $graph)); |
| 82 | + yield 'with multiple subclasses' => [clone $creativeWork, (clone $creativeWork)->withParent('Work'), 'The type "https://schema.org/CreativeWork" has several supertypes. Using the first one.']; |
| 83 | + |
| 84 | + $graph = new RdfGraph(); |
| 85 | + $blogPosting = new Class_('BlogPosting', new RdfResource('https://schema.org/BlogPosting', $graph)); |
| 86 | + yield 'with parent' => [clone $blogPosting, (clone $blogPosting)->withParent('SocialMediaPosting')->addUse(new Use_('socialMediaNamespace\SocialMediaPosting'))]; |
| 87 | + } |
| 88 | +} |
0 commit comments