Skip to content

Commit

Permalink
tests: fix failing tests and add new ones for this new method
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepioupiou committed Jul 24, 2019
1 parent 5f4aa5b commit 99dd98f
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -8,3 +8,5 @@
tests/_output/
tests/.phpunit.result.cache
tests/clover.xml

tests/coverage\.xml
17 changes: 15 additions & 2 deletions src/Model/Documentation.php
Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
115 changes: 105 additions & 10 deletions tests/Model/DocumentationTest.php
Expand Up @@ -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
{
Expand All @@ -21,7 +26,7 @@ public function setUp(): void

public function tearDown(): void
{
$documentation = new Documentation();
$documentation = new Documentation([]);
$documentation->reset();
}

Expand All @@ -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']);

Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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());
Expand All @@ -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']);
Expand All @@ -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());
}

Expand All @@ -124,7 +219,7 @@ public function testInitEmpty(): void
{
$jsonString = null;

$documentation = new Documentation($jsonString);
$documentation = new Documentation([], $jsonString);
$this->assertCount(0, $documentation->getSections());
}
}
18 changes: 12 additions & 6 deletions tests/Service/FixturesDocumentationManagerTest.php
Expand Up @@ -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());
Expand All @@ -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(
Expand All @@ -73,7 +75,8 @@ public function testSave(): void
vfsStream::newDirectory('var')->at($this->root);
$documentationManager = new FixturesDocumentationManager(
$this->root->url(),
['dummyCommand']
['dummyCommand'],
[]
);

$documentationManager->save();
Expand All @@ -93,7 +96,8 @@ public function testInitDocumentation(): void
);
$documentationManager = new FixturesDocumentationManager(
$this->root->url(),
['dummyCommand']
['dummyCommand'],
[]
);

$this->assertInstanceOf(Documentation::class, $documentationManager->getDocumentation());
Expand All @@ -108,7 +112,8 @@ public function testReloadWithUnknownCommand(): void

$documentationManager = new FixturesDocumentationManager(
$this->root->url(),
['unknowCommand']
['unknowCommand'],
[]
);

$documentationManager->reload();
Expand All @@ -131,7 +136,8 @@ public function testReload(): void

$documentationManager = new FixturesDocumentationManager(
$this->root->url(),
['workingCommand']
['workingCommand'],
[]
);

$this->assertSame(1, $documentationManager->reload());
Expand Down
8 changes: 8 additions & 0 deletions tests/helpers/Model/Category.php
@@ -0,0 +1,8 @@
<?php

namespace Adlarge\FixturesDocumentationBundle\helpers\Model;

class Category
{

}
46 changes: 46 additions & 0 deletions tests/helpers/Model/Product.php
@@ -0,0 +1,46 @@
<?php

namespace Adlarge\FixturesDocumentationBundle\helpers\Model;

class Product
{
private $id;

private $name;

private $category;

public function setName(string $name): self
{
$this->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;
}

}

0 comments on commit 99dd98f

Please sign in to comment.