Skip to content

Commit

Permalink
Update doc and add purger loaders (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 2, 2016
1 parent 719476b commit 8508ad5
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 33 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ matrix:
allow_failures:
- php: nightly
- php: '7.0'
env:
- ELOQUENT_VERSION='~5.3.0@dev'
env: ELOQUENT_VERSION='~5.3.0@dev'
- php: '7.0'
env: SYMFONY_VERSION='~3.2.0@dev'

Expand Down
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ AliceDataFixtures

1. [Install](#installation)
1. [Symfony Bundle](#symfony)
1. [Doctrine ORM](#doctrine-orm)
1. [Eloquent ORM](#eloquent-orm)
1. [Basic usage](#basic-usage)
1. Advanced usage
1. [Processors](doc/processors.md)
Expand Down Expand Up @@ -40,14 +42,19 @@ composer require --dev theofidry/alice-persistence

composer require --dev theofidry/alice-persistence doctrine/orm doctrine/data-fixtures

# If you are using Eloquent ORM:
composer require --dev theofidry/alice-persistence illuminate/database
```

If you are working with Doctrine ORM, you need to install the following packages as well:


### Symfony

This library ships with a Symfony bundle. To use it with Doctrine do not forget to install `doctrine/doctrine-bundle`
This library ships with a Symfony bundle `FidryAliceDataFixturesBundle`.


#### Doctrine ORM

To use it with Doctrine do not forget to install `doctrine/doctrine-bundle`
and enable the `DoctrineBundle` (done by default in Symfony Standard Edition).

Then, enable the bundle by updating your `app/AppKernel.php` file to enable the bundle:
Expand All @@ -68,13 +75,33 @@ public function registerBundles()
}
```

#### Eloquent ORM

## Basic usage
To use it with Eloquent do not forget to install `illuminate/database` and
`WouterJEloquentBundle` (`wouterj/eloquent-bundle`).

Then, enable the bundle by updating your `app/AppKernel.php` file to enable the bundle:

Assuming you are using [Doctrine](http://www.doctrine-project.org/projects/orm.html), make sure you
have the [`doctrine/doctrine-bundle`](https://github.com/doctrine/DoctrineBundle) and [`doctrine/data-fixtures`](https://github.com/doctrine/data-fixtures) packages installed.
```php
<?php
// app/AppKernel.php

public function registerBundles()
{
//...
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
//...
$bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
}

return $bundles;
}
```


## Basic usage

Then create a fixture file in `src/AppBundle/Resources/fixtures`:
Create a fixture file in `src/AppBundle/Resources/fixtures`:

```yaml
# src/AppBundle/Resources/fixtures/dummy.yml
Expand All @@ -101,7 +128,8 @@ $files = [
'path/to/src/AppBundle/Resources/fixtures/related_dummy.yml',
];
$loader = $container->get('fidry_alice_data_fixtures.loader');
$loader = $container->get('fidry_alice_data_fixtures.doctrine.persister_loader'); // For Doctrine ORM
$loader = $container->get('fidry_alice_data_fixtures.eloquent.persister_loader'); // For Eloquent ORM
$objects = $loader->load($files);
// $objects is now an array of persisted `Dummy` and `RelatedDummy`
Expand Down
1 change: 0 additions & 1 deletion bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ vendor-bin/symfony/vendor/phpunit/phpunit/phpunit -c phpunit_symfony_doctrine.xm

log "Symfony with Eloquent"
mysql -u root -e "DROP DATABASE IF EXISTS fidry_alice_data_fixtures;"
mysql -u root -e "CREATE DATABASE fidry_alice_data_fixtures;"
rm -rf fixtures/Bridge/Symfony/cache/*
php bin/console eloquent:migrate:install -k=EloquentKernel

Expand Down
13 changes: 4 additions & 9 deletions doc/purge_data.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# Purge data

If you wish to purge the data of your database, all you need is an `Doctrine\ORM\EntityManagerInterface`!
If you wish to purge the data of your database, you can use a purger.

```php
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\OrmPurger;
use Fidry\AliceDataFixtures\Loader;

$manager = $container->get('doctrine')->getManager();
$loader = $container->get('fidry_alice_data_fixtures.loader');

$purger = new OrmPurger($manager);
$loader = new PurgerLoader($loader, $purger, $purger);
$loader = $container->get('fidry_alice_data_fixtures.doctrine.purger_loader');
// Or
$loader = $container->get('fidry_alice_data_fixtures.eloquent.purger_loader');

$loader->load([
'path/to/src/AppBundle/Resources/fixtures/dummy.yml',
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/FidryAliceDataFixturesBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass(
new RegisterTagServicesPass(
'fidry_alice_data_fixtures.loader.doctrine',
'fidry_alice_data_fixtures.doctrine.persister_loader',
'fidry_alice_data_fixtures.processor'
)
);
$container->addCompilerPass(
new RegisterTagServicesPass(
'fidry_alice_data_fixtures.loader.eloquent',
'fidry_alice_data_fixtures.eloquent.persister_loader',
'fidry_alice_data_fixtures.processor'
)
);
Expand Down
9 changes: 8 additions & 1 deletion src/Bridge/Symfony/Resources/config/doctrine_orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@
<argument type="service" id="doctrine.orm.default_entity_manager" />
</service>

<service id="fidry_alice_data_fixtures.loader.doctrine"
<service id="fidry_alice_data_fixtures.doctrine.persister_loader"
class="Fidry\AliceDataFixtures\Loader\PersisterLoader"
lazy="true" >
<argument type="service" id="fidry_alice_data_fixtures.loader.multipass_file" />
<argument type="service" id="fidry_alice_data_fixtures.persistence.persister.doctrine.object_manager_persister" />
<!-- Processors are injected via a Compiler pass -->
</service>

<service id="fidry_alice_data_fixtures.doctrine.purger_loader"
class="Fidry\AliceDataFixtures\Loader\PurgerLoader"
lazy="true" >
<argument type="service" id="fidry_alice_data_fixtures.doctrine.persister_loader" />
<argument type="service" id="fidry_alice_data_fixtures.persistence.purger.doctrine.orm_purger" />
</service>

</services>

</container>
9 changes: 8 additions & 1 deletion src/Bridge/Symfony/Resources/config/eloquent_orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@
<argument type="service" id="wouterj_eloquent.database_manager" />
</service>

<service id="fidry_alice_data_fixtures.loader.eloquent"
<service id="fidry_alice_data_fixtures.eloquent.persister_loader"
class="Fidry\AliceDataFixtures\Loader\PersisterLoader"
lazy="true" >
<argument type="service" id="fidry_alice_data_fixtures.loader.multipass_file" />
<argument type="service" id="fidry_alice_data_fixtures.persistence.persister.eloquent.model_persister" />
<!-- Processors are injected via a Compiler pass -->
</service>

<service id="fidry_alice_data_fixtures.eloquent.purger_loader"
class="Fidry\AliceDataFixtures\Loader\PurgerLoader"
lazy="true" >
<argument type="service" id="fidry_alice_data_fixtures.eloquent.persister_loader" />
<argument type="service" id="fidry_alice_data_fixtures.persistence.purger.eloquent.model_purger" />
</service>

</services>

</container>
1 change: 0 additions & 1 deletion src/Loader/PurgerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Fidry\AliceDataFixtures\LoaderInterface;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
use Fidry\AliceDataFixtures\Persistence\PurgerFactoryInterface;
use Fidry\AliceDataFixtures\Persistence\PurgerInterface;
use Nelmio\Alice\NotClonableTrait;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundleTest as NakedFidryAliceDataFixturesBundleTest;
use Fidry\AliceDataFixtures\Bridge\Symfony\SymfonyApp\DoctrineKernel;
use Fidry\AliceDataFixtures\Loader\PersisterLoader;
use Fidry\AliceDataFixtures\Loader\PurgerLoader;
use Symfony\Component\HttpKernel\KernelInterface;

/**
Expand Down Expand Up @@ -57,7 +58,12 @@ public function testServiceRegistration()

$this->assertInstanceOf(
PersisterLoader::class,
$this->kernel->getContainer()->get('fidry_alice_data_fixtures.loader.doctrine')
$this->kernel->getContainer()->get('fidry_alice_data_fixtures.doctrine.persister_loader')
);

$this->assertInstanceOf(
PurgerLoader::class,
$this->kernel->getContainer()->get('fidry_alice_data_fixtures.doctrine.purger_loader')
);
}
}
5 changes: 2 additions & 3 deletions tests/Bridge/Symfony/Doctrine/ORMLoaderIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function setUp()
$this->kernel = new DoctrineKernel('doctrine', true);
$this->kernel->boot();

$this->loader = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');
$this->loader = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.doctrine.persister_loader');
$this->doctrine = $this->kernel->getContainer()->get('doctrine');
}

Expand Down Expand Up @@ -87,8 +87,7 @@ public function testLoadAFileWithPurger()
// However in this context we unset ALL entities and it's for testing purpose
// Not a real application where deleting an application should be handled properly
$this->doctrine->getConnection()->exec('SET FOREIGN_KEY_CHECKS=0;');
$purger = new OrmPurger($dummyManager, PurgeMode::createDeleteMode());
$loader = new PurgerLoader($this->loader, $purger);
$loader = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.doctrine.purger_loader');
$loader->load([
__DIR__.'/../../../../fixtures/fixture_files/dummy.yml',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundleTest as NakedFidryAliceDataFixturesBundleTest;
use Fidry\AliceDataFixtures\Bridge\Symfony\SymfonyApp\EloquentKernel;
use Fidry\AliceDataFixtures\Loader\PersisterLoader;
use Fidry\AliceDataFixtures\Loader\PurgerLoader;
use Symfony\Component\HttpKernel\KernelInterface;

/**
Expand Down Expand Up @@ -57,7 +58,12 @@ public function testServiceRegistration()

$this->assertInstanceOf(
PersisterLoader::class,
$this->kernel->getContainer()->get('fidry_alice_data_fixtures.loader.eloquent')
$this->kernel->getContainer()->get('fidry_alice_data_fixtures.eloquent.persister_loader')
);

$this->assertInstanceOf(
PurgerLoader::class,
$this->kernel->getContainer()->get('fidry_alice_data_fixtures.eloquent.purger_loader')
);
}
}
5 changes: 2 additions & 3 deletions tests/Bridge/Symfony/Eloquent/ORMLoaderIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setUp()
$this->kernel->getContainer()->get('wouterj_eloquent')->setAsGlobal();
$this->databaseManager = $this->kernel->getContainer()->get('wouterj_eloquent.database_manager');

$this->loader = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.loader.eloquent');
$this->loader = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.eloquent.persister_loader');
$this->execute([
'command' => 'eloquent:migrate',
'--path' => 'migrations',
Expand Down Expand Up @@ -88,8 +88,7 @@ public function testLoadAFileWithPurger()
'address' => 'hello',
]);

$purger = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.persistence.purger.eloquent.model_purger');
$loader = new PurgerLoader($this->loader, $purger);
$loader = $this->kernel->getContainer()->get('fidry_alice_data_fixtures.eloquent.purger_loader');
// Disable foreign keys check
// This is usually a bad idea as you have to deal *how* your entities are deleted
// And doing that can lead to broken entities
Expand Down

0 comments on commit 8508ad5

Please sign in to comment.