# HG changeset patch # User ymh # Date 1323901738 -3600 # Node ID 624e5900f5a4aa471265d93aeab8cc6f8b11d93c # Parent b36a42d260d8ccf19ce90e7699636e9daefe98e2 add tests and fixtures diff -r b36a42d260d8 -r 624e5900f5a4 .hgsubstate --- a/.hgsubstate Fri Dec 09 06:43:49 2011 +0100 +++ b/.hgsubstate Wed Dec 14 23:28:58 2011 +0100 @@ -1,1 +1,1 @@ -10be6b9e55e760b9f2dc4179990a098169612002 vendor/bundles/IRI/Bundle/WikiTagBundle +774ba82dca59276c8c402bb292db656f1c0ca96c vendor/bundles/IRI/Bundle/WikiTagBundle diff -r b36a42d260d8 -r 624e5900f5a4 app/AppKernel.php --- a/app/AppKernel.php Fri Dec 09 06:43:49 2011 +0100 +++ b/app/AppKernel.php Wed Dec 14 23:28:58 2011 +0100 @@ -22,6 +22,7 @@ new Company\UserBundle\CompanyUserBundle(), new IRI\Bundle\WikiTagBundle\WikiTagBundle(), new Symfony\Bundle\DoctrineMigrationsBundle\DoctrineMigrationsBundle(), + new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(), ); if (in_array($this->getEnvironment(), array('dev', 'test'))) { diff -r b36a42d260d8 -r 624e5900f5a4 app/DoctrineMigrations/Version20111209060357.php --- a/app/DoctrineMigrations/Version20111209060357.php Fri Dec 09 06:43:49 2011 +0100 +++ b/app/DoctrineMigrations/Version20111209060357.php Wed Dec 14 23:28:58 2011 +0100 @@ -15,17 +15,17 @@ // this up() migration is autogenerated, please modify it to your needs $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); - $this->addSql("ALTER TABLE wikitag_document ADD createdAt DATETIME NULL"); - $this->addSql("ALTER TABLE wikitag_document_tag ADD createdAt DATETIME NULL"); - $this->addSql("ALTER TABLE wikitag_tag ADD createdAt DATETIME NULL"); + $this->addSql("ALTER TABLE wikitag_document ADD created_at DATETIME NULL"); + $this->addSql("ALTER TABLE wikitag_document_tag ADD created_at DATETIME NULL"); + $this->addSql("ALTER TABLE wikitag_tag ADD created_at DATETIME NULL"); - $this->addSql("UPDATE wikitag_document SET createdAt = UTC_TIMESTAMP()"); - $this->addSql("UPDATE wikitag_document_tag SET createdAt = UTC_TIMESTAMP()"); - $this->addSql("UPDATE wikitag_tag SET createdAt = UTC_TIMESTAMP()"); + $this->addSql("UPDATE wikitag_document SET created_at = UTC_TIMESTAMP()"); + $this->addSql("UPDATE wikitag_document_tag SET created_at = UTC_TIMESTAMP()"); + $this->addSql("UPDATE wikitag_tag SET created_at = UTC_TIMESTAMP()"); - $this->addSql("ALTER TABLE wikitag_document MODIFY createdAt DATETIME NOT NULL"); - $this->addSql("ALTER TABLE wikitag_document_tag MODIFY createdAt DATETIME NOT NULL"); - $this->addSql("ALTER TABLE wikitag_tag MODIFY createdAt DATETIME NOT NULL"); + $this->addSql("ALTER TABLE wikitag_document MODIFY created_at DATETIME NOT NULL"); + $this->addSql("ALTER TABLE wikitag_document_tag MODIFY created_at DATETIME NOT NULL"); + $this->addSql("ALTER TABLE wikitag_tag MODIFY created_at DATETIME NOT NULL"); } @@ -34,8 +34,8 @@ // this down() migration is autogenerated, please modify it to your needs $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); - $this->addSql("ALTER TABLE wikitag_document DROP createdAt"); - $this->addSql("ALTER TABLE wikitag_document_tag DROP createdAt"); - $this->addSql("ALTER TABLE wikitag_tag DROP createdAt"); + $this->addSql("ALTER TABLE wikitag_document DROP created_at"); + $this->addSql("ALTER TABLE wikitag_document_tag DROP created_at"); + $this->addSql("ALTER TABLE wikitag_tag DROP created_at"); } } diff -r b36a42d260d8 -r 624e5900f5a4 app/autoload.php --- a/app/autoload.php Fri Dec 09 06:43:49 2011 +0100 +++ b/app/autoload.php Wed Dec 14 23:28:58 2011 +0100 @@ -5,20 +5,21 @@ $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( - 'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'), - 'Sensio' => __DIR__.'/../vendor/bundles', - 'JMS' => __DIR__.'/../vendor/bundles', - 'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib', - 'Doctrine\\DBAL\\Migrations' => __DIR__.'/../vendor/doctrine-migrations/lib', - 'Doctrine\\DBAL' => __DIR__.'/../vendor/doctrine-dbal/lib', - 'Doctrine' => __DIR__.'/../vendor/doctrine/lib', - 'Monolog' => __DIR__.'/../vendor/monolog/src', - 'Assetic' => __DIR__.'/../vendor/assetic/src', - 'Metadata' => __DIR__.'/../vendor/metadata/src', - 'FOS' => __DIR__.'/../vendor/bundles', - 'Pagerfanta' => __DIR__.'/../vendor/bundles', - 'Mandango\Mondator' => __DIR__.'/../vendor/mondator/src', - 'IRI\Bundle\WikiTagBundle' => __DIR__.'/../vendor/bundles', + 'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'), + 'Sensio' => __DIR__.'/../vendor/bundles', + 'JMS' => __DIR__.'/../vendor/bundles', + 'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib', + 'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib', + 'Doctrine\\DBAL\\Migrations' => __DIR__.'/../vendor/doctrine-migrations/lib', + 'Doctrine\\DBAL' => __DIR__.'/../vendor/doctrine-dbal/lib', + 'Doctrine' => __DIR__.'/../vendor/doctrine/lib', + 'Monolog' => __DIR__.'/../vendor/monolog/src', + 'Assetic' => __DIR__.'/../vendor/assetic/src', + 'Metadata' => __DIR__.'/../vendor/metadata/src', + 'FOS' => __DIR__.'/../vendor/bundles', + 'Pagerfanta' => __DIR__.'/../vendor/bundles', + 'Mandango\Mondator' => __DIR__.'/../vendor/mondator/src', + 'IRI\Bundle\WikiTagBundle' => __DIR__.'/../vendor/bundles', )); $loader->registerPrefixes(array( 'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib', diff -r b36a42d260d8 -r 624e5900f5a4 app/config/config_test.yml --- a/app/config/config_test.yml Fri Dec 09 06:43:49 2011 +0100 +++ b/app/config/config_test.yml Wed Dec 14 23:28:58 2011 +0100 @@ -12,3 +12,11 @@ swiftmailer: disable_delivery: true + +doctrine: + dbal: + dbname: %database_name_test% + +wiki_tag: + ignore_wikipedia_error: true + diff -r b36a42d260d8 -r 624e5900f5a4 app/config/parameters.ini.tmpl --- a/app/config/parameters.ini.tmpl Fri Dec 09 06:43:49 2011 +0100 +++ b/app/config/parameters.ini.tmpl Wed Dec 14 23:28:58 2011 +0100 @@ -2,18 +2,19 @@ ; by enclosing the key with % (like %database_user%) ; Comments start with ';', as in php.ini [parameters] - database_driver = pdo_mysql - database_host = localhost - database_port = - database_name = symfony - database_user = root - database_password = + database_driver = pdo_mysql + database_host = localhost + database_port = + database_name = symfony + database_name_test = symfony_test + database_user = root + database_password = - mailer_transport = smtp - mailer_host = localhost - mailer_user = - mailer_password = + mailer_transport = smtp + mailer_host = localhost + mailer_user = + mailer_password = - locale = en + locale = en - secret = ThisTokenIsNotSoSecretChangeIt + secret = ThisTokenIsNotSoSecretChangeIt diff -r b36a42d260d8 -r 624e5900f5a4 deps --- a/deps Fri Dec 09 06:43:49 2011 +0100 +++ b/deps Wed Dec 14 23:28:58 2011 +0100 @@ -64,3 +64,10 @@ [DoctrineMigrationsBundle] git=http://github.com/symfony/DoctrineMigrationsBundle.git target=/bundles/Symfony/Bundle/DoctrineMigrationsBundle + +[doctrine-fixtures] + git=http://github.com/doctrine/data-fixtures.git + +[DoctrineFixturesBundle] + git=http://github.com/symfony/DoctrineFixturesBundle.git + target=/bundles/Symfony/Bundle/DoctrineFixturesBundle diff -r b36a42d260d8 -r 624e5900f5a4 src/Company/BaseBundle/DataFixtures/ORM/LoadDocumentData.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Company/BaseBundle/DataFixtures/ORM/LoadDocumentData.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Company\BaseBundle\DataFixures\ORM; + +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Doctrine\Common\DataFixtures\FixtureInterface; +use Company\BaseBundle\Entity\Document; + +class LoadDocumentData implements FixtureInterface, ContainerAwareInterface +{ + private $container; + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + public function load($manager) { + + # create new document + + $doc_def_list = array( + array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4')), + array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4')), + array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4')), + array('title'=>'Title 4', 'description'=>'Description 4', 'tags' => array()), + ); + + foreach ($doc_def_list as $doc_def) { + + $newdoc = new Document(); + $newdoc->setTitle($doc_def['title']); + $newdoc->setDescription($doc_def['description']); + + $manager->persist($newdoc); + + $manager->flush(); + + $this->container->get('wiki_tag.document')->addTags($newdoc->getId(), $doc_def['tags']); + } + + # add tags + + $manager->flush(); + + } + +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Command/LoadDataFixturesDoctrineCommand.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Command/LoadDataFixturesDoctrineCommand.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,114 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\DoctrineFixturesBundle\Command; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\Output; +use Symfony\Component\Finder\Finder; +use Symfony\Bundle\FrameworkBundle\Util\Filesystem; +use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader as DataFixturesLoader; +use Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand; +use Doctrine\Common\DataFixtures\Executor\ORMExecutor; +use Doctrine\Common\DataFixtures\Purger\ORMPurger; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Internal\CommitOrderCalculator; +use Doctrine\ORM\Mapping\ClassMetadata; +use InvalidArgumentException; + +/** + * Load data fixtures from bundles. + * + * @author Fabien Potencier + * @author Jonathan H. Wage + */ +class LoadDataFixturesDoctrineCommand extends DoctrineCommand +{ + protected function configure() + { + $this + ->setName('doctrine:fixtures:load') + ->setDescription('Load data fixtures to your database.') + ->addOption('fixtures', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The directory or file to load data fixtures from.') + ->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of deleting all data from the database first.') + ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Purge data by using a database-level TRUNCATE statement') + ->setHelp(<<doctrine:fixtures:load command loads data fixtures from your bundles: + + ./app/console doctrine:fixtures:load + +You can also optionally specify the path to fixtures with the --fixtures option: + + ./app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2 + +If you want to append the fixtures instead of flushing the database first you can use the --append option: + + ./app/console doctrine:fixtures:load --append + +By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from +the database. If you want to use a TRUNCATE statement instead you can use the --purge-with-truncate flag: + + ./app/console doctrine:fixtures:load --purge-with-truncate +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $emName = $input->getOption('em'); + $emName = $emName ? $emName : 'default'; + $emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName); + + if (!$this->getContainer()->has($emServiceName)) { + throw new InvalidArgumentException( + sprintf( + 'Could not find an entity manager configured with the name "%s". Check your '. + 'application configuration to configure your Doctrine entity managers.', $emName + ) + ); + } + + $em = $this->getContainer()->get($emServiceName); + $dirOrFile = $input->getOption('fixtures'); + if ($dirOrFile) { + $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); + } else { + $paths = array(); + foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { + $paths[] = $bundle->getPath().'/DataFixtures/ORM'; + } + } + + $loader = new DataFixturesLoader($this->getContainer()); + foreach ($paths as $path) { + if (is_dir($path)) { + $loader->loadFromDirectory($path); + } + } + $fixtures = $loader->getFixtures(); + if (!$fixtures) { + throw new InvalidArgumentException( + sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $paths)) + ); + } + $purger = new ORMPurger($em); + $purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); + $executor = new ORMExecutor($em, $purger); + $executor->setLogger(function($message) use ($output) { + $output->writeln(sprintf(' > %s', $message)); + }); + $executor->execute($fixtures, $input->getOption('append')); + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Common/DataFixtures/Loader.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Common/DataFixtures/Loader.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,27 @@ +container = $container; + } + + public function addFixture(FixtureInterface $fixture) + { + if ($fixture instanceof ContainerAwareInterface) { + $fixture->setContainer($this->container); + } + + parent::addFixture($fixture); + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/DoctrineFixturesBundle.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/DoctrineFixturesBundle.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\DoctrineFixturesBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +/** + * Bundle. + * + * @author Fabien Potencier + * @author Jonathan H. Wage + */ +class DoctrineFixturesBundle extends Bundle +{ +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/README.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/README.markdown Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,8 @@ +DoctrineFixturesBundle +====================== + +This bundle integrates the [Doctrine2 Data Fixtures library](https://github.com/doctrine/data-fixtures). +into Symfony so that you can load data fixtures programmatically into the Doctrine ORM or ODM. + +Documentation on how to install and use this bundle is available in the +Symfony2 [documentation](http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html). diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Resources/doc/index.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Resources/doc/index.rst Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,325 @@ +DoctrineFixturesBundle +====================== + +Fixtures are used to load a controlled set of data into a database. This data +can be used for testing or could be the initial data required for the +application to run smoothly. Symfony2 has no built in way to manage fixtures +but Doctrine2 has a library to help you write fixtures for the Doctrine +:doc:`ORM` or :doc:`ODM`. + +Setup and Configuration +----------------------- + +If you don't have the `Doctrine Data Fixtures`_ library configured with Symfony2 +yet, follow these steps to do so. + +If you're using the Standard Distribution, add the following to your ``deps`` +file: + +.. code-block:: text + + [doctrine-fixtures] + git=http://github.com/doctrine/data-fixtures.git + + [DoctrineFixturesBundle] + git=http://github.com/symfony/DoctrineFixturesBundle.git + target=/bundles/Symfony/Bundle/DoctrineFixturesBundle + +Update the vendor libraries: + +.. code-block:: bash + + $ php bin/vendors install + +If everything worked, the ``doctrine-fixtures`` library can now be found +at ``vendor/doctrine-fixtures``. + +Register the ``Doctrine\Common\DataFixtures`` namespace in ``app/autoload.php``. + +.. code-block:: php + + // ... + $loader->registerNamespaces(array( + // ... + 'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib', + 'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib', + // ... + )); + +.. caution:: + + Be sure to register the new namespace *before* ``Doctrine\Common``. Otherwise, + Symfony will look for data fixture classes inside the ``Doctrine\Common`` + directory. Symfony's autoloader always looks for a class inside the directory + of the first matching namespace, so more specific namespaces should always + come first. + +Finally, register the Bundle ``DoctrineFixturesBundle`` in ``app/AppKernel.php``. + +.. code-block:: php + + // ... + public function registerBundles() + { + $bundles = array( + // ... + new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(), + // ... + ); + // ... + } + +Writing Simple Fixtures +----------------------- + +Doctrine2 fixtures are PHP classes where you can create objects and persist +them to the database. Like all classes in Symfony2, fixtures should live inside +one of your application bundles. + +For a bundle located at ``src/Acme/HelloBundle``, the fixture classes +should live inside ``src/Acme/HelloBundle/DataFixtures/ORM`` or +``src/Acme/HelloBundle/DataFixtures/MongoDB`` respectively for the ORM and ODM, +This tutorial assumes that you are using the ORM - but fixtures can be added +just as easily if you're using the ODM. + +Imagine that you have a ``User`` class, and you'd like to load one ``User`` +entry: + +.. code-block:: php + + // src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php + namespace Acme\HelloBundle\DataFixtures\ORM; + + use Doctrine\Common\DataFixtures\FixtureInterface; + use Acme\HelloBundle\Entity\User; + + class LoadUserData implements FixtureInterface + { + public function load($manager) + { + $userAdmin = new User(); + $userAdmin->setUsername('admin'); + $userAdmin->setPassword('test'); + + $manager->persist($userAdmin); + $manager->flush(); + } + } + +In Doctrine2, fixtures are just objects where you load data by interacting +with your entities as you normally do. This allows you to create the exact +fixtures you need for your application. + +The most serious limitation is that you cannot share objects between fixtures. +Later, you'll see how to overcome this limitation. + +Executing Fixtures +------------------ + +Once your fixtures have been written, you can load them via the command +line by using the ``doctrine:fixtures:load`` command: + +.. code-block:: bash + + php app/console doctrine:fixtures:load + +If you're using the ODM, use the ``doctrine:mongodb:fixtures:load`` command instead: + +.. code-block:: bash + + php app/console doctrine:mongodb:fixtures:load + +The task will look inside the ``DataFixtures/ORM`` (or ``DataFixtures/MongoDB`` +for the ODM) directory of each bundle and execute each class that implements +the ``FixtureInterface``. + +Both commands come with a few options: + +* ``--fixtures=/path/to/fixture`` - Use this option to manually specify the + directory where the fixtures classes should be loaded; + +* ``--append`` - Use this flag to append data instead of deleting data before + loading it (deleting first is the default behavior); + +* ``--em=manager_name`` - Manually specify the entity manager to use for + loading the data. + +.. note:: + + If using the ``doctrine:mongodb:fixtures:load`` task, replace the ``--em=`` + option with ``--dm=`` to manually specify the document manager. + +A full example use might look like this: + +.. code-block:: bash + + php app/console doctrine:fixtures:load --fixtures=/path/to/fixture1 --fixtures=/path/to/fixture2 --append --em=foo_manager + +Sharing Objects between Fixtures +-------------------------------- + +Writing a basic fixture is simple. But what if you have multiple fixture classes +and want to be able to refer to the data loaded in other fixture classes? +For example, what if you load a ``User`` object in one fixture, and then +want to refer to reference it in a different fixture in order to assign that +user to a particular group? + +The Doctrine fixtures library handles this easily by allowing you to specify +the order in which fixtures are loaded. + +.. code-block:: php + + // src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php + namespace Acme\HelloBundle\DataFixtures\ORM; + + use Doctrine\Common\DataFixtures\AbstractFixture; + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; + use Acme\HelloBundle\Entity\User; + + class LoadUserData extends AbstractFixture implements OrderedFixtureInterface + { + public function load($manager) + { + $userAdmin = new User(); + $userAdmin->setUsername('admin'); + $userAdmin->setPassword('test'); + + $manager->persist($userAdmin); + $manager->flush(); + + $this->addReference('admin-user', $userAdmin); + } + + public function getOrder() + { + return 1; // the order in which fixtures will be loaded + } + } + +The fixture class now implements ``OrderedFixtureInterface``, which tells +Doctrine that you want to control the order of your fixtures. Create another +fixture class and make it load after ``LoadUserData`` by returning an order +of 2: + +.. code-block:: php + + // src/Acme/HelloBundle/DataFixtures/ORM/LoadGroupData.php + namespace Acme\HelloBundle\DataFixtures\ORM; + + use Doctrine\Common\DataFixtures\AbstractFixture; + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; + use Acme\HelloBundle\Entity\Group; + + class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface + { + public function load($manager) + { + $groupAdmin = new Group(); + $groupAdmin->setGroupName('admin'); + + $manager->persist($groupAdmin); + $manager->flush(); + + $this->addReference('admin-group', $groupAdmin); + } + + public function getOrder() + { + return 2; // the order in which fixtures will be loaded + } + } + +Both of the fixture classes extend ``AbstractFixture``, which allows you +to create objects and then set them as references so that they can be used +later in other fixtures. For example, the ``$userAdmin`` and ``$groupAdmin`` +objects can be referenced later via the ``admin-user`` and ``admin-group`` +references: + +.. code-block:: php + + // src/Acme/HelloBundle/DataFixtures/ORM/LoadUserGroupData.php + namespace Acme\HelloBundle\DataFixtures\ORM; + + use Doctrine\Common\DataFixtures\AbstractFixture; + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; + use Acme\HelloBundle\Entity\UserGroup; + + class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface + { + public function load($manager) + { + $userGroupAdmin = new UserGroup(); + $userGroupAdmin->setUser($manager->merge($this->getReference('admin-user'))); + $userGroupAdmin->setGroup($manager->merge($this->getReference('admin-group'))); + + $manager->persist($userGroupAdmin); + $manager->flush(); + } + + public function getOrder() + { + return 3; + } + } + +The fixtures will now be executed in the ascending order of the value returned +by ``getOrder()``. Any object that is set with the ``setReference()`` method +can be accessed via ``getReference()`` in fixture classes that have a higher +order. + +Fixtures allow you to create any type of data you need via the normal PHP +interface for creating and persisting objects. By controlling the order of +fixtures and setting references, almost anything can be handled by fixtures. + +Using the Container in the Fixtures +----------------------------------- + +In some cases you may need to access some services to load the fixtures. +Symfony2 makes it really easy: the container will be injected in all fixture +classes implementing :class:`Symfony\\Component\\DependencyInjection\\ContainerAwareInterface`. + +Let's rewrite the first fixture to encode the password before it's stored +in the database (a very good practice). This will use the encoder factory +to encode the password, ensuring it is encoded in the way used by the security +component when checking it: + +.. code-block:: php + + // src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php + namespace Acme\HelloBundle\DataFixtures\ORM; + + use Doctrine\Common\DataFixtures\FixtureInterface; + use Symfony\Component\DependencyInjection\ContainerAwareInterface; + use Symfony\Component\DependencyInjection\ContainerInterface; + use Acme\HelloBundle\Entity\User; + + class LoadUserData implements FixtureInterface, ContainerAwareInterface + { + private $container; + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + public function load($manager) + { + $userAdmin = new User(); + $userAdmin->setUsername('admin'); + $userAdmin->setSalt(md5(time())); + + $encoder = $this->container->get('security.encoder_factory')->getEncoder($userAdmin); + $userAdmin->setPassword($encoder->encodePassword('test', $userAdmin->getSalt())); + + $manager->persist($userAdmin); + $manager->flush(); + } + } + +As you can see, all you need to do is add ``ContainerAwareInterface`` to +the class and then create a new ``setContainer()`` method that implements +that interface. Before the fixture is executed, Symfony will call the ``setContainer()`` +method automatically. As long as you store the container as a property on +the class (as shown above), you can access it in the ``load()`` method. + +.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Tests/Common/ContainerAwareFixture.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Tests/Common/ContainerAwareFixture.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,21 @@ +container = $container; + } + + public function load($manager) + { + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Tests/Common/DataFixtures/LoaderTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Tests/Common/DataFixtures/LoaderTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,21 @@ +getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $loader = new Loader($container); + $fixture = new ContainerAwareFixture(); + + $loader->addFixture($fixture); + + $this->assertSame($container, $fixture->container); + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Tests/TestCase.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Tests/TestCase.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,13 @@ +markTestSkipped('Doctrine Data Fixtures is not available.'); + } + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/README.md Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,138 @@ +# Doctrine Data Fixtures Extension + +This extension aims to provide a simple way to manage and execute the loading of data fixtures +for the Doctrine ORM or ODM. You can write fixture classes by implementing the +Doctrine\Common\DataFixtures\FixtureInterface interface: + + namespace MyDataFixtures; + + use Doctrine\ORM\EntityManager; + use Doctrine\Common\DataFixtures\FixtureInterface; + + class LoadUserData implements FixtureInterface + { + public function load($manager) + { + $user = new User(); + $user->setUsername('jwage'); + $user->setPassword('test'); + + $manager->persist($user); + $manager->flush(); + } + } + +Now you can begin adding the fixtures to a loader instance: + + use Doctrine\Common\DataFixtures\Loader; + use MyDataFixtures\LoadUserData; + + $loader = new Loader(); + $loader->addFixture(new LoadUserData); + +You can load a set of fixtures from a directory as well: + + $loader->loadFromDirectory('/path/to/MyDataFixtures'); + +You can get the added fixtures using the getFixtures() method: + + $fixtures = $loader->getFixtures(); + +Now you can easily execute the fixtures: + + use Doctrine\Common\DataFixtures\Executor\ORMExecutor; + + $purger = new Purger(); + $executor = new ORMExecutor($em, $purger); + $executor->execute($loader->getFixtures()); + +If you want to append the fixtures instead of purging before loading then pass false +to the 2nd argument of execute: + + $executor->execute($loader->getFixtures(), true); + +## Sharing objects between fixtures + +In case if fixture objects have relations to other fixtures, it is now possible +to easily add a reference to that object by name and later reference it to form +a relation. Here is an example fixtures for **Role** and **User** relation + + namespace MyDataFixtures; + + use Doctrine\Common\DataFixtures\AbstractFixture; + + class LoadUserRoleData extends AbstractFixture + { + public function load($manager) + { + $adminRole = new Role(); + $adminRole->setName('admin'); + + $anonymousRole = new Role; + $anonymousRole->setName('anonymous'); + + $manager->persist($adminRole); + $manager->persist($anonymousRole); + $manager->flush(); + + // store reference to admin role for User relation to Role + $this->addReference('admin-role', $adminRole); + } + } + +And the **User** data loading fixture: + + namespace MyDataFixtures; + + use Doctrine\Common\DataFixtures\AbstractFixture; + + class LoadUserData extends AbstractFixture + { + public function load($manager) + { + $user = new User(); + $user->setUsername('jwage'); + $user->setPassword('test'); + $user->setRole( + $this->getReference('admin-role') // load the stored reference + ); + + $manager->persist($user); + $manager->flush(); + + // store reference of admin-user for other Fixtures + $this->addReference('admin-user', $user); + } + } + +**Notice** that the fixture loading order is important! To handle it manually +implement the OrderedFixtureInterface and set the order: + + namespace MyDataFixtures; + + use Doctrine\Common\DataFixtures\AbstractFixture; + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; + + class MyFixture extends AbstractFixture implements OrderedFixtureInterface + { + public function load($manager) + {} + + public function getOrder() + { + return 10; // number in which order to load fixtures + } + } + +**Notice** the ordering is relevant to Loader class. + +## Running the tests: + +PHPUnit 3.5 or newer together with Mock_Object package is required. +To setup and run tests follow these steps: + +- go to the root directory of data-fixtures +- run: **git submodule init** +- run: **git submodule update** +- copy the phpunit config **cp phpunit.xml.dist phpunit.xml** +- run: **phpunit** diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/composer.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/composer.json Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,17 @@ +{ + "name": "doctrine/data-fixtures", + "type": "library", + "description": "Data Fixtures for all Doctrine Object Managers", + "keywords": ["database"], + "homepage": "http://www.doctrine-project.org", + "license": "LGPL", + "authors": [ + {"name": "Jonathan Wage", "email": "jonwage@gmail.com"} + ], + "require": { + "php": ">=5.3.2" + }, + "autoload": { + "psr-0": { "Doctrine\\Common\\DataFixtures": "lib/" } + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/AbstractFixture.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/AbstractFixture.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,103 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +use Doctrine\Common\DataFixtures\ReferenceRepository; + +/** + * Abstract Fixture class helps to manage references + * between fixture classes in order to set relations + * among other fixtures + * + * @author Gediminas Morkevicius + */ +abstract class AbstractFixture implements SharedFixtureInterface +{ + /** + * Fixture reference repository + * + * @var ReferenceRepository + */ + private $referenceRepository; + + /** + * {@inheritdoc} + */ + public function setReferenceRepository(ReferenceRepository $referenceRepository) + { + $this->referenceRepository = $referenceRepository; + } + + /** + * Set the reference entry identified by $name + * and referenced to managed $object. If $name + * already is set, it overrides it + * + * @param string $name + * @param object $object - managed object + * @see Doctrine\Common\DataFixtures\ReferenceRepository::setReference + * @return void + */ + public function setReference($name, $object) + { + $this->referenceRepository->setReference($name, $object); + } + + /** + * Set the reference entry identified by $name + * and referenced to managed $object. If $name + * already is set, it overrides it + * + * @param string $name + * @param object $object - managed object + * @see Doctrine\Common\DataFixtures\ReferenceRepository::addReference + * @return void + */ + public function addReference($name, $object) + { + $this->referenceRepository->addReference($name, $object); + } + + /** + * Loads an object using stored reference + * named by $name + * + * @param string $name + * @see Doctrine\Common\DataFixtures\ReferenceRepository::getReference + * @return object + */ + public function getReference($name) + { + return $this->referenceRepository->getReference($name); + } + + /** + * Check if an object is stored using reference + * named by $name + * + * @param string $name + * @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference + * @return boolean + */ + public function hasReference($name) + { + return $this->referenceRepository->hasReference($name); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/DependentFixtureInterface.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/DependentFixtureInterface.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,37 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +/** + * DependentFixtureInterface needs to be implemented + * by fixtures which depend on other fixtures + * + * @author Gustavo Adrian + */ +interface DependentFixtureInterface +{ + /* + * This method must return an array of fixtures classes + * on which the implementing class depends on + * + * @return array + */ + function getDependencies(); +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Event/Listener/MongoDBReferenceListener.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Event/Listener/MongoDBReferenceListener.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,75 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Event\Listener; + +use Doctrine\Common\EventSubscriber; +use Doctrine\Common\DataFixtures\ReferenceRepository; +use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; + +/** + * Reference Listener populates identities for + * stored references + * + * @author Gediminas Morkevicius + */ +final class MongoDBReferenceListener implements EventSubscriber +{ + /** + * @var ReferenceRepository + */ + private $referenceRepository; + + /** + * Initialize listener + * + * @param ReferenceRepository $referenceRepository + */ + public function __construct(ReferenceRepository $referenceRepository) + { + $this->referenceRepository = $referenceRepository; + } + + /** + * {@inheritdoc} + */ + public function getSubscribedEvents() + { + return array( + 'postPersist' + ); + } + + /** + * Populates identities for stored references + * + * @param LifecycleEventArgs $args + */ + public function postPersist(LifecycleEventArgs $args) + { + $object = $args->getDocument(); + if (($name = $this->referenceRepository->getReferenceName($object)) !== false) { + $identity = $args->getDocumentManager() + ->getUnitOfWork() + ->getDocumentIdentifier($object); + $this->referenceRepository->setReferenceIdentity($name, $identity); + } + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Event/Listener/ORMReferenceListener.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Event/Listener/ORMReferenceListener.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,75 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Event\Listener; + +use Doctrine\Common\EventSubscriber; +use Doctrine\Common\DataFixtures\ReferenceRepository; +use Doctrine\ORM\Event\LifecycleEventArgs; + +/** + * Reference Listener populates identities for + * stored references + * + * @author Gediminas Morkevicius + */ +final class ORMReferenceListener implements EventSubscriber +{ + /** + * @var ReferenceRepository + */ + private $referenceRepository; + + /** + * Initialize listener + * + * @param ReferenceRepository $referenceRepository + */ + public function __construct(ReferenceRepository $referenceRepository) + { + $this->referenceRepository = $referenceRepository; + } + + /** + * {@inheritdoc} + */ + public function getSubscribedEvents() + { + return array( + 'postPersist' // would be better to use onClear, but it is supported only in 2.1 + ); + } + + /** + * Populates identities for stored references + * + * @param LifecycleEventArgs $args + */ + public function postPersist(LifecycleEventArgs $args) + { + $object = $args->getEntity(); + if (($name = $this->referenceRepository->getReferenceName($object)) !== false) { + $identity = $args->getEntityManager() + ->getUnitOfWork() + ->getEntityIdentifier($object); + $this->referenceRepository->setReferenceIdentity($name, $identity); + } + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Exception/CircularReferenceException.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Exception/CircularReferenceException.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,9 @@ + + */ +abstract class AbstractExecutor +{ + /** Purger instance for purging database before loading data fixtures */ + protected $purger; + + /** Logger callback for logging messages when loading data fixtures */ + protected $logger; + + /** + * Fixture reference repository + * @var ReferenceRepository + */ + protected $referenceRepository; + + /** + * Loads an instance of reference repository + * + * @param object $manager + */ + public function __construct($manager) + { + $this->referenceRepository = new ReferenceRepository($manager); + } + + /** + * Get reference repository + * + * @return ReferenceRepository + */ + public function getReferenceRepository() + { + return $this->referenceRepository; + } + + /** + * Sets the Purger instance to use for this exector instance. + * + * @param Purger $purger + */ + public function setPurger(Purger $purger) + { + $this->purger = $purger; + } + + /** + * Set the logger callable to execute with the log() method. + * + * @param $logger + */ + public function setLogger($logger) + { + $this->logger = $logger; + } + + /** + * Logs a message using the logger. + * + * @param string $message + */ + public function log($message) + { + $logger = $this->logger; + $logger($message); + } + + /** + * Load a fixture with the given persistence manager. + * + * @param object $manager + * @param FixtureInterface $fixture + */ + public function load($manager, FixtureInterface $fixture) + { + if ($this->logger) { + $this->log('loading ' . get_class($fixture)); + } + // additionaly pass the instance of reference repository to shared fixtures + if ($fixture instanceof SharedFixtureInterface) { + $fixture->setReferenceRepository($this->referenceRepository); + } + $fixture->load($manager); + $manager->clear(); + } + + /** + * Purges the database before loading. + */ + public function purge() + { + if ($this->purger === null) { + throw new \Exception('Doctrine\Common\DataFixtures\Purger\PurgerInterface instance is required if you want to purge the database before loading your data fixtures.'); + } + if ($this->logger) { + $this->log('purging database'); + } + $this->purger->purge(); + } + + /** + * Executes the given array of data fixtures. + * + * @param array $fixtures Array of fixtures to execute. + * @param boolean $append Whether to append the data fixtures or purge the database before loading. + */ + abstract public function execute(array $fixtures, $append = false); +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Executor/MongoDBExecutor.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Executor/MongoDBExecutor.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,61 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Executor; + +use Doctrine\ODM\MongoDB\DocumentManager; +use Doctrine\Common\DataFixtures\Purger\MongoDBPurger; +use Doctrine\Common\DataFixtures\Event\Listener\MongoDBReferenceListener; + +/** + * Class responsible for executing data fixtures. + * + * @author Jonathan H. Wage + */ +class MongoDBExecutor extends AbstractExecutor +{ + /** + * Construct new fixtures loader instance. + * + * @param DocumentManager $dm DocumentManager instance used for persistence. + */ + public function __construct(DocumentManager $dm, MongoDBPurger $purger = null) + { + $this->dm = $dm; + if ($purger !== null) { + $this->purger = $purger; + $this->purger->setDocumentManager($dm); + } + parent::__construct($dm); + $dm->getEventManager()->addEventSubscriber( + new MongoDBReferenceListener($this->referenceRepository) + ); + } + + /** @inheritDoc */ + public function execute(array $fixtures, $append = false) + { + if ($append === false) { + $this->purge(); + } + foreach ($fixtures as $fixture) { + $this->load($this->dm, $fixture); + } + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,64 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Executor; + +use Doctrine\ORM\EntityManager; +use Doctrine\Common\DataFixtures\Purger\ORMPurger; +use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener; + +/** + * Class responsible for executing data fixtures. + * + * @author Jonathan H. Wage + */ +class ORMExecutor extends AbstractExecutor +{ + /** + * Construct new fixtures loader instance. + * + * @param EntityManager $em EntityManager instance used for persistence. + */ + public function __construct(EntityManager $em, ORMPurger $purger = null) + { + $this->em = $em; + if ($purger !== null) { + $this->purger = $purger; + $this->purger->setEntityManager($em); + } + parent::__construct($em); + $em->getEventManager()->addEventSubscriber( + new ORMReferenceListener($this->referenceRepository) + ); + } + + /** @inheritDoc */ + public function execute(array $fixtures, $append = false) + { + $executor = $this; + $this->em->transactional(function(EntityManager $em) use ($executor, $fixtures, $append) { + if ($append === false) { + $executor->purge(); + } + foreach ($fixtures as $fixture) { + $executor->load($em, $fixture); + } + }); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Executor/PHPCRExecutor.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Executor/PHPCRExecutor.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,59 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Executor; + +use Doctrine\ODM\PHPCR\DocumentManager; +use Doctrine\Common\DataFixtures\Purger\PHPCRPurger; + +/** + * Class responsible for executing data fixtures. + * + * @author Jonathan H. Wage + * @author Daniel Barsotti + */ +class PHPCRExecutor extends AbstractExecutor +{ + /** + * Construct new fixtures loader instance. + * + * @param DocumentManager $dm DocumentManager instance used for persistence. + */ + public function __construct(DocumentManager $dm, PHPCRPurger $purger = null) + { + $this->dm = $dm; + if ($purger !== null) { + $this->purger = $purger; + $this->purger->setDocumentManager($dm); + } + parent::__construct($dm); + } + + /** @inheritDoc */ + public function execute(array $fixtures, $append = false) + { + if ($append === false) { + $this->purge(); + } + foreach ($fixtures as $fixture) { + $this->load($this->dm, $fixture); + } + } +} + diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/FixtureInterface.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/FixtureInterface.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,35 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +/** + * Interface contract for fixture classes to implement. + * + * @author Jonathan H. Wage + */ +interface FixtureInterface +{ + /** + * Load data fixtures with the passed EntityManager + * + * @param object $manager + */ + function load($manager); +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,320 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +use Doctrine\ORM\EntityManager; +use Doctrine\Common\DataFixtures\Exception\CircularReferenceException; + +/** + * Class responsible for loading data fixture classes. + * + * @author Jonathan H. Wage + */ +class Loader +{ + /** + * Array of fixture object instances to execute. + * + * @var array + */ + private $fixtures = array(); + + /** + * Array of ordered fixture object instances. + * + * @var array + */ + private $orderedFixtures; + + /** + * Determines if we must order fixtures by number + * + * @var boolean + */ + private $orderFixturesByNumber = false; + + /** + * Determines if we must order fixtures by its dependencies + * + * @var boolean + */ + private $orderFixturesByDependencies = false; + + /** + * The file extension of fixture files. + * + * @var string + */ + private $fileExtension = '.php'; + + /** + * Find fixtures classes in a given directory and load them. + * + * @param string $dir Directory to find fixture classes in. + * @return array $fixtures Array of loaded fixture object instances + */ + public function loadFromDirectory($dir) + { + if (!is_dir($dir)) { + throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir)); + } + + $fixtures = array(); + $includedFiles = array(); + + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($dir), + \RecursiveIteratorIterator::LEAVES_ONLY + ); + + foreach ($iterator as $file) { + if (($fileName = $file->getBasename($this->fileExtension)) == $file->getBasename()) { + continue; + } + $sourceFile = realpath($file->getPathName()); + require_once $sourceFile; + $includedFiles[] = $sourceFile; + } + $declared = get_declared_classes(); + + foreach ($declared as $className) { + $reflClass = new \ReflectionClass($className); + $sourceFile = $reflClass->getFileName(); + + if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) { + $fixture = new $className; + $fixtures[] = $fixture; + $this->addFixture($fixture); + } + } + return $fixtures; + } + + /** + * Add a fixture object instance to the loader. + * + * @param object $fixture + */ + public function addFixture(FixtureInterface $fixture) + { + $fixtureClass = get_class($fixture); + + if (!isset($this->fixtures[$fixtureClass])) { + if ($fixture instanceof OrderedFixtureInterface && $fixture instanceof DependentFixtureInterface) { + throw new \InvalidArgumentException(sprintf('Class "%s" can\'t implement "%s" and "%s" at the same time.', + get_class($fixture), + 'OrderedFixtureInterface', + 'DependentFixtureInterface')); + } elseif ($fixture instanceof OrderedFixtureInterface) { + $this->orderFixturesByNumber = true; + } elseif ($fixture instanceof DependentFixtureInterface) { + $this->orderFixturesByDependencies = true; + } + + $this->fixtures[$fixtureClass] = $fixture; + } + } + + /** + * Returns the array of data fixtures to execute. + * + * @return array $fixtures + */ + public function getFixtures() + { + if ($this->orderFixturesByNumber) { + $this->orderFixturesByNumber(); + } + + if ($this->orderFixturesByDependencies) { + $this->orderFixturesByDependencies(); + } + + if (!$this->orderFixturesByNumber && !$this->orderFixturesByDependencies) { + $this->orderedFixtures = $this->fixtures; + } + + return $this->orderedFixtures; + } + + /** + * Check if a given fixture is transient and should not be considered a data fixtures + * class. + * + * @return boolean + */ + public function isTransient($className) + { + $rc = new \ReflectionClass($className); + if ($rc->isAbstract()) return true; + + $interfaces = class_implements($className); + return in_array('Doctrine\Common\DataFixtures\FixtureInterface', $interfaces) ? false : true; + } + + /** + * Orders fixtures by number + * + * @todo maybe there is a better way to handle reordering + * @return void + */ + private function orderFixturesByNumber() + { + $this->orderedFixtures = $this->fixtures; + usort($this->orderedFixtures, function($a, $b) { + if ($a instanceof OrderedFixtureInterface && $b instanceof OrderedFixtureInterface) { + if ($a->getOrder() === $b->getOrder()) { + return 0; + } + return $a->getOrder() < $b->getOrder() ? -1 : 1; + } elseif ($a instanceof OrderedFixtureInterface) { + return $a->getOrder() === 0 ? 0 : 1; + } elseif ($b instanceof OrderedFixtureInterface) { + return $b->getOrder() === 0 ? 0 : -1; + } + return 0; + }); + } + + + /** + * Orders fixtures by dependencies + * + * @return void + */ + private function orderFixturesByDependencies() + { + $sequenceForClasses = array(); + + // If fixtures were already ordered by number then we need + // to remove classes which are not instances of OrderedFixtureInterface + // in case fixtures implementing DependentFixtureInterface exist. + // This is because, in that case, the method orderFixturesByDependencies + // will handle all fixtures which are not instances of + // OrderedFixtureInterface + if ($this->orderFixturesByNumber) { + $count = count($this->orderedFixtures); + + for ($i = 0 ; $i < $count ; ++$i) { + if (!($this->orderedFixtures[$i] instanceof OrderedFixtureInterface)) { + unset($this->orderedFixtures[$i]); + } + } + } + + // First we determine which classes has dependencies and which don't + foreach ($this->fixtures as $fixture) { + $fixtureClass = get_class($fixture); + + if ($fixture instanceof OrderedFixtureInterface) { + continue; + } elseif ($fixture instanceof DependentFixtureInterface) { + $dependenciesClasses = $fixture->getDependencies(); + + $this->validateDependencies($dependenciesClasses); + + if (!is_array($dependenciesClasses) || empty($dependenciesClasses)) { + throw new \InvalidArgumentException(sprintf('Method "%s" in class "%s" must return an array of classes which are dependencies for the fixture, and it must be NOT empty.', 'getDependencies', $fixtureClass)); + } + + if (in_array($fixtureClass, $dependenciesClasses)) { + throw new \InvalidArgumentException(sprintf('Class "%s" can\'t have itself as a dependency', $fixtureClass)); + } + + // We mark this class as unsequenced + $sequenceForClasses[$fixtureClass] = -1; + } else { + // This class has no dependencies, so we assign 0 + $sequenceForClasses[$fixtureClass] = 0; + } + } + + // Now we order fixtures by sequence + $sequence = 1; + $lastCount = -1; + + while (($count = count($unsequencedClasses = $this->getUnsequencedClasses($sequenceForClasses))) > 0 && $count !== $lastCount) { + foreach ($unsequencedClasses as $key => $class) { + $fixture = $this->fixtures[$class]; + $dependencies = $fixture->getDependencies(); + $unsequencedDependencies = $this->getUnsequencedClasses($sequenceForClasses, $dependencies); + + if (count($unsequencedDependencies) === 0) { + $sequenceForClasses[$class] = $sequence++; + } + } + + $lastCount = $count; + } + + $orderedFixtures = array(); + + // If there're fixtures unsequenced left and they couldn't be sequenced, + // it means we have a circular reference + if ($count > 0) { + $msg = 'Classes "%s" have produced a CircularReferenceException. '; + $msg .= 'An example of this problem would be the following: Class C has class B as its dependency. '; + $msg .= 'Then, class B has class A has its dependency. Finally, class A has class C as its dependency. '; + $msg .= 'This case would produce a CircularReferenceException.'; + + throw new CircularReferenceException(sprintf($msg, implode(',', $unsequencedClasses))); + } else { + // We order the classes by sequence + asort($sequenceForClasses); + + foreach ($sequenceForClasses as $class => $sequence) { + // If fixtures were ordered + $orderedFixtures[] = $this->fixtures[$class]; + } + } + + $this->orderedFixtures = is_array($this->orderedFixtures) ? array_merge($this->orderedFixtures, $orderedFixtures) : $orderedFixtures; + } + + private function validateDependencies($dependenciesClasses) + { + $loadedFixtureClasses = array_keys($this->fixtures); + + foreach ($dependenciesClasses as $class) { + if (!in_array($class, $loadedFixtureClasses)) { + throw new \RuntimeException(sprintf('Fixture "%s" was declared as a dependency, but it doesn\'t exist.', $class)); + } + } + + return true; + } + + private function getUnsequencedClasses($sequences, $classes = null) + { + $unsequencedClasses = array(); + + if (is_null($classes)) { + $classes = array_keys($sequences); + } + + foreach ($classes as $class) { + if ($sequences[$class] === -1) { + $unsequencedClasses[] = $class; + } + } + + return $unsequencedClasses; + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/OrderedFixtureInterface.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/OrderedFixtureInterface.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,38 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +/** + * Ordered Fixture interface needs to be implemented + * by fixtures, which needs to have a specific order + * when beeing loaded by directory scan for example + * + * @author Gediminas Morkevicius + * @author Jonathan H. Wage + */ +interface OrderedFixtureInterface +{ + /** + * Get the order of this fixture + * + * @return integer + */ + function getOrder(); +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/MongoDBPurger.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/MongoDBPurger.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,64 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Purger; + +use Doctrine\ODM\MongoDB\DocumentManager; + +/** + * Class responsible for purging databases of data before reloading data fixtures. + * + * @author Jonathan H. Wage + */ +class MongoDBPurger implements PurgerInterface +{ + /** DocumentManager instance used for persistence. */ + private $dm; + + /** + * Construct new purger instance. + * + * @param DocumentManager $dm DocumentManager instance used for persistence. + */ + public function __construct(DocumentManager $dm = null) + { + $this->dm = $dm; + } + + /** + * Set the DocumentManager instance this purger instance should use. + * + * @param DocumentManager $dm + */ + public function setDocumentManager(DocumentManager $dm) + { + $this->dm = $dm; + } + + /** @inheritDoc */ + public function purge() + { + $metadatas = $this->dm->getMetadataFactory()->getAllMetadata(); + foreach ($metadatas as $metadata) { + if ( ! $metadata->isMappedSuperclass) { + $this->dm->getDocumentCollection($metadata->name)->drop(); + } + } + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,165 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Purger; + +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Internal\CommitOrderCalculator; +use Doctrine\ORM\Mapping\ClassMetadata; + +/** + * Class responsible for purging databases of data before reloading data fixtures. + * + * @author Jonathan H. Wage + * @author Benjamin Eberlei + */ +class ORMPurger implements PurgerInterface +{ + const PURGE_MODE_DELETE = 1; + const PURGE_MODE_TRUNCATE = 2; + + /** EntityManager instance used for persistence. */ + private $em; + + /** + * If the purge should be done through DELETE or TRUNCATE statements + * + * @var int + */ + private $purgeMode = self::PURGE_MODE_DELETE; + + /** + * Construct new purger instance. + * + * @param EntityManager $em EntityManager instance used for persistence. + */ + public function __construct(EntityManager $em = null) + { + $this->em = $em; + } + + /** + * Set the purge mode + * + * @param $mode + * @return void + */ + public function setPurgeMode($mode) + { + $this->purgeMode = $mode; + } + + /** + * Get the purge mode + * + * @return int + */ + public function getPurgeMode() + { + return $this->purgeMode; + } + + /** + * Set the EntityManager instance this purger instance should use. + * + * @param EntityManager $em + */ + public function setEntityManager(EntityManager $em) + { + $this->em = $em; + } + + /** @inheritDoc */ + public function purge() + { + $classes = array(); + $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); + + foreach ($metadatas as $metadata) { + if ( ! $metadata->isMappedSuperclass) { + $classes[] = $metadata; + } + } + + $commitOrder = $this->getCommitOrder($this->em, $classes); + + // Drop association tables first + $orderedTables = $this->getAssociationTables($commitOrder); + + // Drop tables in reverse commit order + for ($i = count($commitOrder) - 1; $i >= 0; --$i) { + $class = $commitOrder[$i]; + + if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName) + || $class->isMappedSuperclass) { + continue; + } + + $orderedTables[] = $class->getTableName(); + } + + $platform = $this->em->getConnection()->getDatabasePlatform(); + foreach($orderedTables as $tbl) { + if ($this->purgeMode === self::PURGE_MODE_DELETE) { + $this->em->getConnection()->executeUpdate("DELETE FROM " . $tbl); + } else { + $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true)); + } + } + } + + private function getCommitOrder(EntityManager $em, array $classes) + { + $calc = new CommitOrderCalculator; + + foreach ($classes as $class) { + $calc->addClass($class); + + foreach ($class->associationMappings as $assoc) { + if ($assoc['isOwningSide']) { + $targetClass = $em->getClassMetadata($assoc['targetEntity']); + + if ( ! $calc->hasClass($targetClass->name)) { + $calc->addClass($targetClass); + } + + // add dependency ($targetClass before $class) + $calc->addDependency($targetClass, $class); + } + } + } + + return $calc->getCommitOrder(); + } + + private function getAssociationTables(array $classes) + { + $associationTables = array(); + + foreach ($classes as $class) { + foreach ($class->associationMappings as $assoc) { + if ($assoc['isOwningSide'] && $assoc['type'] == ClassMetadata::MANY_TO_MANY) { + $associationTables[] = $assoc['joinTable']['name']; + } + } + } + + return $associationTables; + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/PHPCRPurger.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/PHPCRPurger.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,68 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Purger; + +use Doctrine\ODM\PHPCR\DocumentManager; + +/** + * Class responsible for purging databases of data before reloading data fixtures. + * + * @author Daniel Barsotti + */ +class PHPCRPurger implements PurgerInterface +{ + /** DocumentManager instance used for persistence. */ + private $dm; + + /** + * Construct new purger instance. + * + * @param DocumentManager $dm DocumentManager instance used for persistence. + */ + public function __construct(DocumentManager $dm = null) + { + $this->dm = $dm; + } + + /** + * Set the DocumentManager instance this purger instance should use. + * + * @param DocumentManager $dm + */ + public function setDocumentManager(DocumentManager $dm) + { + $this->dm = $dm; + } + + /** + * Delete all the nodes in the repository which are not prefixed with jcr: + */ + public function purge() + { + $session = $this->dm->getPhpcrSession(); + foreach($session->getRootNode()->getNodes() as $node) { + if (substr($node->getName(), 0, 4) !== 'jcr:') { + $node->remove(); + } + } + $session->save(); + } + +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/PurgerInterface.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/PurgerInterface.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,35 @@ +. + */ + +namespace Doctrine\Common\DataFixtures\Purger; + +/** + * PurgerInterface + * + * @author Jonathan H. Wage + */ +interface PurgerInterface +{ + /** + * Purge the data from the database for the given EntityManager. + * + * @return void + */ + function purge(); +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,186 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +/** + * ReferenceRepository class manages references for + * fixtures in order to easily support the relations + * between fixtures + * + * @author Gediminas Morkevicius + */ +class ReferenceRepository +{ + /** + * List of named references to the fixture objects + * gathered during loads of fixtures + * + * @var array + */ + private $references = array(); + + /** + * List of identifiers stored for references + * in case if reference gets unmanaged, it will + * use a proxy referenced by this identity + * + * @var array + */ + private $identities = array(); + + /** + * Currently used object manager + * + * @var object - object manager + */ + private $manager; + + /** + * Initialize the ReferenceRepository + * + * @param object $manager + */ + public function __construct($manager) + { + $this->manager = $manager; + } + + /** + * Set the reference entry identified by $name + * and referenced to $reference. If $name + * already is set, it overrides it + * + * @param string $name + * @param object $reference + */ + public function setReference($name, $reference) + { + $this->references[$name] = $reference; + // in case if reference is set after flush, store its identity + $uow = $this->manager->getUnitOfWork(); + if ($uow->isInIdentityMap($reference)) { + if ($uow instanceof \Doctrine\ORM\UnitOfWork) { + $this->identities[$name] = $uow->getEntityIdentifier($reference); + } else { + $this->identities[$name] = $uow->getDocumentIdentifier($reference); + } + } + } + + /** + * Store the identifier of a reference + * + * @param string $name + * @param mixed $identity + */ + public function setReferenceIdentity($name, $identity) + { + $this->identities[$name] = $identity; + } + + /** + * Set the reference entry identified by $name + * and referenced to managed $object. $name must + * not be set yet + * + * Notice: in case if identifier is generated after + * the record is inserted, be sure tu use this method + * after $object is flushed + * + * @param string $name + * @param object $object - managed object + * @throws BadMethodCallException - if repository already has + * a reference by $name + * @return void + */ + public function addReference($name, $object) + { + if (isset($this->references[$name])) { + throw new \BadMethodCallException("Reference to: ({$name}) already exists, use method setReference in order to override it"); + } + $this->setReference($name, $object); + } + + /** + * Loads an object using stored reference + * named by $name + * + * @param string $name + * @return object + */ + public function getReference($name) + { + $reference = $this->references[$name]; + $meta = $this->manager->getClassMetadata(get_class($reference)); + $uow = $this->manager->getUnitOfWork(); + if (!$uow->isInIdentityMap($reference) && isset($this->identities[$name])) { + $reference = $this->manager->getReference( + $meta->name, + $this->identities[$name] + ); + $this->references[$name] = $reference; // allready in identity map + } + return $reference; + } + + /** + * Check if an object is stored using reference + * named by $name + * + * @param string $name + * @return boolean + */ + public function hasReference($name) + { + return isset($this->references[$name]); + } + + /** + * Searches for a reference name in the + * list of stored references + * + * @param object $reference + * @return string + */ + public function getReferenceName($reference) + { + return array_search($reference, $this->references, true); + } + + /** + * Checks if reference has identity stored + * + * @param string $name + */ + public function hasIdentity($name) + { + return array_key_exists($name, $this->identities); + } + + /** + * Get all stored references + * + * @return array + */ + public function getReferences() + { + return $this->references; + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/SharedFixtureInterface.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/SharedFixtureInterface.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,40 @@ +. + */ + +namespace Doctrine\Common\DataFixtures; + +use Doctrine\Common\DataFixtures\ReferenceRepository; + +/** + * Shared Fixture interface needs to be implemented + * by fixtures, which needs some references to be shared + * among other fixture classes in order to maintain + * relation mapping + * + * @author Gediminas Morkevicius + */ +interface SharedFixtureInterface extends FixtureInterface +{ + /** + * Set the reference repository + * + * @param ReferenceRepository $referenceRepository + */ + function setReferenceRepository(ReferenceRepository $referenceRepository); +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/phpunit.xml.dist --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/phpunit.xml.dist Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,17 @@ + + + + + + ./tests/Doctrine/ + + + \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/BaseTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/BaseTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,143 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +require_once __DIR__.'/TestInit.php'; + +use Doctrine\ORM\EntityManager; +use PHPUnit_Framework_TestCase; + +/** + * Base test class + * + * @author Jonathan H. Wage + */ +abstract class BaseTest extends PHPUnit_Framework_TestCase +{ + protected function getMockEntityManager() + { + $driver = $this->getMock('Doctrine\DBAL\Driver'); + $driver->expects($this->once()) + ->method('getDatabasePlatform') + ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform'))); + + $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver)); + $conn->expects($this->once()) + ->method('getEventManager') + ->will($this->returnValue($this->getMock('Doctrine\Common\EventManager'))); + + $config = $this->getMock('Doctrine\ORM\Configuration'); + $config->expects($this->once()) + ->method('getProxyDir') + ->will($this->returnValue('test')); + + $config->expects($this->once()) + ->method('getProxyNamespace') + ->will($this->returnValue('Proxies')); + + $config->expects($this->once()) + ->method('getMetadataDriverImpl') + ->will($this->returnValue($this->getMock('Doctrine\ORM\Mapping\Driver\DriverChain'))); + + $em = EntityManager::create($conn, $config); + return $em; + } + + /** + * EntityManager mock object together with + * annotation mapping driver + * + * @return EntityManager + */ + protected function getMockAnnotationReaderEntityManager() + { + $driver = $this->getMock('Doctrine\DBAL\Driver'); + $driver->expects($this->once()) + ->method('getDatabasePlatform') + ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform'))); + + $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver)); + $conn->expects($this->once()) + ->method('getEventManager') + ->will($this->returnValue($this->getMock('Doctrine\Common\EventManager'))); + + $config = $this->getMock('Doctrine\ORM\Configuration'); + $config->expects($this->once()) + ->method('getProxyDir') + ->will($this->returnValue('test')); + + $config->expects($this->once()) + ->method('getProxyNamespace') + ->will($this->returnValue('Proxies')); + + $reader = new \Doctrine\Common\Annotations\AnnotationReader(); + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + $mappingDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( + $reader, + __DIR__ . '/TestEntity' + ); + + $config->expects($this->any()) + ->method('getMetadataDriverImpl') + ->will($this->returnValue($mappingDriver)); + + $em = EntityManager::create($conn, $config); + return $em; + } + + /** + * EntityManager mock object together with + * annotation mapping driver and pdo_sqlite + * database in memory + * + * @return EntityManager + */ + protected function getMockSqliteEntityManager() + { + $conn = array( + 'driver' => 'pdo_sqlite', + 'memory' => true, + ); + + $config = $this->getMock('Doctrine\ORM\Configuration'); + $config->expects($this->once()) + ->method('getProxyDir') + ->will($this->returnValue(sys_get_temp_dir())); + + $config->expects($this->once()) + ->method('getProxyNamespace') + ->will($this->returnValue('Proxy')); + + $config->expects($this->once()) + ->method('getAutoGenerateProxyClasses') + ->will($this->returnValue(true)); + + $reader = new \Doctrine\Common\Annotations\AnnotationReader(); + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + $mappingDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader); + + $config->expects($this->any()) + ->method('getMetadataDriverImpl') + ->will($this->returnValue($mappingDriver)); + + $em = EntityManager::create($conn, $config); + return $em; + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/DependentFixtureTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/DependentFixtureTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,384 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +use Doctrine\Common\DataFixtures\Loader; +use Doctrine\Common\DataFixtures\DependentFixtureInterface; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\DataFixtures\FixtureInterface; + +require_once __DIR__.'/TestInit.php'; + +/** + * Test Fixture ordering by dependencies. + * + * @author Gustavo Adrian + */ +class DependentFixtureTest extends BaseTest +{ + public function test_orderFixturesByDependencies_orderClassesWithASingleParent() + { + $loader = new Loader(); + $loader->addFixture(new DependentFixture3); + $loader->addFixture(new DependentFixture1); + $loader->addFixture(new DependentFixture2); + $loader->addFixture(new BaseParentFixture1); + + $orderedFixtures = $loader->getFixtures(); + + $this->assertEquals(4, count($orderedFixtures)); + + $this->assertTrue(array_shift($orderedFixtures) instanceof BaseParentFixture1); + $this->assertTrue(array_shift($orderedFixtures) instanceof DependentFixture1); + $this->assertTrue(array_shift($orderedFixtures) instanceof DependentFixture2); + $this->assertTrue(array_shift($orderedFixtures) instanceof DependentFixture3); + } + + public function test_orderFixturesByDependencies_orderClassesWithAMultipleParents() + { + $loader = new Loader(); + + $addressFixture = new AddressFixture(); + $contactMethodFixture = new ContactMethodFixture(); + $contactFixture = new ContactFixture(); + $baseParentFixture = new BaseParentFixture1(); + $countryFixture = new CountryFixture(); + $stateFixture = new StateFixture(); + + $loader->addFixture($addressFixture); + $loader->addFixture($contactMethodFixture); + $loader->addFixture($contactFixture); + $loader->addFixture($baseParentFixture); + $loader->addFixture($countryFixture); + $loader->addFixture($stateFixture); + + $orderedFixtures = $loader->getFixtures(); + + $this->assertEquals(6, count($orderedFixtures)); + + $contactFixtureOrder = array_search($contactFixture, $orderedFixtures); + $contactMethodFixtureOrder = array_search($contactMethodFixture, $orderedFixtures); + $addressFixtureOrder = array_search($addressFixture, $orderedFixtures); + $countryFixtureOrder = array_search($countryFixture, $orderedFixtures); + $stateFixtureOrder = array_search($stateFixture, $orderedFixtures); + $baseParentFixtureOrder = array_search($baseParentFixture, $orderedFixtures); + + // Order of fixtures is not exact. We need to test, however, that dependencies are + // indeed satisfied + + // BaseParentFixture1 has no dependencies, so it will always be first in this case + $this->assertEquals($baseParentFixtureOrder, 0); + + $this->assertTrue(($contactFixtureOrder > $contactMethodFixtureOrder)); + $this->assertTrue(($contactFixtureOrder > $addressFixtureOrder)); + $this->assertTrue(($contactFixtureOrder > $countryFixtureOrder)); + $this->assertTrue(($contactFixtureOrder > $stateFixtureOrder)); + $this->assertTrue(($contactFixtureOrder > $contactMethodFixtureOrder)); + + $this->assertTrue(($addressFixtureOrder > $stateFixtureOrder)); + $this->assertTrue(($addressFixtureOrder > $countryFixtureOrder)); + } + + + /** + * @expectedException Doctrine\Common\DataFixtures\Exception\CircularReferenceException + */ + public function test_orderFixturesByDependencies_circularReferencesMakeMethodThrowCircularReferenceException() + { + $loader = new Loader(); + + $loader->addFixture(new CircularReferenceFixture3); + $loader->addFixture(new CircularReferenceFixture); + $loader->addFixture(new CircularReferenceFixture2); + + $orderedFixtures = $loader->getFixtures(); + } + + /** + * @expectedException InvalidArgumentException + */ + public function test_orderFixturesByDependencies_fixturesCantHaveItselfAsParent() + { + $loader = new Loader(); + + $loader->addFixture(new FixtureWithItselfAsParent); + + $orderedFixtures = $loader->getFixtures(); + } + + public function test_inCaseThereAreFixturesOrderedByNumberAndByDependenciesBothOrdersAreExecuted() + { + $loader = new Loader(); + $loader->addFixture(new OrderedByNumberFixture1); + $loader->addFixture(new OrderedByNumberFixture3); + $loader->addFixture(new OrderedByNumberFixture2); + $loader->addFixture(new DependentFixture3); + $loader->addFixture(new DependentFixture1); + $loader->addFixture(new DependentFixture2); + $loader->addFixture(new BaseParentFixture1); + + $orderedFixtures = $loader->getFixtures(); + + $this->assertEquals(7, count($orderedFixtures)); + + $this->assertTrue(array_shift($orderedFixtures) instanceof OrderedByNumberFixture1); + $this->assertTrue(array_shift($orderedFixtures) instanceof OrderedByNumberFixture2); + $this->assertTrue(array_shift($orderedFixtures) instanceof OrderedByNumberFixture3); + $this->assertTrue(array_shift($orderedFixtures) instanceof BaseParentFixture1); + $this->assertTrue(array_shift($orderedFixtures) instanceof DependentFixture1); + $this->assertTrue(array_shift($orderedFixtures) instanceof DependentFixture2); + $this->assertTrue(array_shift($orderedFixtures) instanceof DependentFixture3); + } + + /** + * @expectedException RuntimeException + */ + public function test_inCaseAFixtureHasAnUnexistenDependencyOrIfItWasntLoaded_throwsException() + { + $loader = new Loader(); + $loader->addFixture(new FixtureWithUnexistentDependency); + + $orderedFixtures = $loader->getFixtures(); + } +} + +class DependentFixture1 implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( 'Doctrine\Tests\Common\DataFixtures\BaseParentFixture1' ); + } +} + +class DependentFixture2 implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( 'Doctrine\Tests\Common\DataFixtures\DependentFixture1' ); + } +} + +class DependentFixture3 implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( 'Doctrine\Tests\Common\DataFixtures\DependentFixture2' ); + } +} + +class BaseParentFixture1 implements FixtureInterface +{ + public function load($manager) + {} +} + +class CountryFixture implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\BaseParentFixture1' + ); + } +} + +class StateFixture implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\BaseParentFixture1', + 'Doctrine\Tests\Common\DataFixtures\CountryFixture' + ); + } +} + +class AddressFixture implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\BaseParentFixture1', + 'Doctrine\Tests\Common\DataFixtures\CountryFixture', + 'Doctrine\Tests\Common\DataFixtures\StateFixture' + ); + } +} + +class ContactMethodFixture implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\BaseParentFixture1' + ); + } +} + +class ContactFixture implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\AddressFixture', + 'Doctrine\Tests\Common\DataFixtures\ContactMethodFixture' + ); + } +} + +class CircularReferenceFixture implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\CircularReferenceFixture3' + ); + } +} + +class CircularReferenceFixture2 implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\CircularReferenceFixture' + ); + } +} + +class CircularReferenceFixture3 implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\CircularReferenceFixture2' + ); + } +} + +class FixtureWithItselfAsParent implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\FixtureWithItselfAsParent' + ); + } +} + +class FixtureWithUnexistentDependency implements FixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getDependencies() + { + return array( + 'UnexistentDependency' + ); + } +} + +class FixtureImplementingBothOrderingInterfaces implements FixtureInterface, OrderedFixtureInterface, DependentFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 1; + } + + public function getDependencies() + { + return array( + 'Doctrine\Tests\Common\DataFixtures\FixtureWithItselfAsParent' + ); + } +} + +class OrderedByNumberFixture1 implements FixtureInterface, OrderedFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 1; + } +} + +class OrderedByNumberFixture2 implements FixtureInterface, OrderedFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 5; + } +} + +class OrderedByNumberFixture3 implements FixtureInterface, OrderedFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 10; + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/Executor/ORMExecutorSharedFixtureTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/Executor/ORMExecutorSharedFixtureTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,97 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +require_once __DIR__.'/../TestInit.php'; + +use Doctrine\Common\DataFixtures\Executor\ORMExecutor; +use Doctrine\Common\DataFixtures\Purger\ORMPurger; +use Doctrine\ORM\Proxy\Proxy; +use Doctrine\Tests\Common\DataFixtures\TestEntity\Role; +use Doctrine\Tests\Common\DataFixtures\TestEntity\User; + +/** + * Test referenced fixture execution + * + * @author Gediminas Morkevicius + */ +class ORMExecutorSharedFixtureTest extends BaseTest +{ + const TEST_ENTITY_ROLE = 'Doctrine\Tests\Common\DataFixtures\TestEntity\Role'; + const TEST_ENTITY_USER = 'Doctrine\Tests\Common\DataFixtures\TestEntity\User'; + + public function testFixtureExecution() + { + $em = $this->getMockAnnotationReaderEntityManager(); + $purger = new ORMPurger(); + $executor = new ORMExecutor($em, $purger); + + $referenceRepository = $executor->getReferenceRepository(); + $fixture = $this->getMockFixture(); + $fixture->expects($this->once()) + ->method('load') + ->with($em); + + $fixture->expects($this->once()) + ->method('setReferenceRepository') + ->with($referenceRepository); + + $executor->execute(array($fixture), true); + } + + public function testSharedFixtures() + { + if (!extension_loaded('pdo_sqlite')) { + $this->markTestSkipped('Missing pdo_sqlite extension.'); + } + + $em = $this->getMockSqliteEntityManager(); + $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em); + $schemaTool->dropSchema(array()); + $schemaTool->createSchema(array( + $em->getClassMetadata(self::TEST_ENTITY_ROLE), + $em->getClassMetadata(self::TEST_ENTITY_USER) + )); + + $purger = new ORMPurger(); + $executor = new ORMExecutor($em, $purger); + + $userFixture = new TestFixtures\UserFixture; + $roleFixture = new TestFixtures\RoleFixture; + $executor->execute(array($roleFixture, $userFixture), true); + + $referenceRepository = $executor->getReferenceRepository(); + $references = $referenceRepository->getReferences(); + + $this->assertEquals(2, count($references)); + $roleReference = $referenceRepository->getReference('admin-role'); + $this->assertTrue($roleReference instanceof Role); + $this->assertEquals('admin', $roleReference->getName()); + + $userReference = $referenceRepository->getReference('admin'); + $this->assertTrue($userReference instanceof User); + $this->assertEquals('admin@example.com', $userReference->getEmail()); + } + + private function getMockFixture() + { + return $this->getMock('Doctrine\Common\DataFixtures\SharedFixtureInterface'); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/Executor/ORMExecutorTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/Executor/ORMExecutorTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,88 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures\Executor; + +require_once __DIR__.'/../TestInit.php'; + +use Doctrine\ORM\EntityManager; +use Doctrine\Common\DataFixtures\Executor\ORMExecutor; +use PHPUnit_Framework_TestCase; +use Doctrine\Tests\Common\DataFixtures\BaseTest; + +/** + * Test Fixture executor. + * + * @author Jonathan H. Wage + */ +class ORMExecutorTest extends BaseTest +{ + public function testExecuteWithNoPurge() + { + $em = $this->getMockEntityManager(); + $purger = $this->getMockPurger(); + $purger->expects($this->once()) + ->method('setEntityManager') + ->with($em); + $executor = new ORMExecutor($em, $purger); + $fixture = $this->getMockFixture($em); + $fixture->expects($this->once()) + ->method('load') + ->with($em); + $executor->execute(array($fixture), true); + } + + public function testExecuteWithPurge() + { + $em = $this->getMockEntityManager(); + $purger = $this->getMockPurger(); + $purger->expects($this->once()) + ->method('purge') + ->will($this->returnValue(null)); + $executor = new ORMExecutor($em, $purger); + $fixture = $this->getMockFixture($em); + $fixture->expects($this->once()) + ->method('load') + ->with($em); + $executor->execute(array($fixture), false); + } + + public function testExecuteTransaction() + { + $em = $this->getMockEntityManager(); + $executor = new ORMExecutor($em); + $fixture = $this->getMockFixture($em); + $executor->execute(array($fixture), true); + } + + public function testSharedFixtureExecution() + { + $em = $this->getMockEntityManager(); + } + + private function getMockFixture($em) + { + return $this->getMock('Doctrine\Common\DataFixtures\FixtureInterface'); + } + + private function getMockPurger() + { + return $this->getMock('Doctrine\Common\DataFixtures\Purger\ORMPurger'); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/FixtureTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/FixtureTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,51 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +require_once __DIR__.'/TestInit.php'; + +use Doctrine\Common\DataFixtures\FixtureInterface; + +/** + * Test Fixture interface. + * + * @author Jonathan H. Wage + */ +class FixtureTest extends BaseTest +{ + public function testFixtureInterface() + { + $em = $this->getMockEntityManager(); + $fixture = new MyFixture2(); + $fixture->load($em); + + $this->assertTrue($fixture->loaded); + } +} + +class MyFixture2 implements FixtureInterface +{ + public $loaded = false; + + public function load($manager) + { + $this->loaded = true; + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/LoaderTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/LoaderTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,47 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +require_once __DIR__.'/TestInit.php'; + +use Doctrine\Common\DataFixtures\Loader; + +/** + * Test fixtures loader. + * + * @author Jonathan H. Wage + */ +class LoaderTest extends BaseTest +{ + public function testLoader() + { + $loader = new Loader(); + $loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\FixtureInterface'), array(), array(), 'Mock1'); + $loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\FixtureInterface', array(), array(), 'Mock2')); + $loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\SharedFixtureInterface', array(), array(), 'Mock3')); + + $this->assertEquals(3, count($loader->getFixtures())); + + $loader->loadFromDirectory(__DIR__.'/TestFixtures'); + $this->assertEquals(7, count($loader->getFixtures())); + $this->assertTrue($loader->isTransient('TestFixtures\NotAFixture')); + $this->assertFalse($loader->isTransient('TestFixtures\MyFixture1')); + } +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/OrderedFixtureTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/OrderedFixtureTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,89 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +use Doctrine\Common\DataFixtures\Loader; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\DataFixtures\FixtureInterface; + +require_once __DIR__.'/TestInit.php'; + +/** + * Test Fixture ordering. + * + * @author Gediminas Morkevicius + */ +class OrderedFixtureTest extends BaseTest +{ + public function testFixtureOrder() + { + $loader = new Loader(); + $loader->addFixture(new OrderedFixture1); + $loader->addFixture(new OrderedFixture2); + $loader->addFixture(new OrderedFixture3); + $loader->addFixture(new BaseFixture1); + + $orderedFixtures = $loader->getFixtures(); + $this->assertEquals(4, count($orderedFixtures)); + $this->assertTrue($orderedFixtures[0] instanceof BaseFixture1); + $this->assertTrue($orderedFixtures[1] instanceof OrderedFixture2); + $this->assertTrue($orderedFixtures[2] instanceof OrderedFixture1); + $this->assertTrue($orderedFixtures[3] instanceof OrderedFixture3); + } +} + +class OrderedFixture1 implements FixtureInterface, OrderedFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 5; + } +} + +class OrderedFixture2 implements FixtureInterface, OrderedFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 2; + } +} + +class OrderedFixture3 implements FixtureInterface, OrderedFixtureInterface +{ + public function load($manager) + {} + + public function getOrder() + { + return 8; + } +} + +class BaseFixture1 implements FixtureInterface +{ + public function load($manager) + {} +} diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/ReferenceRepositoryTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/ReferenceRepositoryTest.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,113 @@ +. + */ + +namespace Doctrine\Tests\Common\DataFixtures; + +use Doctrine\Common\DataFixtures\ReferenceRepository; +use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener; +use Doctrine\ORM\Tools\SchemaTool; +use Doctrine\ORM\Proxy\Proxy; + +require_once __DIR__.'/TestInit.php'; + +/** + * Test ReferenceRepository. + * + * @author Gediminas Morkevicius + */ +class ReferenceRepositoryTest extends BaseTest +{ + const TEST_ENTITY_ROLE = 'Doctrine\Tests\Common\DataFixtures\TestEntity\Role'; + + public function testReferenceEntry() + { + $em = $this->getMockAnnotationReaderEntityManager(); + $role = new TestEntity\Role; + $role->setName('admin'); + $meta = $em->getClassMetadata(self::TEST_ENTITY_ROLE); + $meta->getReflectionProperty('id')->setValue($role, 1); + + $referenceRepo = new ReferenceRepository($em); + $referenceRepo->addReference('test', $role); + + $references = $referenceRepo->getReferences(); + $this->assertEquals(1, count($references)); + $this->assertArrayHasKey('test', $references); + $this->assertInstanceOf(self::TEST_ENTITY_ROLE, $references['test']); + } + + public function testReferenceIdentityPopulation() + { + $em = $this->getMockSqliteEntityManager(); + $referenceRepository = $this->getMockBuilder('Doctrine\Common\DataFixtures\ReferenceRepository') + ->setConstructorArgs(array($em)) + ->getMock(); + $em->getEventManager()->addEventSubscriber( + new ORMReferenceListener($referenceRepository) + ); + $schemaTool = new SchemaTool($em); + $schemaTool->dropSchema(array()); + $schemaTool->createSchema(array( + $em->getClassMetadata(self::TEST_ENTITY_ROLE) + )); + + $referenceRepository->expects($this->once()) + ->method('addReference') + ->with('admin-role'); + + $referenceRepository->expects($this->once()) + ->method('getReferenceName') + ->will($this->returnValue('admin-role')); + + $referenceRepository->expects($this->once()) + ->method('setReferenceIdentity') + ->with('admin-role', array('id' => 1)); + + $roleFixture = new TestFixtures\RoleFixture; + $roleFixture->setReferenceRepository($referenceRepository); + + $roleFixture->load($em); + } + + public function testReferenceReconstruction() + { + $em = $this->getMockSqliteEntityManager(); + $referenceRepository = new ReferenceRepository($em); + $em->getEventManager()->addEventSubscriber( + new ORMReferenceListener($referenceRepository) + ); + $schemaTool = new SchemaTool($em); + $schemaTool->dropSchema(array()); + $schemaTool->createSchema(array( + $em->getClassMetadata(self::TEST_ENTITY_ROLE) + )); + $roleFixture = new TestFixtures\RoleFixture; + $roleFixture->setReferenceRepository($referenceRepository); + + $roleFixture->load($em); + // first test against managed state + $ref = $referenceRepository->getReference('admin-role'); + $this->assertFalse($ref instanceof Proxy); + + // now test reference reconstruction from identity + $em->clear(); + $ref = $referenceRepository->getReference('admin-role'); + $this->assertTrue($ref instanceof Proxy); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Role.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Role.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,36 @@ +id; + } + + public function setName($name) + { + $this->name = $name; + } + + public function getName() + { + return $this->name; + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/User.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/User.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,76 @@ +id = $id; + } + + public function setCode($code) + { + $this->code = $code; + } + + public function setPassword($password) + { + $this->password = md5($password); + } + + public function getPassword() + { + return $this->password; + } + + public function setEmail($email) + { + $this->email = $email; + } + + public function getEmail() + { + return $this->email; + } + + public function setRole(Role $role) + { + $this->role = $role; + } + + public function getRole() + { + return $this->role; + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestFixtures/MyFixture1.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestFixtures/MyFixture1.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,12 @@ +referenceRepository = $referenceRepository; + } + + public function load($manager) + { + $adminRole = new Role(); + $adminRole->setName('admin'); + + $manager->persist($adminRole); + $this->referenceRepository->addReference('admin-role', $adminRole); + $manager->flush(); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestFixtures/UserFixture.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestFixtures/UserFixture.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,24 @@ +setId(4); + $admin->setCode('007'); + $admin->setEmail('admin@example.com'); + $admin->setPassword('secret'); + $role = $this->getReference('admin-role'); + $admin->setRole($role); + + $manager->persist($admin); + $manager->flush(); + + $this->addReference('admin', $admin); + } +} \ No newline at end of file diff -r b36a42d260d8 -r 624e5900f5a4 vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestInit.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/TestInit.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,20 @@ +register(); + +$classLoader = new ClassLoader('Doctrine\\Tests\\Common\\DataFixtures', __DIR__.'/../../../../../tests'); +$classLoader->register(); + +$classLoader = new ClassLoader('Doctrine\\Common', __DIR__.'/../../../../../lib/vendor/doctrine-common/lib'); +$classLoader->register(); + +$classLoader = new ClassLoader('Doctrine\\DBAL', __DIR__.'/../../../../../lib/vendor/doctrine-dbal/lib'); +$classLoader->register(); + +$classLoader = new ClassLoader('Doctrine\\ORM', __DIR__.'/../../../../../lib/vendor/doctrine-orm/lib'); +$classLoader->register();