Skip to content

Commit

Permalink
Merge 931bbbe into c3a6958
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepioupiou committed Jul 28, 2019
2 parents c3a6958 + 931bbbe commit 612f61d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -112,8 +112,9 @@ class AppFixtures extends Fixture

If you provided the good entity name and properties in configuration `entities` you can
use the method `addFixtureEntity`.
It only parse scalar properties and can check public properties as well as private ones with a getter (property, getProperty(), hasProperty(), isProperty()).
It will ignore non scalar properties as well as non existing ones.
It will parse scalar properties and can check public properties as well as private ones with a getter (property, getProperty(), hasProperty(), isProperty()).
It will parse non scalar properties as well, if it's an array it will display the count, if it's an entity it will display the result of __toString if it exists.
It will ignore non existing properties.

With the following configuration :

Expand All @@ -126,6 +127,7 @@ With the following configuration :
Product:
- name
- category
- owner
Customer:
- firstname
- lastname
Expand Down Expand Up @@ -185,6 +187,8 @@ class AppFixtures extends Fixture
}
```

NB: if the property owner is not scalar it will use the method __toString() if it exists

Then to generate the doc you only have to run :

php bin/console doctrine:fixtures:load
Expand Down
9 changes: 8 additions & 1 deletion src/Model/Documentation.php
Expand Up @@ -5,6 +5,7 @@
namespace Adlarge\FixturesDocumentationBundle\Model;

use Adlarge\FixturesDocumentationBundle\Exception\DuplicateFixtureException;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use TypeError;
Expand Down Expand Up @@ -110,7 +111,13 @@ public function addFixtureEntity($entity): self
$value = $propertyAccessor->getValue($entity, $property);
if (is_scalar($value)) {
$fixture[$property] = $value;
}
} else if (is_array($value)) {
$fixture[$property] = count($value);
} else {
if (method_exists($value, '__toString')) {
$fixture[$property] = $value->__toString();
}
}
} catch (NoSuchPropertyException $exception) {
// ignore this exception silently
}
Expand Down
30 changes: 17 additions & 13 deletions tests/Model/DocumentationTest.php
Expand Up @@ -24,6 +24,9 @@ public function setUp(): void
$this->root = vfsStream::setup();
}

/**
* @throws DuplicateFixtureException
*/
public function tearDown(): void
{
$documentation = new Documentation([]);
Expand Down Expand Up @@ -81,9 +84,6 @@ public function testAddFixtureWithMultidimensionalArray(): void
$documentation->addFixture('fixtures', ['id' => 1, 'array' => ['name' => 'fixture1', 'color' => 'red']]);
}

/**
* @throws DuplicateFixtureException
*/
public function testAddFixtureEntity(): void
{
$mockDocumentation = Mockery::mock(
Expand Down Expand Up @@ -111,9 +111,6 @@ public function testAddFixtureEntity(): void
$this->assertCount(0, $mockDocumentation->getSections());
}

/**
* @throws DuplicateFixtureException
*/
public function testAddFixtureEntityWithPublicProperties(): void
{
$mockDocumentation = Mockery::mock(
Expand Down Expand Up @@ -141,30 +138,37 @@ public function testAddFixtureEntityWithPublicProperties(): void
$this->assertCount(0, $mockDocumentation->getSections());
}

/**
* @throws DuplicateFixtureException
*/
public function testAddFixtureEntityWithComplexProperties(): void
{
$mockDocumentation = Mockery::mock(
Documentation::class,
[
['ProductComplex' => ['name', 'category', 'tags']]
[
'ProductComplex' => ['name', 'category', 'tags', 'categories'],
'Category' => ['id', 'name']
]
]
)
->makePartial();

$mockDocumentation->shouldReceive('addFixture')
->once()
->with('ProductComplex', [
'name' => 'product 1'
'name' => 'product 1',
'category' => 'category name',
'tags' => 3
])
->andReturn($mockDocumentation);


$category = new Category();
$category->name = 'category name';
$category->visibility = true;


$product = (new ProductComplex())
->setId(1)
->setName('product 1')
->setCategory(new Category())
->setCategory($category)
->setTags(['tag1', 'tag2', 'tag3']);

$mockDocumentation->addFixtureEntity($product);
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers/Model/Category.php
Expand Up @@ -4,5 +4,14 @@

class Category
{
public $id;

public $name;

public $visibility;

public function __toString(): string
{
return $this->name;
}
}

0 comments on commit 612f61d

Please sign in to comment.