From 68e772b271dacd360d669323b36873c2fa76769e Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Tue, 20 Dec 2011 11:08:43 -0800 Subject: [PATCH 1/2] fixed test suite when mongod is not running --- Tests/Form/Type/DocumentTypeTest.php | 12 ++++++++++++ Tests/Security/DocumentUserProviderTest.php | 4 ++-- Tests/TestCase.php | 8 ++++++++ Tests/Validator/Constraints/UniqueValidatorTest.php | 6 +++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Tests/Form/Type/DocumentTypeTest.php b/Tests/Form/Type/DocumentTypeTest.php index a55de8b3..403fa057 100644 --- a/Tests/Form/Type/DocumentTypeTest.php +++ b/Tests/Form/Type/DocumentTypeTest.php @@ -35,6 +35,18 @@ class DocumentTypeTest extends TypeTestCase protected function setUp() { + if (!class_exists('Mongo')) { + $this->markTestSkipped('Mongo PHP/PECL Extension is not available.'); + } + if (!class_exists('Doctrine\\ODM\\MongoDB\\Version')) { + $this->markTestSkipped('Doctrine MongoDB ODM is not available.'); + } + try { + new \Mongo(); + } catch (\MongoException $e) { + $this->markTestSkipped('Unable to connect to Mongo.'); + } + $this->documentManager = TestCase::createTestDocumentManager(); $this->documentManager->createQueryBuilder(self::DOCUMENT_CLASS) ->remove() diff --git a/Tests/Security/DocumentUserProviderTest.php b/Tests/Security/DocumentUserProviderTest.php index 6905ae00..ad2eb80e 100644 --- a/Tests/Security/DocumentUserProviderTest.php +++ b/Tests/Security/DocumentUserProviderTest.php @@ -23,13 +23,13 @@ class DocumentUserProviderTest extends TestCase protected function setUp() { + parent::setUp(); + $this->documentManager = TestCase::createTestDocumentManager(); $this->documentManager->createQueryBuilder(self::DOCUMENT_CLASS) ->remove() ->getQuery() ->execute(); - - parent::setUp(); } public function testRefreshUserGetsUserByIdentifier() diff --git a/Tests/TestCase.php b/Tests/TestCase.php index 16e434e1..fabddc5d 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -20,9 +20,17 @@ class TestCase extends \PHPUnit_Framework_TestCase { protected function setUp() { + if (!class_exists('Mongo')) { + $this->markTestSkipped('Mongo PHP/PECL Extension is not available.'); + } if (!class_exists('Doctrine\\ODM\\MongoDB\\Version')) { $this->markTestSkipped('Doctrine MongoDB ODM is not available.'); } + try { + new \Mongo(); + } catch (\MongoException $e) { + $this->markTestSkipped('Unable to connect to Mongo.'); + } } /** diff --git a/Tests/Validator/Constraints/UniqueValidatorTest.php b/Tests/Validator/Constraints/UniqueValidatorTest.php index 3b93b924..9d4a714c 100644 --- a/Tests/Validator/Constraints/UniqueValidatorTest.php +++ b/Tests/Validator/Constraints/UniqueValidatorTest.php @@ -28,6 +28,8 @@ class UniqueValidatorTest extends TestCase protected function setUp() { + parent::setUp(); + $this->documentManager = $this->createTestDocumentManager(array( __DIR__ . '/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Document' )); @@ -37,7 +39,9 @@ protected function setUp() protected function tearDown() { - $this->dropDocumentCollection(); + if ($this->documentManager) { + $this->dropDocumentCollection(); + } } public function testValidateUniquenessForScalarField() From 37eb3816b1f12ddd3843853d3c0433855cd86f57 Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Tue, 20 Dec 2011 11:33:04 -0800 Subject: [PATCH 2/2] added shutdown function to remove test db --- .../Constraints/UniqueValidatorTest.php | 16 +++------------- Tests/bootstrap.php | 8 ++++++++ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Tests/Validator/Constraints/UniqueValidatorTest.php b/Tests/Validator/Constraints/UniqueValidatorTest.php index 9d4a714c..7b4cce2d 100644 --- a/Tests/Validator/Constraints/UniqueValidatorTest.php +++ b/Tests/Validator/Constraints/UniqueValidatorTest.php @@ -34,14 +34,9 @@ protected function setUp() __DIR__ . '/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Document' )); - $this->dropDocumentCollection(); - } - - protected function tearDown() - { - if ($this->documentManager) { - $this->dropDocumentCollection(); - } + $this->documentManager + ->getDocumentCollection('Symfony\Bundle\DoctrineMongoDBBundle\Tests\Fixtures\Validator\Document') + ->drop(); } public function testValidateUniquenessForScalarField() @@ -223,11 +218,6 @@ public function testEmbeddedDocumentIsNotUnsupported() $violationsList = $validator->validate(new EmbeddedDocument()); } - private function dropDocumentCollection() - { - $this->documentManager->getDocumentCollection('Symfony\Bundle\DoctrineMongoDBBundle\Tests\Fixtures\Validator\Document')->drop(); - } - private function createMockContainer($documentManagerId = self::DEFAULT_DOCUMENT_MANAGER) { $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index 89c827a5..dffc1a27 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -14,3 +14,11 @@ } elseif (file_exists($file = __DIR__.'/autoload.php.dist')) { require_once $file; } + +register_shutdown_function(function() { + try { + $mongo = new Mongo(); + $mongo->doctrine->drop(); + } catch (\MongoException $e) { + } +});