Skip to content
This repository has been archived by the owner on Sep 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from caciobanu/master
Browse files Browse the repository at this point in the history
Finished implementation of AliceODMContext:
  • Loading branch information
theofidry committed Mar 16, 2016
2 parents dfcb439 + 7a151d3 commit 07ac254
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
19 changes: 15 additions & 4 deletions src/Context/Doctrine/AliceODMContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Fidry\AliceBundleExtension\Context\Doctrine;

use Doctrine\ODM\MongoDB\SchemaManager;
use Symfony\Component\HttpKernel\KernelInterface;

/**
Expand All @@ -20,6 +21,11 @@
*/
class AliceODMContext extends AbstractAliceContext
{
/**
* @var SchemaManager
*/
private $schemaManager;

/**
* {@inheritdoc}
*/
Expand All @@ -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;
}

Expand All @@ -40,22 +48,25 @@ public function setKernel(KernelInterface $kernel)
*/
public function createSchema()
{
// TODO: Implement createDatabase() method.
$this->schemaManager->createCollections();
$this->schemaManager->ensureIndexes();
}

/**
* {@inheritdoc}
*/
public function dropSchema()
{
// TODO: Implement dropDatabase() method.
$this->schemaManager->deleteIndexes();
$this->schemaManager->dropCollections();
}

/**
* {@inheritdoc}
*/
public function emptyDatabase()
{
// TODO: Implement emptyDatabase() method.
$this->dropSchema();
$this->createSchema();
}
}
42 changes: 42 additions & 0 deletions tests/Features/ODM/loader.feature
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
105 changes: 105 additions & 0 deletions tests/Features/bootstrap/ODMFeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the Fidry\AliceBundleExtension package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* 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 <theo.fidry@gmail.com>
*/
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Théo FIDRY <theo.fidry@gmail.com>
*/
class FeatureContext implements KernelAwareContext
class ORMFeatureContext implements KernelAwareContext
{
/**
* @var EntityManager
Expand Down
3 changes: 3 additions & 0 deletions tests/Features/fixtures/ODM/another_dummy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\AnotherDummy:
dummy_{0..9}:
name: Dummy <current()>
3 changes: 3 additions & 0 deletions tests/Features/fixtures/ODM/one_another_dummy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\AnotherDummy:
dummy_{10}:
name: Dummy <current()>
2 changes: 2 additions & 0 deletions tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@ public function registerBundles()
new HautelookAliceBundle(),
new TestBundle(),
new PsyshBundle(),
new DoctrineMongoDBBundle(),
];
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/Bundle/TestBundle/DataFixtures/ODM/dummy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\Dummy:
dummy_{0..9}:
name: Dummy <current()>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Document\Dummy:
dummy_{10}:
name: Dummy <current()>
34 changes: 34 additions & 0 deletions tests/Functional/Bundle/TestBundle/Document/AnotherDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Fidry\AliceBundleExtension package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* 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;
}
34 changes: 34 additions & 0 deletions tests/Functional/Bundle/TestBundle/Document/Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Fidry\AliceBundleExtension package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* 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;
}
10 changes: 10 additions & 0 deletions tests/Functional/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 07ac254

Please sign in to comment.