From 5a2973273d6b20b49d1240f70d797cb00ccc5a72 Mon Sep 17 00:00:00 2001 From: Chris Saylor Date: Sun, 30 Mar 2014 16:00:57 -0400 Subject: [PATCH 1/2] Added PHPUnit 4 support for the mongo test trait. --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++- composer.json | 2 +- src/TestTrait.php | 6 ++++-- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5f56113..db53f7c 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,62 @@ Mongounit is a PHPUnit extension for test cases that utilize MongoDB as their da ## Requirements * PHP 5.3+ +* PHPUnit ~3.7, ~4.0 * PECL mongo 1.3+ ## Testing -1. Clone repo. 1. Install dependencies `composer install -dev` 1. Run `./bin/phpunit` + +## Example use + +```php +getMongoConnection()); + $dataset->setFixture([ + 'some_collection' => [ + ['name' => 'Document 1'], + ['name' => 'Document 2'] + ] + ]); + return $dataset; + } + + public function testRead() { + $expected = [ + 'name' => 'Document 1' + ]; + $result = $this->getMongoConnection()->test->some_collection->findOne(['name' => 'Document 2']); + $this->assertEquals('Document 1', $result['name']); + } + +} +``` + +[See full working example.](https://github.com/zumba/mongounit/blob/master/examples/PizzaTraitTest.php) + +## Note about PHP and PHPUnit Versions + +PHP 5.3 is supported for PHPUnit ~3.7 by way of extending `\Zumba\PHPUnit\Extensions\Mongo\TestCase`. PHPUnit 4 is working with this testcase, however it is not actively supported. + +PHP 5.4 is supported via use of the `\Zumba\PHPUnit\Extensions\Mongo\TestTrait` trait. It currently is supporting PHPUnit 4 `@before` and `@after` but can be used in PHPUnit ~3.7 by either aliasing the `mongoSetUp` and `mongoTearDown` to `setUp` and `tearDown`, or by calling `mongoSetUp` and `mongoTearDown` in your respective methods. diff --git a/composer.json b/composer.json index ead5cad..8c9456e 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "ext-mongo": "*" }, "require-dev": { - "phpunit/phpunit": "3.7.*" + "phpunit/phpunit": "~4.0" }, "config": { "bin-dir": "bin" diff --git a/src/TestTrait.php b/src/TestTrait.php index 48a95ff..22ddcb1 100644 --- a/src/TestTrait.php +++ b/src/TestTrait.php @@ -9,8 +9,9 @@ trait TestTrait { * Setup the mongo db with fixture data. * * @return void + * @before */ - public function setUp() { + public function mongoSetUp() { if (!class_exists('MongoClient')) { $this->markTestSkipped('The Mongo extension is not available.'); return; @@ -24,8 +25,9 @@ public function setUp() { * Cleanup after test. * * @return void + * @after */ - public function tearDown() { + public function mongoTearDown() { $this->getMongoDataSet()->dropAllCollections(); } From ed721e1a9b64c2308568ae9c2e4a4f04b1d5eb0c Mon Sep 17 00:00:00 2001 From: Chris Saylor Date: Sun, 30 Mar 2014 16:04:12 -0400 Subject: [PATCH 2/2] Updated example test. --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index db53f7c..150b0a9 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,8 @@ class MyMongoTestCase extends \PHPUnit_Framework_TestCase { } public function testRead() { - $expected = [ - 'name' => 'Document 1' - ]; $result = $this->getMongoConnection()->test->some_collection->findOne(['name' => 'Document 2']); - $this->assertEquals('Document 1', $result['name']); + $this->assertEquals('Document 2', $result['name']); } }