diff --git a/.travis.yml b/.travis.yml index fc33780..babe9ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,15 @@ php: matrix: allow_failures: - php: nightly + - php: hhvm fast_finish: true +services: + - mongodb + +before_install: + - echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + install: - composer self-update - composer install diff --git a/behat.yml b/behat.yml index 66a1b3a..e28a647 100644 --- a/behat.yml +++ b/behat.yml @@ -2,12 +2,18 @@ default: autoload: '': %paths.base%/tests/Features/bootstrap suites: - default: - paths: [ %paths.base%/tests/Features ] + orm_context: + paths: [ %paths.base%/tests/Features/ORM ] contexts: - - FeatureContext + - ORMFeatureContext - Fidry\AliceBundleExtension\Context\Doctrine\AliceORMContext: basePath: %paths.base%/tests/Features/fixtures/ORM + odm_context: + paths: [ %paths.base%/tests/Features/ODM ] + contexts: + - ODMFeatureContext + - Fidry\AliceBundleExtension\Context\Doctrine\AliceODMContext: + basePath: %paths.base%/tests/Features/fixtures/ODM extensions: Behat\Symfony2Extension: diff --git a/composer.json b/composer.json index 4001b5f..48fe6f2 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "symfony/expression-language": "^2.7", "symfony/framework-bundle": "~2.1", "symfony/yaml": "~2.1", - "theofidry/psysh-bundle": "~1.1" + "theofidry/psysh-bundle": "~1.1", + "doctrine/mongodb-odm-bundle": "~3.0" }, "autoload": { "psr-4": { diff --git a/src/Context/Doctrine/AliceODMContext.php b/src/Context/Doctrine/AliceODMContext.php index 5edb34d..b16483b 100644 --- a/src/Context/Doctrine/AliceODMContext.php +++ b/src/Context/Doctrine/AliceODMContext.php @@ -11,6 +11,7 @@ namespace Fidry\AliceBundleExtension\Context\Doctrine; +use Doctrine\ODM\MongoDB\SchemaManager; use Symfony\Component\HttpKernel\KernelInterface; /** @@ -20,6 +21,11 @@ */ class AliceODMContext extends AbstractAliceContext { + /** + * @var SchemaManager + */ + private $schemaManager; + /** * {@inheritdoc} */ @@ -29,9 +35,11 @@ public function setKernel(KernelInterface $kernel) $kernel, $kernel->getContainer()->get('hautelook_alice.doctrine.mongodb.fixtures_finder'), $kernel->getContainer()->get('hautelook_alice.fixtures.loader'), - $this->resolvePersister($kernel->getContainer()->get('doctrine.mongodb.entity_manager')) + $this->resolvePersister($kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager')) ); + $this->schemaManager = $kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager')->getSchemaManager(); + return $this; } @@ -40,7 +48,8 @@ public function setKernel(KernelInterface $kernel) */ public function createSchema() { - // TODO: Implement createDatabase() method. + $this->schemaManager->createCollections(); + $this->schemaManager->ensureIndexes(); } /** @@ -48,7 +57,8 @@ public function createSchema() */ public function dropSchema() { - // TODO: Implement dropDatabase() method. + $this->schemaManager->deleteIndexes(); + $this->schemaManager->dropCollections(); } /** @@ -56,6 +66,7 @@ public function dropSchema() */ public function emptyDatabase() { - // TODO: Implement emptyDatabase() method. + $this->dropSchema(); + $this->createSchema(); } } diff --git a/tests/Features/ODM/loader.feature b/tests/Features/ODM/loader.feature new file mode 100644 index 0000000..65d056d --- /dev/null +++ b/tests/Features/ODM/loader.feature @@ -0,0 +1,42 @@ +Feature: Test Doctrine ODM context + + Scenario: Emptying the database + Given the database is empty + Given there is 10 "dummy" entities + Then the database should contain 10 "dummy" entities + When I empty the database + Then the database should be empty + + Scenario: Loads a fixtures file with @Bundlename notation + Given the database is empty + Given the fixtures file "@TestBundle/DataFixtures/ODM/dummy.yml" is loaded + Then the database should contain 10 "dummy" entities + + Scenario: Loads a fixture file based on basePath + Given the database is empty + Given the fixtures file "another_dummy.yml" is loaded + Then the database should contain 10 "another_dummy" entities + + Scenario: Loads a fixture file with absolute path + Given the database is empty + Given the fixtures file "/home/travis/build/theofidry/AliceBundleExtension/tests/Features/fixtures/ODM/another_dummy.yml" is loaded + Then the database should contain 10 "another_dummy" entities + + Scenario: Loads a fixture file with a custom persister + Given the database is empty + Given the fixtures file "another_dummy.yml" is loaded with the persister "doctrine_mongodb.odm.default_document_manager" + Then the database should contain 10 "another_dummy" entities + + Scenario: Loads several fixtures files based on basePath + Given the database is empty + Given the following fixtures files are loaded: + | another_dummy.yml | + | one_another_dummy.yml | + Then the database should contain 11 "another_dummy" entities + + Scenario: Loads several fixtures files with @Bundlename notation + Given the database is empty + Given the following fixtures files are loaded: + | @TestBundle/DataFixtures/ODM/dummy.yml | + | @TestBundle/DataFixtures/ODM/one_another_dummy.yml | + Then the database should contain 11 "dummy" entities diff --git a/tests/Features/loader.feature b/tests/Features/ORM/loader.feature similarity index 100% rename from tests/Features/loader.feature rename to tests/Features/ORM/loader.feature diff --git a/tests/Features/bootstrap/ODMFeatureContext.php b/tests/Features/bootstrap/ODMFeatureContext.php new file mode 100644 index 0000000..52404cb --- /dev/null +++ b/tests/Features/bootstrap/ODMFeatureContext.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Behat\Symfony2Extension\Context\KernelAwareContext; +use Doctrine\ORM\EntityManager; +use Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\AnotherDummy; +use Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\Dummy; +use PHPUnit_Framework_Assert as PHPUnit; +use Symfony\Component\HttpKernel\KernelInterface; + +/** + * Class FeatureContext. + * + * @author Théo FIDRY + */ +class ODMFeatureContext implements KernelAwareContext +{ + /** + * @var EntityManager + */ + private $entityManager; + + /** + * Sets Kernel instance. + * + * @param KernelInterface $kernel + */ + public function setKernel(KernelInterface $kernel) + { + $this->entityManager = $kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager'); + } + + /** + * @Then /^the database should be empty$/ + */ + public function theDatabaseShouldBeEmpty() + { + $entities = array_merge( + $this->entityManager->getRepository('TestBundle:Dummy')->findAll(), + $this->entityManager->getRepository('TestBundle:AnotherDummy')->findAll() + ); + + PHPUnit::assertCount(0, $entities); + } + + /** + * @Given /^there is (\d+) "([^"]*)" entities$/ + */ + public function thereIsEntities($nbr, $class) + { + for ($i = 0; $i < $nbr; $i++) { + switch ($class) { + case 'dummy': + $entity = new Dummy(); + $entity->name = sprintf('Dummy %d', $i); + break; + + case 'another_dummy': + $entity = new AnotherDummy(); + $entity->name = sprintf('Dummy %d', $i); + break; + + default: + throw new \UnexpectedValueException(sprintf('Unknown %s entity', $class)); + } + + $this->entityManager->persist($entity); + } + + $this->entityManager->flush(); + $this->entityManager->clear('Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\Dummy'); + $this->entityManager->clear( + 'Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\AnotherDummy' + ); + } + + /** + * @Then /^the database should contain (\d+) "([^"]*)" entities$/ + */ + public function thereShouldBeEntities($nbr, $class) + { + switch ($class) { + case 'dummy': + $repository = $this->entityManager->getRepository('TestBundle:Dummy'); + break; + + case 'another_dummy': + $repository = $this->entityManager->getRepository('TestBundle:AnotherDummy'); + break; + + default: + throw new \UnexpectedValueException(sprintf('Unknown %s entity', $class)); + } + + PHPUnit::assertCount((int) $nbr, $repository->findAll()); + } +} diff --git a/tests/Features/bootstrap/FeatureContext.php b/tests/Features/bootstrap/ORMFeatureContext.php similarity index 98% rename from tests/Features/bootstrap/FeatureContext.php rename to tests/Features/bootstrap/ORMFeatureContext.php index 00aaa22..2c3132c 100644 --- a/tests/Features/bootstrap/FeatureContext.php +++ b/tests/Features/bootstrap/ORMFeatureContext.php @@ -21,7 +21,7 @@ * * @author Théo FIDRY */ -class FeatureContext implements KernelAwareContext +class ORMFeatureContext implements KernelAwareContext { /** * @var EntityManager diff --git a/tests/Features/fixtures/ODM/another_dummy.yml b/tests/Features/fixtures/ODM/another_dummy.yml new file mode 100644 index 0000000..b29bd88 --- /dev/null +++ b/tests/Features/fixtures/ODM/another_dummy.yml @@ -0,0 +1,3 @@ +Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\AnotherDummy: + dummy_{0..9}: + name: Dummy diff --git a/tests/Features/fixtures/ODM/one_another_dummy.yml b/tests/Features/fixtures/ODM/one_another_dummy.yml new file mode 100644 index 0000000..5732cac --- /dev/null +++ b/tests/Features/fixtures/ODM/one_another_dummy.yml @@ -0,0 +1,3 @@ +Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\AnotherDummy: + dummy_{10}: + name: Dummy diff --git a/tests/Functional/AppKernel.php b/tests/Functional/AppKernel.php index 20ec98b..6b0c0f5 100644 --- a/tests/Functional/AppKernel.php +++ b/tests/Functional/AppKernel.php @@ -10,6 +10,7 @@ */ use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; +use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle; use Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\TestBundle; use Fidry\PsyshBundle\PsyshBundle; use Hautelook\AliceBundle\HautelookAliceBundle; @@ -27,6 +28,7 @@ public function registerBundles() new HautelookAliceBundle(), new TestBundle(), new PsyshBundle(), + new DoctrineMongoDBBundle(), ]; } diff --git a/tests/Functional/Bundle/TestBundle/DataFixtures/ODM/dummy.yml b/tests/Functional/Bundle/TestBundle/DataFixtures/ODM/dummy.yml new file mode 100644 index 0000000..9ac2861 --- /dev/null +++ b/tests/Functional/Bundle/TestBundle/DataFixtures/ODM/dummy.yml @@ -0,0 +1,3 @@ +Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\Dummy: + dummy_{0..9}: + name: Dummy diff --git a/tests/Functional/Bundle/TestBundle/DataFixtures/ODM/one_another_dummy.yml b/tests/Functional/Bundle/TestBundle/DataFixtures/ODM/one_another_dummy.yml new file mode 100644 index 0000000..ac6613e --- /dev/null +++ b/tests/Functional/Bundle/TestBundle/DataFixtures/ODM/one_another_dummy.yml @@ -0,0 +1,3 @@ +Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\Dummy: + dummy_{10}: + name: Dummy diff --git a/tests/Functional/Bundle/TestBundle/Document/AnotherDummy.php b/tests/Functional/Bundle/TestBundle/Document/AnotherDummy.php new file mode 100644 index 0000000..d5f830f --- /dev/null +++ b/tests/Functional/Bundle/TestBundle/Document/AnotherDummy.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document; + +use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; + +/** + * @MongoDB\Document + */ +class AnotherDummy +{ + /** + * @var int + * + * @MongoDB\Id() + */ + public $id; + + /** + * @var string + * + * @MongoDB\String() + */ + public $name; +} diff --git a/tests/Functional/Bundle/TestBundle/Document/Dummy.php b/tests/Functional/Bundle/TestBundle/Document/Dummy.php new file mode 100644 index 0000000..91397a1 --- /dev/null +++ b/tests/Functional/Bundle/TestBundle/Document/Dummy.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document; + +use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; + +/** + * @MongoDB\Document + */ +class Dummy +{ + /** + * @var int + * + * @MongoDB\Id() + */ + public $id; + + /** + * @var string + * + * @MongoDB\String() + */ + public $name; +} diff --git a/tests/Functional/config/config.yml b/tests/Functional/config/config.yml index 1f73bfa..8f0b2ad 100644 --- a/tests/Functional/config/config.yml +++ b/tests/Functional/config/config.yml @@ -16,3 +16,13 @@ doctrine: driver: pdo_sqlite path: %kernel.cache_dir%/db.sqlite charset: UTF8 + +doctrine_mongodb: + connections: + default: + server: mongodb://localhost:27017 + options: {} + default_database: test_database + document_managers: + default: + auto_mapping: true