Skip to content

Commit

Permalink
Merge 2cbb09a into bf9a207
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepioupiou committed Jul 25, 2019
2 parents bf9a207 + 2cbb09a commit 286e693
Show file tree
Hide file tree
Showing 16 changed files with 564 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
tests/_output/
tests/.phpunit.result.cache
tests/clover.xml

tests/coverage\.xml
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
test:
vendor/bin/phpunit tests

test7.1:
/usr/bin/php7.1 vendor/bin/phpunit tests

coverage:
vendor/bin/phpunit --configuration tests/php-unit.xml --coverage-html tests/_output tests
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ You can define vars by creating the file `config/packages/dev/adlarge_fixtures_d
reloadCommands:
- php bin/console doctrine:fixtures:load
- ....
entities:
Product:
- id
- name
- category
Customer:
- firstname
- lastname

Then you can install assets :

Expand All @@ -51,6 +59,8 @@ Then you can install assets :

To add fixtures to your documentation you have to get the manager in your fixtures file :

### Adding fixtures manually

```php
class AppFixtures extends Fixture
{
Expand Down Expand Up @@ -98,6 +108,83 @@ class AppFixtures extends Fixture
}
```

### Adding fixtures with configuration and entity

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.

With the following configuration :

```yaml
adlarge_fixtures_documentation:
title: 'Your title'
reloadCommands:
- php bin/console doctrine:fixtures:load
entities:
Product:
- name
- category
Customer:
- firstname
- lastname
- email
```

You can use

```php
class AppFixtures extends Fixture
{
/**
* @var FixturesDocumentationManager
*/
private $documentationManager;

/**
* AppFixtures constructor.
*
* @param FixturesDocumentationManager $documentationManager
*/
public function __construct(FixturesDocumentationManager $documentationManager)
{
$this->documentationManager = $documentationManager;
}

/**
* @param ObjectManager $manager
*
* @throws DuplicateFixtureException
*/
public function load(ObjectManager $manager)
{
$doc = $this->documentationManager->getDocumentation();

$product = (new Product())
->setName("Product 1")
->setCategory("Category 1");

$doc->addFixtureEntity($product);

$product = (new Product())
->setName("Product 2")
->setCategory("Category 2");

$doc->addFixtureEntity($product);

$customer = (new Customer())
->setFirstname('John')
->setLastname('Doe')
->setEmail('john.doe@test.fr');

$doc->addFixtureEntity($customer);

$manager->flush();
}
}
```

Then to generate the doc you only have to run :

php bin/console doctrine:fixtures:load
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.8",
"doctrine/doctrine-fixtures-bundle": "^2.4|^3.0",
"ext-json": "*"
"ext-json": "*",
"symfony/property-access": "^4.3"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
Expand Down
129 changes: 127 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->children()
->scalarNode('title')
->defaultValue('Fixtures documentation')->end()
->defaultValue('Fixtures documentation')
->end()
->arrayNode('reloadCommands')
->scalarPrototype()->end()
->end()
->end();
->end()
->arrayNode('configEntities')
->arrayPrototype()
->scalarPrototype()->end()
->end()
->end()
->end();

return $treeBuilder;
}
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/FixturesDocumentationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function load(array $configs, ContainerBuilder $container): void
$config['reloadCommands']
);

$container->setParameter(
'adlarge_fixtures_documentation.configEntities',
$config['configEntities']
);

$loader = new Loader\YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
Expand Down
51 changes: 49 additions & 2 deletions src/Model/Documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
namespace Adlarge\FixturesDocumentationBundle\Model;

use Adlarge\FixturesDocumentationBundle\Exception\DuplicateFixtureException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use TypeError;
use ReflectionClass;
use ReflectionException;
use function array_key_exists;

class Documentation
{
Expand All @@ -15,15 +20,21 @@ class Documentation
* @var Section[]
*/
private $sections = [];

/**
* @var array
*/
private $configEntities;
/**
* Documentation constructor.
*
* @param array $configEntities
* @param string $jsonString
*
* @throws DuplicateFixtureException
*/
public function __construct(string $jsonString = null)
public function __construct(array $configEntities, string $jsonString = null)
{
$this->configEntities = $configEntities;
if ($jsonString) {
$this->init($jsonString);
}
Expand Down Expand Up @@ -74,6 +85,42 @@ 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 mixed $entity
*
* @return Documentation
*
* @throws DuplicateFixtureException
* @throws ReflectionException
*/
public function addFixtureEntity($entity): self
{
$className = (new ReflectionClass($entity))->getShortName();
if (array_key_exists($className, $this->configEntities)) {
$propertyAccessor = PropertyAccess::createPropertyAccessor();
/** @var array $properties */
$properties = $this->configEntities[$className];
$fixture = [];
foreach ($properties as $property) {
try {
$value = $propertyAccessor->getValue($entity, $property);
if (is_scalar($value)) {
$fixture[$property] = $value;
}
} catch (NoSuchPropertyException $exception) {
// ignore this exception silently
}
}

$this->addFixture($className, $fixture);
}
return $this;
}

/**
* Reset the documentation by removing all sections.
*
Expand Down
Loading

0 comments on commit 286e693

Please sign in to comment.