Skip to content

Commit

Permalink
Merge pull request #3 from zumba/phpunit-4-support
Browse files Browse the repository at this point in the history
Added PHPUnit 4 support for the mongo test trait.
  • Loading branch information
jrbasso committed Mar 31, 2014
2 parents f8e840f + ed721e1 commit b6d25b6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,59 @@ 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
<?php

class MyMongoTestCase extends \PHPUnit_Framework_TestCase {
use \Zumba\PHPUnit\Extensions\Mongo\TestTrait;

/**
* Get the mongo connection for this test.
*
* @return Zumba\PHPUnit\Extensions\Mongo\Client\Connector
*/
public function getMongoConnection() {
return new \MongoClient();
}

/**
* Get the dataset to be used for this test.
*
* @return Zumba\PHPUnit\Extensions\Mongo\DataSet\DataSet
*/
public function getMongoDataSet() {
$dataset = new \Zumba\PHPUnit\Extensions\Mongo\DataSet\DataSet($this->getMongoConnection());
$dataset->setFixture([
'some_collection' => [
['name' => 'Document 1'],
['name' => 'Document 2']
]
]);
return $dataset;
}

public function testRead() {
$result = $this->getMongoConnection()->test->some_collection->findOne(['name' => 'Document 2']);
$this->assertEquals('Document 2', $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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"ext-mongo": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "~4.0"
},
"config": {
"bin-dir": "bin"
Expand Down
6 changes: 4 additions & 2 deletions src/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +25,9 @@ public function setUp() {
* Cleanup after test.
*
* @return void
* @after
*/
public function tearDown() {
public function mongoTearDown() {
$this->getMongoDataSet()->dropAllCollections();
}

Expand Down

0 comments on commit b6d25b6

Please sign in to comment.