diff --git a/.gitignore b/.gitignore index 7a72d69..492afb8 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ tests/_output/ tests/.phpunit.result.cache tests/clover.xml + +tests/coverage\.xml diff --git a/src/Model/Documentation.php b/src/Model/Documentation.php index 4f3ef03..4008012 100644 --- a/src/Model/Documentation.php +++ b/src/Model/Documentation.php @@ -81,6 +81,17 @@ public function addFixture(string $sectionTitle, array $fixture): self return $this; } + /** + * Add a fixture to the documentation when passing directly the entity. + * Use configEntites and their property to create the array of value + * to pass to addFixture method + * + * @param object $entity + * + * @return Documentation + * + * @throws DuplicateFixtureException + */ public function addFixtureEntity(object $entity): self { $className = (new \ReflectionClass($entity))->getShortName(); @@ -91,10 +102,12 @@ public function addFixtureEntity(object $entity): self $fixture = []; foreach ($properties as $property) { $value = $propertyAccessor->getValue($entity, $property); - $fixture[$property] = $value; + if (is_scalar($value)) { + $fixture[$property] = $value; + } } - $this->addFixture($className . 's', $fixture); + $this->addFixture($className, $fixture); } return $this; } diff --git a/tests/Model/DocumentationTest.php b/tests/Model/DocumentationTest.php index 5a94b42..641d17a 100644 --- a/tests/Model/DocumentationTest.php +++ b/tests/Model/DocumentationTest.php @@ -5,9 +5,14 @@ use org\bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; use Adlarge\FixturesDocumentationBundle\Model\Documentation; -use \Adlarge\FixturesDocumentationBundle\Exception\DuplicateFixtureException; +use Adlarge\FixturesDocumentationBundle\Exception\DuplicateFixtureException; use TypeError; use org\bovigo\vfs\vfsStream; +use Mockery; +use Adlarge\FixturesDocumentationBundle\helpers\Model\Product; +use Adlarge\FixturesDocumentationBundle\helpers\Model\ProductPublic; +use Adlarge\FixturesDocumentationBundle\helpers\Model\ProductComplex; +use Adlarge\FixturesDocumentationBundle\helpers\Model\Category; class DocumentationTest extends TestCase { @@ -21,7 +26,7 @@ public function setUp(): void public function tearDown(): void { - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->reset(); } @@ -30,7 +35,7 @@ public function tearDown(): void */ public function testAddFixture(): void { - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->addFixture('fixtures', ['id' => 1, 'name' => 'fixture1']); @@ -43,7 +48,7 @@ public function testAddFixture(): void */ public function testAddFixtureWithSameSection(): void { - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->addFixture('fixtures', ['id' => 1, 'name' => 'fixture1']); $documentation->addFixture('fixtures', ['id' => 2, 'name' => 'fixture2']); @@ -56,7 +61,7 @@ public function testAddFixtureWithSameSection(): void */ public function testAddFixtureWithDifferentSection(): void { - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->addFixture('fixtures', ['id' => 1, 'name' => 'fixture1']); $documentation->addFixture('other', ['id' => 1, 'name' => 'fixture1']); @@ -71,17 +76,107 @@ public function testAddFixtureWithMultidimensionalArray(): void { $this->expectException(TypeError::class); - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->addFixture('fixtures', ['id' => 1, 'array' => ['name' => 'fixture1', 'color' => 'red']]); } + /** + * @throws DuplicateFixtureException + */ + public function testAddFixtureEntity(): void + { + $mockDocumentation = Mockery::mock( + Documentation::class, + [ + ['Product' => ['name', 'category']] + ] + ) + ->makePartial(); + + $mockDocumentation->shouldReceive('addFixture') + ->once() + ->with('Product', [ + 'name' => 'product 1', + 'category' => 'category 1' + ]) + ->andReturn($mockDocumentation); + + $product = (new Product()) + ->setId(1) + ->setName('product 1') + ->setCategory('category 1'); + + $mockDocumentation->addFixtureEntity($product); + $this->assertCount(0, $mockDocumentation->getSections()); + } + + /** + * @throws DuplicateFixtureException + */ + public function testAddFixtureEntityWithPublicProperties(): void + { + $mockDocumentation = Mockery::mock( + Documentation::class, + [ + ['ProductPublic' => ['name', 'category']] + ] + ) + ->makePartial(); + + $mockDocumentation->shouldReceive('addFixture') + ->once() + ->with('ProductPublic', [ + 'name' => 'product 1', + 'category' => 'category 1' + ]) + ->andReturn($mockDocumentation); + + $product = new ProductPublic(); + $product->id = 1; + $product->name = 'product 1'; + $product->category = 'category 1'; + + $mockDocumentation->addFixtureEntity($product); + $this->assertCount(0, $mockDocumentation->getSections()); + } + + /** + * @throws DuplicateFixtureException + */ + public function testAddFixtureEntityWithComplexProperties(): void + { + $mockDocumentation = Mockery::mock( + Documentation::class, + [ + ['ProductComplex' => ['name', 'category', 'tags']] + ] + ) + ->makePartial(); + + $mockDocumentation->shouldReceive('addFixture') + ->once() + ->with('ProductComplex', [ + 'name' => 'product 1' + ]) + ->andReturn($mockDocumentation); + + $product = (new ProductComplex()) + ->setId(1) + ->setName('product 1') + ->setCategory(new Category()) + ->setTags(['tag1', 'tag2', 'tag3']); + + $mockDocumentation->addFixtureEntity($product); + $this->assertCount(0, $mockDocumentation->getSections()); + } + /** * @throws DuplicateFixtureException */ public function testReset(): void { - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->addFixture('fixtures', ['id' => 1, 'name' => 'fixture1']); $this->assertCount(1, $documentation->getSections()); @@ -95,7 +190,7 @@ public function testReset(): void */ public function testToJson(): void { - $documentation = new Documentation(); + $documentation = new Documentation([]); $documentation->addFixture('some', ['id' => 1, 'name' => 'fixture1']); $documentation->addFixture('some', ['id' => 2, 'name' => 'fixture2']); @@ -113,7 +208,7 @@ public function testInit(): void { $jsonString = '{"some":{"fixtures":[{"id":1,"name":"fixture1"},{"id":2,"name":"fixture2"}]},"others":{"fixtures":[{"id":1,"pseudo":"autre2"}]}}'; - $documentation = new Documentation($jsonString); + $documentation = new Documentation([], $jsonString); $this->assertCount(2, $documentation->getSections()); } @@ -124,7 +219,7 @@ public function testInitEmpty(): void { $jsonString = null; - $documentation = new Documentation($jsonString); + $documentation = new Documentation([], $jsonString); $this->assertCount(0, $documentation->getSections()); } } diff --git a/tests/Service/FixturesDocumentationManagerTest.php b/tests/Service/FixturesDocumentationManagerTest.php index 50f87ff..95c3e91 100644 --- a/tests/Service/FixturesDocumentationManagerTest.php +++ b/tests/Service/FixturesDocumentationManagerTest.php @@ -39,7 +39,8 @@ public function testGetDocumentation(): void vfsStream::newDirectory('var')->at($this->root); $documentationManager = new FixturesDocumentationManager( $this->root->url(), - ['dummyCommand'] + ['dummyCommand'], + [] ); $this->assertInstanceOf(Documentation::class, $documentationManager->getDocumentation()); @@ -53,7 +54,8 @@ public function testReset(): void vfsStream::newDirectory('var')->at($this->root); $documentationManager = new FixturesDocumentationManager( $this->root->url(), - ['dummyCommand'] + ['dummyCommand'], + [] ); file_put_contents( @@ -73,7 +75,8 @@ public function testSave(): void vfsStream::newDirectory('var')->at($this->root); $documentationManager = new FixturesDocumentationManager( $this->root->url(), - ['dummyCommand'] + ['dummyCommand'], + [] ); $documentationManager->save(); @@ -93,7 +96,8 @@ public function testInitDocumentation(): void ); $documentationManager = new FixturesDocumentationManager( $this->root->url(), - ['dummyCommand'] + ['dummyCommand'], + [] ); $this->assertInstanceOf(Documentation::class, $documentationManager->getDocumentation()); @@ -108,7 +112,8 @@ public function testReloadWithUnknownCommand(): void $documentationManager = new FixturesDocumentationManager( $this->root->url(), - ['unknowCommand'] + ['unknowCommand'], + [] ); $documentationManager->reload(); @@ -131,7 +136,8 @@ public function testReload(): void $documentationManager = new FixturesDocumentationManager( $this->root->url(), - ['workingCommand'] + ['workingCommand'], + [] ); $this->assertSame(1, $documentationManager->reload()); diff --git a/tests/helpers/Model/Category.php b/tests/helpers/Model/Category.php new file mode 100644 index 0000000..0735d69 --- /dev/null +++ b/tests/helpers/Model/Category.php @@ -0,0 +1,8 @@ +name = $name; + return $this; + } + + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + public function setCategory(string $category): self + { + $this->category = $category; + return $this; + } + + public function getId(): int + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getCategory(): string + { + return $this->category; + } + +} \ No newline at end of file diff --git a/tests/helpers/Model/ProductComplex.php b/tests/helpers/Model/ProductComplex.php new file mode 100644 index 0000000..271d751 --- /dev/null +++ b/tests/helpers/Model/ProductComplex.php @@ -0,0 +1,60 @@ +name = $name; + return $this; + } + + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + public function setCategory(Category $category): self + { + $this->category = $category; + return $this; + } + + public function getId(): int + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getCategory(): Category + { + return $this->category; + } + + public function setTags(array $tags): self + { + $this->tags = $tags; + return $this; + } + + public function getTags(): array + { + return $this->tags; + } +} \ No newline at end of file diff --git a/tests/helpers/Model/ProductPublic.php b/tests/helpers/Model/ProductPublic.php new file mode 100644 index 0000000..88c06cf --- /dev/null +++ b/tests/helpers/Model/ProductPublic.php @@ -0,0 +1,12 @@ +