From 4ee56d0e02bed8d526f3084de0616600f8569aea Mon Sep 17 00:00:00 2001 From: Bryan Crowe Date: Thu, 23 Oct 2014 01:15:53 -0400 Subject: [PATCH] Apply PSR2 PHP CS fixer to tests --- tests/CollectionTests.php | 711 ++++---- tests/ConnectionTests.php | 267 ++- tests/CursorTests.php | 98 +- tests/DatabaseTests.php | 425 +++-- tests/FilesystemTests.php | 316 ++-- tests/MongaTests.php | 100 +- tests/QueryAggregateTests.php | 360 ++-- tests/QueryBuilderTests.php | 154 +- tests/QueryFindTests.php | 230 +-- tests/QueryIndexesTests.php | 188 +- tests/QueryRemoveTests.php | 74 +- tests/QueryUpdateTests.php | 362 ++-- tests/QueryWhereTests.php | 3062 ++++++++++++++++----------------- 13 files changed, 3171 insertions(+), 3176 deletions(-) diff --git a/tests/CollectionTests.php b/tests/CollectionTests.php index b723763..d2acc74 100644 --- a/tests/CollectionTests.php +++ b/tests/CollectionTests.php @@ -7,367 +7,366 @@ class CollectionTests extends PHPUnit_Framework_TestCase { - protected $database; - protected $connection; - protected $collection; - - public function setUp() - { - if ( ! $this->connection) - { - $this->connection = Monga::connection(); - } - - $this->database = $this->connection->database('__unit_testing__'); - $this->collection = $this->database->collection('__unit_testing__'); - } - - public function tearDown() - { - $this->database->collection('__unit_testing__')->getCollection()->drop(); - // $this->connection->dropDatabase('__unit_testing__'); - $this->database = null; - } - - public function testGetCollection() - { - $collection = $this->collection->getCollection(); - - $this->assertInstanceOf('MongoCollection', $collection); - } - - public function testSetCollection() - { - $original = $this->collection->getCollection(); - $originalHash = spl_object_hash($original); - $new = $this->database->collection('__different__')->getCollection(); - $newHash = spl_object_hash($new); - $this->collection->setCollection($new); - $reflection = new ReflectionObject($this->collection); - $property = $reflection->getProperty('collection'); - $property->setAccessible(true); - $this->assertInstanceOf('MongoCollection', $property->getValue($this->collection)); - $this->assertEquals($newHash, spl_object_hash($property->getValue($this->collection))); - $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->collection))); - $this->collection->setCollection($original); - } - - public function testCount() - { - $result = $this->collection->count(); - $this->assertEquals(0, $result); - $this->collection->getCollection()->insert(array('this' => 'value')); - $result = $this->collection->count(); - $this->assertEquals(1, $result); - } - - /** + protected $database; + protected $connection; + protected $collection; + + public function setUp() + { + if ( ! $this->connection) { + $this->connection = Monga::connection(); + } + + $this->database = $this->connection->database('__unit_testing__'); + $this->collection = $this->database->collection('__unit_testing__'); + } + + public function tearDown() + { + $this->database->collection('__unit_testing__')->getCollection()->drop(); + // $this->connection->dropDatabase('__unit_testing__'); + $this->database = null; + } + + public function testGetCollection() + { + $collection = $this->collection->getCollection(); + + $this->assertInstanceOf('MongoCollection', $collection); + } + + public function testSetCollection() + { + $original = $this->collection->getCollection(); + $originalHash = spl_object_hash($original); + $new = $this->database->collection('__different__')->getCollection(); + $newHash = spl_object_hash($new); + $this->collection->setCollection($new); + $reflection = new ReflectionObject($this->collection); + $property = $reflection->getProperty('collection'); + $property->setAccessible(true); + $this->assertInstanceOf('MongoCollection', $property->getValue($this->collection)); + $this->assertEquals($newHash, spl_object_hash($property->getValue($this->collection))); + $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->collection))); + $this->collection->setCollection($original); + } + + public function testCount() + { + $result = $this->collection->count(); + $this->assertEquals(0, $result); + $this->collection->getCollection()->insert(array('this' => 'value')); + $result = $this->collection->count(); + $this->assertEquals(1, $result); + } + + /** * @expectedException InvalidArgumentException */ - public function testCountException() - { - $this->collection->count(false); - } - - public function testCountWhere() - { - $where = new League\Monga\Query\Where(); - $where->where('name', 'Frank'); - $result = $this->collection->count($where); - $this->assertEquals(0, $result); - $this->collection->getCollection()->insert(array('name' => 'Frank')); - $result = $this->collection->count($where); - $this->assertEquals(1, $result); - } - - public function testCountClosure() - { - $where = function($query){ - $query->where('name', 'Frank'); - }; - $result = $this->collection->count($where); - $this->assertEquals(0, $result); - $this->collection->getCollection()->insert(array('name' => 'Frank')); - $result = $this->collection->count($where); - $this->assertEquals(1, $result); - } - - public function testDrop() - { - $result = $this->collection->drop(); - $this->assertFalse($result); - $this->collection->insert(array('name' => 'Frank')); - $result = $this->collection->drop(); - $this->assertTrue($result); - } - - public function testTruncate() - { - $result = $this->collection->truncate(); - $this->assertTrue($result); - } - - public function testRemove() - { - $result = $this->collection->remove(array()); - $this->assertTrue($result); - } - - public function testRemoveWhere() - { - $this->collection->getCollection()->insert(array('name' => 'Frank')); - $this->assertEquals(1, $this->collection->count()); - $result = $this->collection->remove(array('name' => 'Bert')); - $this->assertTrue($result); - $this->assertEquals(1, $this->collection->count()); - $result = $this->collection->remove(array('name' => 'Frank')); - $this->assertTrue($result); - $this->assertEquals(0, $this->collection->count()); - } - - public function testRemoveWhereClosure() - { - $closure = function($query){ - $query->where('name' ,'Frank'); - }; - $closure2 = function($query){ - $query->where('name' ,'Bert'); - }; - $this->collection->getCollection()->insert(array('name' => 'Frank')); - $this->assertEquals(1, $this->collection->count()); - $result = $this->collection->remove($closure2); - $this->assertTrue($result); - $this->assertEquals(1, $this->collection->count()); - $result = $this->collection->remove($closure); - $this->assertTrue($result); - $this->assertEquals(0, $this->collection->count()); - } - - /** + public function testCountException() + { + $this->collection->count(false); + } + + public function testCountWhere() + { + $where = new League\Monga\Query\Where(); + $where->where('name', 'Frank'); + $result = $this->collection->count($where); + $this->assertEquals(0, $result); + $this->collection->getCollection()->insert(array('name' => 'Frank')); + $result = $this->collection->count($where); + $this->assertEquals(1, $result); + } + + public function testCountClosure() + { + $where = function ($query) { + $query->where('name', 'Frank'); + }; + $result = $this->collection->count($where); + $this->assertEquals(0, $result); + $this->collection->getCollection()->insert(array('name' => 'Frank')); + $result = $this->collection->count($where); + $this->assertEquals(1, $result); + } + + public function testDrop() + { + $result = $this->collection->drop(); + $this->assertFalse($result); + $this->collection->insert(array('name' => 'Frank')); + $result = $this->collection->drop(); + $this->assertTrue($result); + } + + public function testTruncate() + { + $result = $this->collection->truncate(); + $this->assertTrue($result); + } + + public function testRemove() + { + $result = $this->collection->remove(array()); + $this->assertTrue($result); + } + + public function testRemoveWhere() + { + $this->collection->getCollection()->insert(array('name' => 'Frank')); + $this->assertEquals(1, $this->collection->count()); + $result = $this->collection->remove(array('name' => 'Bert')); + $this->assertTrue($result); + $this->assertEquals(1, $this->collection->count()); + $result = $this->collection->remove(array('name' => 'Frank')); + $this->assertTrue($result); + $this->assertEquals(0, $this->collection->count()); + } + + public function testRemoveWhereClosure() + { + $closure = function ($query) { + $query->where('name' ,'Frank'); + }; + $closure2 = function ($query) { + $query->where('name' ,'Bert'); + }; + $this->collection->getCollection()->insert(array('name' => 'Frank')); + $this->assertEquals(1, $this->collection->count()); + $result = $this->collection->remove($closure2); + $this->assertTrue($result); + $this->assertEquals(1, $this->collection->count()); + $result = $this->collection->remove($closure); + $this->assertTrue($result); + $this->assertEquals(0, $this->collection->count()); + } + + /** * @expectedException InvalidArgumentException */ - public function testInvalidRemove() - { - $this->collection->remove(false); - } - - public function testListIndexes() - { - $this->assertInternalType('array', $this->collection->listIndexes()); - } - - public function testDistinct() - { - $collection = m::mock('MongoCollection'); - $collection->shouldReceive('distinct') - ->with('surname', array('age' => 25)) - ->once() - ->andReturn(array('randomstring')); - - $expected = array('randomstring'); - $c = new Collection($collection); - $result = $c->distinct('surname', array('age' => 25)); - $this->assertEquals($expected, $result); - } - - public function testDistinctClosure() - { - $collection = m::mock('MongoCollection'); - $collection->shouldReceive('distinct') - ->with('surname', array('age' => 25)) - ->once() - ->andReturn(array('randomstring')); - - $expected = array('randomstring'); - $c = new Collection($collection); - $result = $c->distinct('surname', function($w){ - $w->where('age', 25); - }); - $this->assertEquals($expected, $result); - } - - public function testAggregation() - { - $collection = m::mock('MongoCollection'); - $collection->shouldReceive('aggregate') - ->with(array('randomstring')) - ->once() - ->andReturn(array('randomstring')); - - $expected = array('randomstring'); - $c = new Collection($collection); - $result = $c->aggregate(array('randomstring')); - $this->assertEquals($expected, $result); - } - - public function testAggregationClosure() - { - $collection = m::mock('MongoCollection'); - $collection->shouldReceive('aggregate') - ->with(array( - array('$limit' => 1), - )) - ->once() - ->andReturn(array('randomstring')); - - $expected = array('randomstring'); - $c = new Collection($collection); - $result = $c->aggregate(function($a){ - $a->limit(1); - }); - $this->assertEquals($expected, $result); - } - - public function testIndexes() - { - $result = false; - $callback = function() use(&$result) { - $result = true; - }; - $this->collection->indexes($callback); - - $this->assertTrue($result); - } - - public function testFind() - { - $result = $this->collection->find(); - $this->assertInstanceOf('League\Monga\Cursor', $result); - } - - public function testFindOneEmpty() - { - $result = $this->collection->findOne(); - $this->assertNull($result); - } - - public function testFindOneNotEmpty() - { - $this->collection->insert(array('some' => 'value')); - $result = $this->collection->findOne(); - $this->assertInternalType('array', $result); - $this->assertEquals('value', $result['some']); - } - - public function testFindOneWithPostFindAction() - { - $result = $this->collection->findOne(function($query){ - $query->where('some', 'value') - ->orderBy('some', 'asc') - ->skip(0) - ->limit(1); - }); - - $this->assertNull($result); - } - - public function testFindOneWithPostFindActionWithResult() - { - $this->collection->insert(array('some' => 'value')); - - $result = $this->collection->findOne(function($query){ - $query->where('some', 'value') - ->orderBy('some', 'asc') - ->skip(0) - ->limit(1); - }); - - $this->assertInternalType('array', $result); - $this->assertEquals('value', $result['some']); - } - - /** + public function testInvalidRemove() + { + $this->collection->remove(false); + } + + public function testListIndexes() + { + $this->assertInternalType('array', $this->collection->listIndexes()); + } + + public function testDistinct() + { + $collection = m::mock('MongoCollection'); + $collection->shouldReceive('distinct') + ->with('surname', array('age' => 25)) + ->once() + ->andReturn(array('randomstring')); + + $expected = array('randomstring'); + $c = new Collection($collection); + $result = $c->distinct('surname', array('age' => 25)); + $this->assertEquals($expected, $result); + } + + public function testDistinctClosure() + { + $collection = m::mock('MongoCollection'); + $collection->shouldReceive('distinct') + ->with('surname', array('age' => 25)) + ->once() + ->andReturn(array('randomstring')); + + $expected = array('randomstring'); + $c = new Collection($collection); + $result = $c->distinct('surname', function ($w) { + $w->where('age', 25); + }); + $this->assertEquals($expected, $result); + } + + public function testAggregation() + { + $collection = m::mock('MongoCollection'); + $collection->shouldReceive('aggregate') + ->with(array('randomstring')) + ->once() + ->andReturn(array('randomstring')); + + $expected = array('randomstring'); + $c = new Collection($collection); + $result = $c->aggregate(array('randomstring')); + $this->assertEquals($expected, $result); + } + + public function testAggregationClosure() + { + $collection = m::mock('MongoCollection'); + $collection->shouldReceive('aggregate') + ->with(array( + array('$limit' => 1), + )) + ->once() + ->andReturn(array('randomstring')); + + $expected = array('randomstring'); + $c = new Collection($collection); + $result = $c->aggregate(function ($a) { + $a->limit(1); + }); + $this->assertEquals($expected, $result); + } + + public function testIndexes() + { + $result = false; + $callback = function () use (&$result) { + $result = true; + }; + $this->collection->indexes($callback); + + $this->assertTrue($result); + } + + public function testFind() + { + $result = $this->collection->find(); + $this->assertInstanceOf('League\Monga\Cursor', $result); + } + + public function testFindOneEmpty() + { + $result = $this->collection->findOne(); + $this->assertNull($result); + } + + public function testFindOneNotEmpty() + { + $this->collection->insert(array('some' => 'value')); + $result = $this->collection->findOne(); + $this->assertInternalType('array', $result); + $this->assertEquals('value', $result['some']); + } + + public function testFindOneWithPostFindAction() + { + $result = $this->collection->findOne(function ($query) { + $query->where('some', 'value') + ->orderBy('some', 'asc') + ->skip(0) + ->limit(1); + }); + + $this->assertNull($result); + } + + public function testFindOneWithPostFindActionWithResult() + { + $this->collection->insert(array('some' => 'value')); + + $result = $this->collection->findOne(function ($query) { + $query->where('some', 'value') + ->orderBy('some', 'asc') + ->skip(0) + ->limit(1); + }); + + $this->assertInternalType('array', $result); + $this->assertEquals('value', $result['some']); + } + + /** * @expectedException InvalidArgumentException */ - public function testInvalidFind() - { - $this->collection->find(false); - } - - public function testInsertOne() - { - $result = $this->collection->insert(array('new' => 'entry')); - - $this->assertInstanceOf('MongoId', $result); - } - - public function testInsertMultiple() - { - $result = $this->collection->insert(array( - array('number' => 'one'), - array('number' => 'two'), - )); - - $this->assertCount(2, $result); - $this->assertContainsOnlyInstancesOf('MongoId', $result); - } - - public function testInvalidInsert() - { - $collection = $this->getMockBuilder('MongoCollection') - ->disableOriginalConstructor() - ->setMethods(array('insert')) - ->getMock(); - $collection->expects($this->once()) - ->method('insert') - ->with($this->equalTo(array('invalid'))) - ->will($this->returnValue(false)); - - $this->collection->setCollection($collection); - $result = $this->collection->insert(array('invalid')); - $this->assertFalse($result); - } - - public function testInsertMultipleInvalid() - { - $input = array( - array(false), array(false), - ); - $collection = $this->getMockBuilder('MongoCollection') - ->disableOriginalConstructor() - ->setMethods(array('batchInsert')) - ->getMock(); - $collection->expects($this->once()) - ->method('batchInsert') - ->with($this->equalTo($input)) - ->will($this->returnValue(false)); - - $this->collection->setCollection($collection); - $result = $this->collection->insert(array( - array(false), array(false), - )); - - $this->assertFalse($result); - } - - public function testSave() - { - $item = array('name' => 'Frank'); - $result = $this->collection->save($item); - $this->assertTrue($result); - } - - public function testUpdate() - { - $result = $this->collection->update(array('name' => 'changed')); - $this->assertTrue($result); - } - - public function testUpdateClosure() - { - $result = $this->collection->update(function($query){ - $query->set('name', 'changed') - ->increment('viewcount', 2); - }); - - $this->assertTrue($result); - } - - /** + public function testInvalidFind() + { + $this->collection->find(false); + } + + public function testInsertOne() + { + $result = $this->collection->insert(array('new' => 'entry')); + + $this->assertInstanceOf('MongoId', $result); + } + + public function testInsertMultiple() + { + $result = $this->collection->insert(array( + array('number' => 'one'), + array('number' => 'two'), + )); + + $this->assertCount(2, $result); + $this->assertContainsOnlyInstancesOf('MongoId', $result); + } + + public function testInvalidInsert() + { + $collection = $this->getMockBuilder('MongoCollection') + ->disableOriginalConstructor() + ->setMethods(array('insert')) + ->getMock(); + $collection->expects($this->once()) + ->method('insert') + ->with($this->equalTo(array('invalid'))) + ->will($this->returnValue(false)); + + $this->collection->setCollection($collection); + $result = $this->collection->insert(array('invalid')); + $this->assertFalse($result); + } + + public function testInsertMultipleInvalid() + { + $input = array( + array(false), array(false), + ); + $collection = $this->getMockBuilder('MongoCollection') + ->disableOriginalConstructor() + ->setMethods(array('batchInsert')) + ->getMock(); + $collection->expects($this->once()) + ->method('batchInsert') + ->with($this->equalTo($input)) + ->will($this->returnValue(false)); + + $this->collection->setCollection($collection); + $result = $this->collection->insert(array( + array(false), array(false), + )); + + $this->assertFalse($result); + } + + public function testSave() + { + $item = array('name' => 'Frank'); + $result = $this->collection->save($item); + $this->assertTrue($result); + } + + public function testUpdate() + { + $result = $this->collection->update(array('name' => 'changed')); + $this->assertTrue($result); + } + + public function testUpdateClosure() + { + $result = $this->collection->update(function ($query) { + $query->set('name', 'changed') + ->increment('viewcount', 2); + }); + + $this->assertTrue($result); + } + + /** * @expectedException InvalidArgumentException */ - public function testInvalidUpdate() - { - $result = $this->collection->update(false); - } -} \ No newline at end of file + public function testInvalidUpdate() + { + $result = $this->collection->update(false); + } +} diff --git a/tests/ConnectionTests.php b/tests/ConnectionTests.php index 474f813..8e8853e 100644 --- a/tests/ConnectionTests.php +++ b/tests/ConnectionTests.php @@ -4,137 +4,136 @@ class ConnectionTests extends PHPUnit_Framework_TestCase { - protected $connection; - - public function setUp() - { - if ( ! $this->connection) - { - $this->connection = new Connection(null, array( - 'connect' => true, - )); - } - } - - public function testInjection() - { - $mongo = $this->connection->getConnection(); - $mongoHash = spl_object_hash($mongo); - $connection = new Connection($mongo); - $reflection = new ReflectionObject($connection); - $connectionProperty = $reflection->getProperty('connection'); - $connectionProperty->setAccessible(true); - $this->assertInstanceOf('MongoClient', $connectionProperty->getValue($connection)); - $this->assertEquals($mongoHash, spl_object_hash($connectionProperty->getValue($connection))); - } - - public function testDisconnect() - { - $this->connection->disconnect(); - $this->assertFalse($this->connection->isConnected()); - } - - - public function testReconnectonnect() - { - $this->connection->disconnect(); - $this->assertTrue($this->connection->disconnect()); - $this->assertFalse($this->connection->isConnected()); - $this->connection->connect(); - $this->assertTrue($this->connection->connect()); - $this->assertTrue($this->connection->isConnected()); - } - - - public function testHasDatabase() - { - $this->assertFalse($this->connection->hasDatabase('__unknown__database__')); - $this->assertTrue($this->connection->hasDatabase('admin')); - } - - public function testDatabaseDefaultServer() - { - $connection = new Connection(null); - $host = (string) $connection->getConnection(); - $this->assertEquals('localhost:27017', $host); - } - - public function testDatabaseConfig() - { - $connection = new Connection(array('connect' => true)); - $host = (string) $connection->getConnection(); - $this->assertEquals('localhost:27017', $host); - } - - public function testListDatabases() - { - $list = $this->connection->listDatabases(); - - $this->assertInternalType('array', $list); - $this->assertContains('admin', $list); - - $list = $this->connection->listDatabases(true); - $this->assertInternalType('array', $list); - } - - public function testGetMongoObject() - { - $mongo = $this->connection->getConnection(); - - $this->assertInstanceOf('MongoClient', $mongo); - } - - - public function testDropUnknownDatabase() - { - $result = $this->connection->dropDatabase('_unknown_'); - - $this->assertTrue($result); - } - - - public function testDropKnownDatabase() - { - $mongo = $this->connection->getConnection(); - $mongo->demo->users->insert(array('test' => true)); - - $result = $this->connection->dropDatabase('demo'); - - $this->assertTrue($result); - } - - - public function testGetDatabase() - { - $database = $this->connection->database('my_db'); - $this->assertInstanceOf('League\Monga\Database', $database); - } - - - public function testGetMongoDatabase() - { - $database = $this->connection->database('my_db', false); - $this->assertInstanceOf('MongoDB', $database); - } - - - public function testGetConnection() - { - $mongo = $this->connection->getConnection(); - $this->assertInstanceOf('MongoClient', $mongo); - } - - - public function testReplaceConnection() - { - $original = $this->connection->getConnection(); - $new = new MongoClient(); - $original_hash = spl_object_hash($original); - $new_hash = spl_object_hash($new); - $this->connection->setConnection($new); - $get_hash = spl_object_hash($this->connection->getConnection()); - $this->assertEquals($get_hash, $new_hash); - $this->assertNotEquals($get_hash, $original_hash); - $this->connection->setConnection($original); - } -} \ No newline at end of file + protected $connection; + + public function setUp() + { + if ( ! $this->connection) { + $this->connection = new Connection(null, array( + 'connect' => true, + )); + } + } + + public function testInjection() + { + $mongo = $this->connection->getConnection(); + $mongoHash = spl_object_hash($mongo); + $connection = new Connection($mongo); + $reflection = new ReflectionObject($connection); + $connectionProperty = $reflection->getProperty('connection'); + $connectionProperty->setAccessible(true); + $this->assertInstanceOf('MongoClient', $connectionProperty->getValue($connection)); + $this->assertEquals($mongoHash, spl_object_hash($connectionProperty->getValue($connection))); + } + + public function testDisconnect() + { + $this->connection->disconnect(); + $this->assertFalse($this->connection->isConnected()); + } + + + public function testReconnectonnect() + { + $this->connection->disconnect(); + $this->assertTrue($this->connection->disconnect()); + $this->assertFalse($this->connection->isConnected()); + $this->connection->connect(); + $this->assertTrue($this->connection->connect()); + $this->assertTrue($this->connection->isConnected()); + } + + + public function testHasDatabase() + { + $this->assertFalse($this->connection->hasDatabase('__unknown__database__')); + $this->assertTrue($this->connection->hasDatabase('admin')); + } + + public function testDatabaseDefaultServer() + { + $connection = new Connection(null); + $host = (string) $connection->getConnection(); + $this->assertEquals('localhost:27017', $host); + } + + public function testDatabaseConfig() + { + $connection = new Connection(array('connect' => true)); + $host = (string) $connection->getConnection(); + $this->assertEquals('localhost:27017', $host); + } + + public function testListDatabases() + { + $list = $this->connection->listDatabases(); + + $this->assertInternalType('array', $list); + $this->assertContains('admin', $list); + + $list = $this->connection->listDatabases(true); + $this->assertInternalType('array', $list); + } + + public function testGetMongoObject() + { + $mongo = $this->connection->getConnection(); + + $this->assertInstanceOf('MongoClient', $mongo); + } + + + public function testDropUnknownDatabase() + { + $result = $this->connection->dropDatabase('_unknown_'); + + $this->assertTrue($result); + } + + + public function testDropKnownDatabase() + { + $mongo = $this->connection->getConnection(); + $mongo->demo->users->insert(array('test' => true)); + + $result = $this->connection->dropDatabase('demo'); + + $this->assertTrue($result); + } + + + public function testGetDatabase() + { + $database = $this->connection->database('my_db'); + $this->assertInstanceOf('League\Monga\Database', $database); + } + + + public function testGetMongoDatabase() + { + $database = $this->connection->database('my_db', false); + $this->assertInstanceOf('MongoDB', $database); + } + + + public function testGetConnection() + { + $mongo = $this->connection->getConnection(); + $this->assertInstanceOf('MongoClient', $mongo); + } + + + public function testReplaceConnection() + { + $original = $this->connection->getConnection(); + $new = new MongoClient(); + $original_hash = spl_object_hash($original); + $new_hash = spl_object_hash($new); + $this->connection->setConnection($new); + $get_hash = spl_object_hash($this->connection->getConnection()); + $this->assertEquals($get_hash, $new_hash); + $this->assertNotEquals($get_hash, $original_hash); + $this->connection->setConnection($original); + } +} diff --git a/tests/CursorTests.php b/tests/CursorTests.php index 36cde33..e2866fe 100644 --- a/tests/CursorTests.php +++ b/tests/CursorTests.php @@ -4,66 +4,64 @@ class CursorTests extends PHPUnit_Framework_TestCase { - public function testCursor() - { - $database = Monga::connection() - ->database('__database__'); + public function testCursor() + { + $database = Monga::connection() + ->database('__database__'); - $collection = $database->collection('_collection'); - $collection->drop(); - $values = array( - array('name' => 'Bill'), - array('name' => 'Frank'), - array('name' => 'George'), - ); + $collection = $database->collection('_collection'); + $collection->drop(); + $values = array( + array('name' => 'Bill'), + array('name' => 'Frank'), + array('name' => 'George'), + ); - $ids = $collection->insert($values); + $ids = $collection->insert($values); - foreach($values as $index => &$row) - { - $row['_id'] = $ids[$index]; - } + foreach($values as $index => &$row) { + $row['_id'] = $ids[$index]; + } - $cursor = $collection->find(function($query){ - $query->orderBy('name', 'asc'); - }); - $asArray = array_values($cursor->toArray()); - $oldCollection = $cursor->getCollection(); - $mongocursor = $cursor->getCursor(); - $this->assertInstanceOf('MongoCursor', $mongocursor); - $this->assertEquals(3, $cursor->count()); - $this->assertInstanceOf('League\Monga\Collection', $oldCollection); + $cursor = $collection->find(function ($query) { + $query->orderBy('name', 'asc'); + }); + $asArray = array_values($cursor->toArray()); + $oldCollection = $cursor->getCollection(); + $mongocursor = $cursor->getCursor(); + $this->assertInstanceOf('MongoCursor', $mongocursor); + $this->assertEquals(3, $cursor->count()); + $this->assertInstanceOf('League\Monga\Collection', $oldCollection); - foreach($cursor as $row) - { - $this->assertInternalType('array', $row); - } + foreach($cursor as $row) { + $this->assertInternalType('array', $row); + } - $newCollection = $database->collection('_new_collection_'); + $newCollection = $database->collection('_new_collection_'); - $this->assertEquals($values, $asArray); - $this->assertInternalType('array', $asArray); - $this->assertContainsOnly('array', $asArray); + $this->assertEquals($values, $asArray); + $this->assertInternalType('array', $asArray); + $this->assertContainsOnly('array', $asArray); - $refs = $cursor->toRefArray(); - $this->assertInternalType('array', $refs); - $this->assertContainsOnly('array', $refs); - $this->assertInternalType('array', $cursor->explain()); - $this->assertInstanceOf('League\Monga\Cursor', $cursor->partial(false)); - $collection->drop(); - } + $refs = $cursor->toRefArray(); + $this->assertInternalType('array', $refs); + $this->assertContainsOnly('array', $refs); + $this->assertInternalType('array', $cursor->explain()); + $this->assertInstanceOf('League\Monga\Cursor', $cursor->partial(false)); + $collection->drop(); + } - /** + /** * @expectedException BadMethodCallException */ - public function testBadMethodCall() - { - $mock = $this->getMockBuilder('MongoCursor') - ->disableOriginalConstructor() - ->getMock(); + public function testBadMethodCall() + { + $mock = $this->getMockBuilder('MongoCursor') + ->disableOriginalConstructor() + ->getMock(); - $cursor = new League\Monga\Cursor($mock); + $cursor = new League\Monga\Cursor($mock); - $cursor->badMethodCall(); - } -} \ No newline at end of file + $cursor->badMethodCall(); + } +} diff --git a/tests/DatabaseTests.php b/tests/DatabaseTests.php index 2704669..6a5c78f 100644 --- a/tests/DatabaseTests.php +++ b/tests/DatabaseTests.php @@ -6,217 +6,216 @@ class DatabaseTests extends PHPUnit_Framework_TestCase { - protected $database; - - protected $connection; - - public function setUp() - { - if ( ! $this->connection) - { - $this->connection = Monga::connection(); - } - - $this->database = null; - $this->database = $this->connection->database('__unit_testing__'); - } - - public function tearDown() - { - $this->connection->dropDatabase('__unit_testing__'); - } - - public function testBasicNewDatabase() - { - $mongo = new MongoClient(); - $database = new Database($mongo->{"__unit_testing__"}); - $reflection = new ReflectionObject($database); - $propertyDatabase = $reflection->getProperty('database'); - $propertyDatabase->setAccessible(true); - $propertyConnection = $reflection->getProperty('connection'); - $propertyConnection->setAccessible(true); - $this->assertInstanceOf('MongoDB', $propertyDatabase->getValue($database)); - $this->assertNull($propertyConnection->getValue($database)); - } - - public function testNewDatabaseWithConnection() - { - $mongo = new MongoClient(); - $connection = Monga::connection(); - $connectionHash = spl_object_hash($connection); - $database = new Database($mongo->{"__unit_testing__"}, $connection); - $reflection = new ReflectionObject($database); - $propertyDatabase = $reflection->getProperty('database'); - $propertyDatabase->setAccessible(true); - $propertyConnection = $reflection->getProperty('connection'); - $propertyConnection->setAccessible(true); - $this->assertInstanceOf('MongoDB', $propertyDatabase->getValue($database)); - $this->assertInstanceOf('League\Monga\Connection', $propertyConnection->getValue($database)); - $this->assertEquals($connectionHash, spl_object_hash($propertyConnection->getValue($database))); - } - - public function testGetDatabase() - { - $database = $this->database->getDatabase(); - $this->assertInstanceOf('MongoDB', $database); - } - - public function testSetDatabase() - { - $original = $this->database->getDatabase(); - $originalHash = spl_object_hash($original); - $new = $this->connection->database('_other_unknown_', false); - $newHash = spl_object_hash($new); - $this->database->setDatabase($new); - $reflection = new ReflectionObject($this->database); - $property = $reflection->getProperty('database'); - $property->setAccessible(true); - $this->assertInstanceOf('MongoDB', $property->getValue($this->database)); - $this->assertEquals($newHash, spl_object_hash($property->getValue($this->database))); - $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->database))); - $this->database->setDatabase('from_string'); - $this->assertInstanceOf('MongoDB', $property->getValue($this->database)); - $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->database))); - $this->assertNotEquals($newHash, spl_object_hash($property->getValue($this->database))); - $this->database->setDatabase($original); - } - - public function testGetConnection() - { - $database = $this->database->getConnection(); - $this->assertInstanceOf('League\Monga\Connection', $database); - } - - public function testSetConnection() - { - $original = $this->database->getConnection(); - $originalHash = spl_object_hash($original); - $new = new Connection(); - $newHash = spl_object_hash($new); - $this->database->setConnection($new); - $reflection = new ReflectionObject($this->database); - $property = $reflection->getProperty('connection'); - $property->setAccessible(true); - $this->assertInstanceOf('League\Monga\Connection', $property->getValue($this->database)); - $this->assertEquals($newHash, spl_object_hash($property->getValue($this->database))); - $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->database))); - $this->database->setConnection($original); - } - - public function testListCollections() - { - $collections = $this->database->listCollections(); - $this->assertInternalType('array', $collections); - $this->assertEmpty($collections); - } - - public function testListCollectionsWithCollections() - { - $database = $this->database->getDatabase(); - $database->demo1->insert(array('some' => 'data')); - $database->demo2->insert(array('some' => 'data')); - $database->demo3->insert(array('some' => 'data')); - - $collections = $this->database->listCollections(); - $this->assertCount(3, $collections); - $this->assertEquals(array('demo1', 'demo2', 'demo3'), $collections); - } - - public function testHasCollection() - { - $this->database->getDatabase()->demo->insert(array('some' => 'data')); - - $this->assertTrue($this->database->hasCollection('demo')); - $this->assertFalse($this->database->hasCollection('__unknown_collection__')); - } - - public function testAllCollections() - { - $all = $this->database->allCollections(); - $this->assertEmpty($all); - - $database = $this->database->getDatabase(); - $database->demo1->insert(array('some' => 'data')); - $database->demo2->insert(array('some' => 'data')); - $database->demo3->insert(array('some' => 'data')); - - $all = $this->database->allCollections(true); - $this->assertContainsOnlyInstancesOf('League\Monga\Collection', $all); - - $raw = $this->database->allCollections(); - $this->assertContainsOnlyInstancesOf('MongoCollection', $raw); - } - - public function testGetCollection() - { - $collection = $this->database->collection('demo'); - $this->assertInstanceOf('League\Monga\Collection', $collection); - } - - public function testGetRawCollection() - { - $collection = $this->database->collection('demo', false); - $this->assertInstanceOf('MongoCollection', $collection); - } - - public function testGetFilesystem() - { - $fs = $this->database->filesystem('demo'); - $this->assertInstanceOf('League\Monga\Filesystem', $fs); - } - - public function testGetRawFilesystem() - { - $fs = $this->database->filesystem('demo', false); - $this->assertInstanceOf('MongoGridFS', $fs); - } - - public function testGetRef() - { - $item = array('something' => 'something'); - - $collection = $this->database->getDatabase()->{'demo'}; - $collection->insert($item); - $ref = MongoDBRef::create('demo', $item['_id']); - - $fetched = $this->database->getRef($ref); - $this->assertEquals($item, $fetched); - } - - public function testExecuteCode() - { - $result = $this->database->executeCode('function(){ return 1;}'); - - $this->assertInternalType('array', $result); - $this->assertEquals(1, $result['retval']); - } - - public function testExecuteParams() - { - $name = 'Frank'; - $result = $this->database->executeCode('function(name){ return name;}', array($name)); - $this->assertInternalType('array', $result); - $this->assertEquals($name, $result['retval']); - } - - public function testExecuteMongoCode() - { - $result = $this->database->executeCode(new MongoCode('function(){ return 1;}')); - $this->assertInternalType('array', $result); - $this->assertEquals(1, $result['retval']); - } - - public function testCommand() - { - $this->database->collection('demo')->insert(array('this' => 'that')); - $result = $this->database->command(array('count' => 'demo')); - $this->assertInternalType('array', $result); - $this->assertEquals(1, $result['ok']); - $this->assertEquals(1, $result['n']); - } - - public function testDrop() - { - $this->assertTrue($this->database->drop()); - } + protected $database; + + protected $connection; + + public function setUp() + { + if ( ! $this->connection) { + $this->connection = Monga::connection(); + } + + $this->database = null; + $this->database = $this->connection->database('__unit_testing__'); + } + + public function tearDown() + { + $this->connection->dropDatabase('__unit_testing__'); + } + + public function testBasicNewDatabase() + { + $mongo = new MongoClient(); + $database = new Database($mongo->{"__unit_testing__"}); + $reflection = new ReflectionObject($database); + $propertyDatabase = $reflection->getProperty('database'); + $propertyDatabase->setAccessible(true); + $propertyConnection = $reflection->getProperty('connection'); + $propertyConnection->setAccessible(true); + $this->assertInstanceOf('MongoDB', $propertyDatabase->getValue($database)); + $this->assertNull($propertyConnection->getValue($database)); + } + + public function testNewDatabaseWithConnection() + { + $mongo = new MongoClient(); + $connection = Monga::connection(); + $connectionHash = spl_object_hash($connection); + $database = new Database($mongo->{"__unit_testing__"}, $connection); + $reflection = new ReflectionObject($database); + $propertyDatabase = $reflection->getProperty('database'); + $propertyDatabase->setAccessible(true); + $propertyConnection = $reflection->getProperty('connection'); + $propertyConnection->setAccessible(true); + $this->assertInstanceOf('MongoDB', $propertyDatabase->getValue($database)); + $this->assertInstanceOf('League\Monga\Connection', $propertyConnection->getValue($database)); + $this->assertEquals($connectionHash, spl_object_hash($propertyConnection->getValue($database))); + } + + public function testGetDatabase() + { + $database = $this->database->getDatabase(); + $this->assertInstanceOf('MongoDB', $database); + } + + public function testSetDatabase() + { + $original = $this->database->getDatabase(); + $originalHash = spl_object_hash($original); + $new = $this->connection->database('_other_unknown_', false); + $newHash = spl_object_hash($new); + $this->database->setDatabase($new); + $reflection = new ReflectionObject($this->database); + $property = $reflection->getProperty('database'); + $property->setAccessible(true); + $this->assertInstanceOf('MongoDB', $property->getValue($this->database)); + $this->assertEquals($newHash, spl_object_hash($property->getValue($this->database))); + $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->database))); + $this->database->setDatabase('from_string'); + $this->assertInstanceOf('MongoDB', $property->getValue($this->database)); + $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->database))); + $this->assertNotEquals($newHash, spl_object_hash($property->getValue($this->database))); + $this->database->setDatabase($original); + } + + public function testGetConnection() + { + $database = $this->database->getConnection(); + $this->assertInstanceOf('League\Monga\Connection', $database); + } + + public function testSetConnection() + { + $original = $this->database->getConnection(); + $originalHash = spl_object_hash($original); + $new = new Connection(); + $newHash = spl_object_hash($new); + $this->database->setConnection($new); + $reflection = new ReflectionObject($this->database); + $property = $reflection->getProperty('connection'); + $property->setAccessible(true); + $this->assertInstanceOf('League\Monga\Connection', $property->getValue($this->database)); + $this->assertEquals($newHash, spl_object_hash($property->getValue($this->database))); + $this->assertNotEquals($originalHash, spl_object_hash($property->getValue($this->database))); + $this->database->setConnection($original); + } + + public function testListCollections() + { + $collections = $this->database->listCollections(); + $this->assertInternalType('array', $collections); + $this->assertEmpty($collections); + } + + public function testListCollectionsWithCollections() + { + $database = $this->database->getDatabase(); + $database->demo1->insert(array('some' => 'data')); + $database->demo2->insert(array('some' => 'data')); + $database->demo3->insert(array('some' => 'data')); + + $collections = $this->database->listCollections(); + $this->assertCount(3, $collections); + $this->assertEquals(array('demo1', 'demo2', 'demo3'), $collections); + } + + public function testHasCollection() + { + $this->database->getDatabase()->demo->insert(array('some' => 'data')); + + $this->assertTrue($this->database->hasCollection('demo')); + $this->assertFalse($this->database->hasCollection('__unknown_collection__')); + } + + public function testAllCollections() + { + $all = $this->database->allCollections(); + $this->assertEmpty($all); + + $database = $this->database->getDatabase(); + $database->demo1->insert(array('some' => 'data')); + $database->demo2->insert(array('some' => 'data')); + $database->demo3->insert(array('some' => 'data')); + + $all = $this->database->allCollections(true); + $this->assertContainsOnlyInstancesOf('League\Monga\Collection', $all); + + $raw = $this->database->allCollections(); + $this->assertContainsOnlyInstancesOf('MongoCollection', $raw); + } + + public function testGetCollection() + { + $collection = $this->database->collection('demo'); + $this->assertInstanceOf('League\Monga\Collection', $collection); + } + + public function testGetRawCollection() + { + $collection = $this->database->collection('demo', false); + $this->assertInstanceOf('MongoCollection', $collection); + } + + public function testGetFilesystem() + { + $fs = $this->database->filesystem('demo'); + $this->assertInstanceOf('League\Monga\Filesystem', $fs); + } + + public function testGetRawFilesystem() + { + $fs = $this->database->filesystem('demo', false); + $this->assertInstanceOf('MongoGridFS', $fs); + } + + public function testGetRef() + { + $item = array('something' => 'something'); + + $collection = $this->database->getDatabase()->{'demo'}; + $collection->insert($item); + $ref = MongoDBRef::create('demo', $item['_id']); + + $fetched = $this->database->getRef($ref); + $this->assertEquals($item, $fetched); + } + + public function testExecuteCode() + { + $result = $this->database->executeCode('function () { return 1;}'); + + $this->assertInternalType('array', $result); + $this->assertEquals(1, $result['retval']); + } + + public function testExecuteParams() + { + $name = 'Frank'; + $result = $this->database->executeCode('function (name) { return name;}', array($name)); + $this->assertInternalType('array', $result); + $this->assertEquals($name, $result['retval']); + } + + public function testExecuteMongoCode() + { + $result = $this->database->executeCode(new MongoCode('function () { return 1;}')); + $this->assertInternalType('array', $result); + $this->assertEquals(1, $result['retval']); + } + + public function testCommand() + { + $this->database->collection('demo')->insert(array('this' => 'that')); + $result = $this->database->command(array('count' => 'demo')); + $this->assertInternalType('array', $result); + $this->assertEquals(1, $result['ok']); + $this->assertEquals(1, $result['n']); + } + + public function testDrop() + { + $this->assertTrue($this->database->drop()); + } } diff --git a/tests/FilesystemTests.php b/tests/FilesystemTests.php index cd758ad..84c74f8 100644 --- a/tests/FilesystemTests.php +++ b/tests/FilesystemTests.php @@ -2,170 +2,170 @@ class FileMock { - protected $return = true; + protected $return = true; - public function __construct($return) - { - $this->return = $return; - } + public function __construct($return) + { + $this->return = $return; + } - public $file = array( - '_id' => 'some_id', - ); + public $file = array( + '_id' => 'some_id', + ); - public function write() - { - return $this->return; - } + public function write() + { + return $this->return; + } } class FilesystemTests extends PHPUnit_Framework_TestCase { - public function testStore() - { - $id = new MongoId('516ba5033b21c50005a93f76'); - - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('put')) - ->getMock(); - - $mock->expects($this->once()) - ->method('put') - ->with('filename.json', array('downloads' => 0)) - ->will($this->returnValue($id)); + public function testStore() + { + $id = new MongoId('516ba5033b21c50005a93f76'); + + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('put')) + ->getMock(); + + $mock->expects($this->once()) + ->method('put') + ->with('filename.json', array('downloads' => 0)) + ->will($this->returnValue($id)); - $fs = new League\Monga\Filesystem($mock); - - $result = $fs->store('filename.json', array('downloads' => 0)); - $this->assertEquals($id, $result); - } - - public function testStoreBytes() - { - $id = new MongoId('516ba5033b21c50005a93f76'); - - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('storeBytes')) - ->getMock(); - - $mock->expects($this->once()) - ->method('storeBytes') - ->with('filename.json', array('downloads' => 0), array()) - ->will($this->returnValue($id)); - - $fs = new League\Monga\Filesystem($mock); - - $result = $fs->storeBytes('filename.json', array('downloads' => 0)); - $this->assertEquals($id, $result); - } - - public function testStoreUpload() - { - $id = new MongoId('516ba5033b21c50005a93f76'); - - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('storeUpload')) - ->getMock(); - - $mock->expects($this->once()) - ->method('storeUpload') - ->with('filename.json', array('downloads' => 0)) - ->will($this->returnValue($id)); - - $fs = new League\Monga\Filesystem($mock); - - $result = $fs->storeUpload('filename.json', array('downloads' => 0)); - $this->assertEquals($id, $result); - } - - public function testExtractFailing() - { - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('extract', 'findOne', 'remove')) - ->getMock(); - - $fileMock = $this->getMockBuilder('MongoGridFile') - ->disableOriginalConstructor() - ->setMethods(array('write')) - ->getMock(); - - $mock->expects($this->once()) - ->method('findOne') - ->with('filename.json') - ->will($this->returnValue(new FileMock(false))); - - - $fs = new League\Monga\Filesystem($mock); - - $this->assertFalse($fs->extract('filename.json')); - } - - public function testExtract() - { - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('extract', 'findOne', 'remove')) - ->getMock(); - - $fileMock = $this->getMockBuilder('MongoGridFile') - ->disableOriginalConstructor() - ->setMethods(array('write')) - ->getMock(); - - $mock->expects($this->once()) - ->method('findOne') - ->with('filename.json') - ->will($this->returnValue(new FileMock(true))); - - $mock->expects($this->once()) - ->method('remove') - ->with(array('_id' => 'some_id')) - ->will($this->returnValue(true)); - - $fs = new League\Monga\Filesystem($mock); - - $this->assertTrue($fs->extract('filename.json')); - } - - public function testStoreFile() - { - $id = new MongoId('516ba5033b21c50005a93f76'); + $fs = new League\Monga\Filesystem($mock); + + $result = $fs->store('filename.json', array('downloads' => 0)); + $this->assertEquals($id, $result); + } + + public function testStoreBytes() + { + $id = new MongoId('516ba5033b21c50005a93f76'); + + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('storeBytes')) + ->getMock(); + + $mock->expects($this->once()) + ->method('storeBytes') + ->with('filename.json', array('downloads' => 0), array()) + ->will($this->returnValue($id)); + + $fs = new League\Monga\Filesystem($mock); + + $result = $fs->storeBytes('filename.json', array('downloads' => 0)); + $this->assertEquals($id, $result); + } + + public function testStoreUpload() + { + $id = new MongoId('516ba5033b21c50005a93f76'); + + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('storeUpload')) + ->getMock(); + + $mock->expects($this->once()) + ->method('storeUpload') + ->with('filename.json', array('downloads' => 0)) + ->will($this->returnValue($id)); + + $fs = new League\Monga\Filesystem($mock); + + $result = $fs->storeUpload('filename.json', array('downloads' => 0)); + $this->assertEquals($id, $result); + } + + public function testExtractFailing() + { + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('extract', 'findOne', 'remove')) + ->getMock(); + + $fileMock = $this->getMockBuilder('MongoGridFile') + ->disableOriginalConstructor() + ->setMethods(array('write')) + ->getMock(); + + $mock->expects($this->once()) + ->method('findOne') + ->with('filename.json') + ->will($this->returnValue(new FileMock(false))); + + + $fs = new League\Monga\Filesystem($mock); + + $this->assertFalse($fs->extract('filename.json')); + } + + public function testExtract() + { + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('extract', 'findOne', 'remove')) + ->getMock(); + + $fileMock = $this->getMockBuilder('MongoGridFile') + ->disableOriginalConstructor() + ->setMethods(array('write')) + ->getMock(); + + $mock->expects($this->once()) + ->method('findOne') + ->with('filename.json') + ->will($this->returnValue(new FileMock(true))); + + $mock->expects($this->once()) + ->method('remove') + ->with(array('_id' => 'some_id')) + ->will($this->returnValue(true)); + + $fs = new League\Monga\Filesystem($mock); + + $this->assertTrue($fs->extract('filename.json')); + } + + public function testStoreFile() + { + $id = new MongoId('516ba5033b21c50005a93f76'); - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('storeFile')) - ->getMock(); - - $mock->expects($this->once()) - ->method('storeFile') - ->with('filename.json', array('downloads' => 0), array()) - ->will($this->returnValue($id)); - - $fs = new League\Monga\Filesystem($mock); - - $result = $fs->storeFile('filename.json', array('downloads' => 0)); - $this->assertEquals($id, $result); - } - - public function testFindOne() - { - $mock = $this->getMockBuilder('MongoGridFS') - ->disableOriginalConstructor() - ->setMethods(array('findOne')) - ->getMock(); - - $mock->expects($this->once()) - ->method('findOne') - ->with(array('key' => 'value')) - ->will($this->returnValue('_dummy_')); - - $fs = new League\Monga\Filesystem($mock); - - $result = $fs->findOne(array('key' => 'value')); - $this->assertEquals('_dummy_', $result); - } -} \ No newline at end of file + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('storeFile')) + ->getMock(); + + $mock->expects($this->once()) + ->method('storeFile') + ->with('filename.json', array('downloads' => 0), array()) + ->will($this->returnValue($id)); + + $fs = new League\Monga\Filesystem($mock); + + $result = $fs->storeFile('filename.json', array('downloads' => 0)); + $this->assertEquals($id, $result); + } + + public function testFindOne() + { + $mock = $this->getMockBuilder('MongoGridFS') + ->disableOriginalConstructor() + ->setMethods(array('findOne')) + ->getMock(); + + $mock->expects($this->once()) + ->method('findOne') + ->with(array('key' => 'value')) + ->will($this->returnValue('_dummy_')); + + $fs = new League\Monga\Filesystem($mock); + + $result = $fs->findOne(array('key' => 'value')); + $this->assertEquals('_dummy_', $result); + } +} diff --git a/tests/MongaTests.php b/tests/MongaTests.php index 592b609..817f038 100644 --- a/tests/MongaTests.php +++ b/tests/MongaTests.php @@ -4,68 +4,68 @@ class MongaTests extends PHPUnit_Framework_TestCase { - public function testMongaId() - { - $id = Monga::id('516ba5033b21c50005a93f76'); + public function testMongaId() + { + $id = Monga::id('516ba5033b21c50005a93f76'); - $this->assertInstanceOf('MongoId', $id); - } + $this->assertInstanceOf('MongoId', $id); + } - public function testMongoData() - { - $data = Monga::data('something'); + public function testMongoData() + { + $data = Monga::data('something'); - $this->assertInstanceOf('MongoBinData', $data); - } + $this->assertInstanceOf('MongoBinData', $data); + } - public function testMongoDataWithType() - { - $data = Monga::data('something', MongoBinData::CUSTOM); + public function testMongoDataWithType() + { + $data = Monga::data('something', MongoBinData::CUSTOM); - $this->assertInstanceOf('MongoBinData', $data); - $this->assertEquals($data->bin, 'something'); - $this->assertEquals($data->type, MongoBinData::CUSTOM); - } + $this->assertInstanceOf('MongoBinData', $data); + $this->assertEquals($data->bin, 'something'); + $this->assertEquals($data->type, MongoBinData::CUSTOM); + } - public function testMongaCode() - { - $code = Monga::code('__code__'); + public function testMongaCode() + { + $code = Monga::code('__code__'); - $this->assertInstanceOf('MongoCode', $code); + $this->assertInstanceOf('MongoCode', $code); - $string = (string) $code; - $this->assertEquals('__code__', $string); - } + $string = (string) $code; + $this->assertEquals('__code__', $string); + } - public function testMongaDate() - { - // random time - $time = time() - 100; - $date = Monga::date($time, 2); + public function testMongaDate() + { + // random time + $time = time() - 100; + $date = Monga::date($time, 2); - $this->assertInstanceOf('MongoDate', $date); - $this->assertEquals($time, $date->sec); - } + $this->assertInstanceOf('MongoDate', $date); + $this->assertEquals($time, $date->sec); + } - public function testMongaRegex() - { - $regex = Monga::regex('/(.*)/imu'); + public function testMongaRegex() + { + $regex = Monga::regex('/(.*)/imu'); - $this->assertInstanceOf('MongoRegex', $regex); - $this->assertEquals('imu', $regex->flags); - } + $this->assertInstanceOf('MongoRegex', $regex); + $this->assertEquals('imu', $regex->flags); + } - /** + /** * @expectedException MongoException */ - public function testInvalidRegex() - { - $regex = Monga::regex('#(.*)#imu'); - } - - public function testMongaConnection() - { - $connection = Monga::connection(); - $this->assertInstanceOf('League\Monga\\Connection', $connection); - } -} \ No newline at end of file + public function testInvalidRegex() + { + $regex = Monga::regex('#(.*)#imu'); + } + + public function testMongaConnection() + { + $connection = Monga::connection(); + $this->assertInstanceOf('League\Monga\\Connection', $connection); + } +} diff --git a/tests/QueryAggregateTests.php b/tests/QueryAggregateTests.php index 24db706..6576454 100644 --- a/tests/QueryAggregateTests.php +++ b/tests/QueryAggregateTests.php @@ -5,183 +5,183 @@ class QueryAggregateTests extends PHPUnit_Framework_TestCase { - public function testProject() - { - $a = new Agr(); - $a->project(array( - 'field' => 1, - 'other' => -1, - )); - - $expected = array( - array('$project' => array( - 'field' => 1, - 'other' => -1, - )), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testProjectClosure() - { - $a = new Agr(); - $a->project(function($p){ - $p->select('field'); - $p->exclude('other'); - $p->alias('actual', 'alias'); - }); - - $expected = array( - array('$project' => array( - 'field' => 1, - 'other' => -1, - 'alias' => '$actual', - )), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testGroup() - { - $a = new Agr(); - $a->group(array( - 'num' => array('$sum' => '$num'), - )); - - $expected = array( - array('$group' => array( - 'num' => array('$sum' => '$num'), - )), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testGroupClosure() - { - $a = new Agr(); - $a->group(function($g){ - $g->sum('num'); - }); - - $expected = array( - array('$group' => array( - 'num' => array('$sum' => 1), - )), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testUnwind() - { - $a = new Agr(); - $a->unwind('tags'); - $expected = array( - array('$unwind' => '$tags'), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testSkip() - { - $a = new Agr(); - $a->skip(1); - $expected = array( - array('$skip' => 1), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testLimit() - { - $a = new Agr(); - $a->limit(1); - $expected = array( - array('$limit' => 1), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testPipe() - { - $a = new Agr(); - $a->pipe(array('$limit' => 1)); - $expected = array( - array('$limit' => 1), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testMatch() - { - $a = new Agr(); - $a->match(array('field' => 'value')); - $expected = array( - array( - '$match' => array( - 'field' => 'value', - ), - ), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testMatchClosure() - { - $a = new Agr(); - $a->match(function($w){ - $w->where('field', 'value'); - }); - $expected = array( - array( - '$match' => array( - 'field' => 'value', - ), - ), - ); - - $this->assertEquals($expected, $a->getPipeline()); - } - - public function testComputor() - { - $a = new Agr(); - $a->group(function($g){ - $g->addToSet('tags') - ->first('first', 'favs') - ->last('last', 'favs') - ->max('max', 'scores') - ->min('min', 'scores') - ->push('names', 'name') - ->by('rank'); - }); - - $expected = array( - array('$group' => array( - 'tags' => array('$addToSet' => '$tags'), - 'first' => array('$first' => '$favs'), - 'last' => array('$last' => '$favs'), - 'max' => array('$max' => '$scores'), - 'min' => array('$min' => '$scores'), - 'names' => array('$push' => '$name'), - '_id' => '$rank', - )), - ); - } - - public function testSetPipeline() - { - $a = new Agr(); - $expected = array('pipeline'); - $a->setPipeline($expected); - $this->assertEquals($expected, $a->getPipeline()); - } -} \ No newline at end of file + public function testProject() + { + $a = new Agr(); + $a->project(array( + 'field' => 1, + 'other' => -1, + )); + + $expected = array( + array('$project' => array( + 'field' => 1, + 'other' => -1, + )), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testProjectClosure() + { + $a = new Agr(); + $a->project(function ($p) { + $p->select('field'); + $p->exclude('other'); + $p->alias('actual', 'alias'); + }); + + $expected = array( + array('$project' => array( + 'field' => 1, + 'other' => -1, + 'alias' => '$actual', + )), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testGroup() + { + $a = new Agr(); + $a->group(array( + 'num' => array('$sum' => '$num'), + )); + + $expected = array( + array('$group' => array( + 'num' => array('$sum' => '$num'), + )), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testGroupClosure() + { + $a = new Agr(); + $a->group(function ($g) { + $g->sum('num'); + }); + + $expected = array( + array('$group' => array( + 'num' => array('$sum' => 1), + )), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testUnwind() + { + $a = new Agr(); + $a->unwind('tags'); + $expected = array( + array('$unwind' => '$tags'), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testSkip() + { + $a = new Agr(); + $a->skip(1); + $expected = array( + array('$skip' => 1), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testLimit() + { + $a = new Agr(); + $a->limit(1); + $expected = array( + array('$limit' => 1), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testPipe() + { + $a = new Agr(); + $a->pipe(array('$limit' => 1)); + $expected = array( + array('$limit' => 1), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testMatch() + { + $a = new Agr(); + $a->match(array('field' => 'value')); + $expected = array( + array( + '$match' => array( + 'field' => 'value', + ), + ), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testMatchClosure() + { + $a = new Agr(); + $a->match(function ($w) { + $w->where('field', 'value'); + }); + $expected = array( + array( + '$match' => array( + 'field' => 'value', + ), + ), + ); + + $this->assertEquals($expected, $a->getPipeline()); + } + + public function testComputor() + { + $a = new Agr(); + $a->group(function ($g) { + $g->addToSet('tags') + ->first('first', 'favs') + ->last('last', 'favs') + ->max('max', 'scores') + ->min('min', 'scores') + ->push('names', 'name') + ->by('rank'); + }); + + $expected = array( + array('$group' => array( + 'tags' => array('$addToSet' => '$tags'), + 'first' => array('$first' => '$favs'), + 'last' => array('$last' => '$favs'), + 'max' => array('$max' => '$scores'), + 'min' => array('$min' => '$scores'), + 'names' => array('$push' => '$name'), + '_id' => '$rank', + )), + ); + } + + public function testSetPipeline() + { + $a = new Agr(); + $expected = array('pipeline'); + $a->setPipeline($expected); + $this->assertEquals($expected, $a->getPipeline()); + } +} diff --git a/tests/QueryBuilderTests.php b/tests/QueryBuilderTests.php index 0725041..b1324af 100644 --- a/tests/QueryBuilderTests.php +++ b/tests/QueryBuilderTests.php @@ -4,80 +4,80 @@ class BuilderMock extends League\Monga\Query\Builder {} class QueryBuilderTests extends PHPUnit_Framework_TestCase { - protected $builder; - - public function setUp() - { - $this->builder = new BuilderMock(); - } - - public function getProperty($property) - { - $reflection = new ReflectionObject($this->builder); - $property = $property = $reflection->getProperty($property); - $property->setAccessible(true); - return $property->getValue($this->builder); - } - - public function testSafe() - { - $this->builder->safe(); - $this->assertTrue($this->getProperty('safe')); - $this->builder->safe(false); - $this->assertFalse($this->getProperty('safe')); - } - - public function testFsync() - { - $this->builder->fsync(); - $this->assertTrue($this->getProperty('fsync')); - $this->builder->fsync(false); - $this->assertFalse($this->getProperty('fsync')); - } - - public function testTimeout() - { - $this->assertNull($this->getProperty('timeout')); - $this->builder->timeout(100); - $this->assertEquals(100, $this->getProperty('timeout')); - } - - public function testSetOption() - { - $this->builder->setOptions(array( - 'fsync' => true, - 'safe' => true, - )); - - $this->assertTrue($this->getProperty('fsync')); - $this->assertTrue($this->getProperty('safe')); - - $this->builder->setOptions(array( - 'fsync' => false, - 'safe' => false, - )); - - $this->assertFalse($this->getProperty('fsync')); - $this->assertFalse($this->getProperty('safe')); - } - - public function testGetOptions() - { - $result = $this->builder->getOptions(); - - $this->assertEquals(array( - 'w' => 0, - 'fsync' => false, - 'timeout' => MongoCursor::$timeout - ), $result); - - $result = $this->builder->timeout(100)->getOptions(); - - - $this->assertEquals(array( - 'w' => 0, - 'fsync' => false, - 'timeout' => 100 - ), $result); - } -} \ No newline at end of file + protected $builder; + + public function setUp() + { + $this->builder = new BuilderMock(); + } + + public function getProperty($property) + { + $reflection = new ReflectionObject($this->builder); + $property = $property = $reflection->getProperty($property); + $property->setAccessible(true); + return $property->getValue($this->builder); + } + + public function testSafe() + { + $this->builder->safe(); + $this->assertTrue($this->getProperty('safe')); + $this->builder->safe(false); + $this->assertFalse($this->getProperty('safe')); + } + + public function testFsync() + { + $this->builder->fsync(); + $this->assertTrue($this->getProperty('fsync')); + $this->builder->fsync(false); + $this->assertFalse($this->getProperty('fsync')); + } + + public function testTimeout() + { + $this->assertNull($this->getProperty('timeout')); + $this->builder->timeout(100); + $this->assertEquals(100, $this->getProperty('timeout')); + } + + public function testSetOption() + { + $this->builder->setOptions(array( + 'fsync' => true, + 'safe' => true, + )); + + $this->assertTrue($this->getProperty('fsync')); + $this->assertTrue($this->getProperty('safe')); + + $this->builder->setOptions(array( + 'fsync' => false, + 'safe' => false, + )); + + $this->assertFalse($this->getProperty('fsync')); + $this->assertFalse($this->getProperty('safe')); + } + + public function testGetOptions() + { + $result = $this->builder->getOptions(); + + $this->assertEquals(array( + 'w' => 0, + 'fsync' => false, + 'timeout' => MongoCursor::$timeout + ), $result); + + $result = $this->builder->timeout(100)->getOptions(); + + + $this->assertEquals(array( + 'w' => 0, + 'fsync' => false, + 'timeout' => 100 + ), $result); + } +} diff --git a/tests/QueryFindTests.php b/tests/QueryFindTests.php index 6ffe36e..9998b3f 100644 --- a/tests/QueryFindTests.php +++ b/tests/QueryFindTests.php @@ -2,118 +2,118 @@ class QueryFindTests extends PHPUnit_Framework_TestCase { - protected $find; - - public function setUp() - { - $this->find = new League\Monga\Query\Find(); - } - - public function getProperty($property) - { - $reflection = new ReflectionObject($this->find); - $property = $property = $reflection->getProperty($property); - $property->setAccessible(true); - return $property->getValue($this->find); - } - - public function testOrderBy() - { - $this->find->orderBy('one', 1); - $this->find->orderBy('two', 'asc'); - $this->find->orderBy('three', 'desc'); - - $this->assertEquals(array - ( - 'one' => 1, - 'two' => 1, - 'three' => -1 - ), - $this->getProperty('orderBy')); - } - - public function testSelect() - { - $this->find->select('one', 'two'); - - $this->assertEquals(array - ( - 'one' => 1, - 'two' => 1, - ), - $this->getProperty('fields')); - } - - public function testExclude() - { - $this->find->exclude('one', 'two'); - - $this->assertEquals(array - ( - 'one' => -1, - 'two' => -1, - ), - $this->getProperty('fields')); - } - - public function testFields() - { - $this->find->fields(array( - 'one' => 1, - 'two' => false - )); - - $this->assertEquals(array - ( - 'one' => 1, - 'two' => -1, - ), - $this->getProperty('fields')); - } - - public function testGetFields() - { - $this->assertEmpty($this->getProperty('fields')); - $this->find->fields(array( - 'one' => 1, - )); - - $this->assertEquals(array('one' => 1), $this->getProperty('fields')); - } - - public function testOne() - { - $this->assertFalse($this->getProperty('findOne')); - $this->find->one(); - $this->assertTrue($this->getProperty('findOne')); - } - - public function testMultiple() - { - $this->assertFalse($this->getProperty('findOne')); - $this->find->multiple(); - $this->assertFalse($this->getProperty('findOne')); - $this->find->multiple(false); - $this->assertTrue($this->getProperty('findOne')); - } - - public function testGetFindOne() - { - $this->assertFalse($this->find->getFindOne()); - $this->find->one(); - $this->assertTrue($this->find->getFindOne()); - } - - public function testGetPostFindActions() - { - $expected = array( - array('sort', array('one' => 1)), - array('skip', 5), - array('limit', 15), - ); - - $this->find->skip(5)->limit(15)->orderBy('one', 'asc'); - - $this->assertEquals($expected, $this->find->getPostFindActions()); - } -} \ No newline at end of file + protected $find; + + public function setUp() + { + $this->find = new League\Monga\Query\Find(); + } + + public function getProperty($property) + { + $reflection = new ReflectionObject($this->find); + $property = $property = $reflection->getProperty($property); + $property->setAccessible(true); + return $property->getValue($this->find); + } + + public function testOrderBy() + { + $this->find->orderBy('one', 1); + $this->find->orderBy('two', 'asc'); + $this->find->orderBy('three', 'desc'); + + $this->assertEquals(array + ( + 'one' => 1, + 'two' => 1, + 'three' => -1 + ), + $this->getProperty('orderBy')); + } + + public function testSelect() + { + $this->find->select('one', 'two'); + + $this->assertEquals(array + ( + 'one' => 1, + 'two' => 1, + ), + $this->getProperty('fields')); + } + + public function testExclude() + { + $this->find->exclude('one', 'two'); + + $this->assertEquals(array + ( + 'one' => -1, + 'two' => -1, + ), + $this->getProperty('fields')); + } + + public function testFields() + { + $this->find->fields(array( + 'one' => 1, + 'two' => false + )); + + $this->assertEquals(array + ( + 'one' => 1, + 'two' => -1, + ), + $this->getProperty('fields')); + } + + public function testGetFields() + { + $this->assertEmpty($this->getProperty('fields')); + $this->find->fields(array( + 'one' => 1, + )); + + $this->assertEquals(array('one' => 1), $this->getProperty('fields')); + } + + public function testOne() + { + $this->assertFalse($this->getProperty('findOne')); + $this->find->one(); + $this->assertTrue($this->getProperty('findOne')); + } + + public function testMultiple() + { + $this->assertFalse($this->getProperty('findOne')); + $this->find->multiple(); + $this->assertFalse($this->getProperty('findOne')); + $this->find->multiple(false); + $this->assertTrue($this->getProperty('findOne')); + } + + public function testGetFindOne() + { + $this->assertFalse($this->find->getFindOne()); + $this->find->one(); + $this->assertTrue($this->find->getFindOne()); + } + + public function testGetPostFindActions() + { + $expected = array( + array('sort', array('one' => 1)), + array('skip', 5), + array('limit', 15), + ); + + $this->find->skip(5)->limit(15)->orderBy('one', 'asc'); + + $this->assertEquals($expected, $this->find->getPostFindActions()); + } +} diff --git a/tests/QueryIndexesTests.php b/tests/QueryIndexesTests.php index 32f96f8..b6b8325 100644 --- a/tests/QueryIndexesTests.php +++ b/tests/QueryIndexesTests.php @@ -5,97 +5,97 @@ class QueryIndexesTests extends PHPUnit_Framework_TestCase { - protected $indexes; - - public function tearDown() - { - m::close(); - } - - public function testGetCollection() - { - $collection = m::mock('MongoCollection'); - $indexes = new i($collection); - $this->assertInstanceOf('MongoCollection', $indexes->getCollection()); - } - - public function testSetCollection() - { - $collection = m::mock('MongoCollection'); - $collection2 = m::mock('MongoCollection'); - $indexes = new i($collection); - - $hash = spl_object_hash($collection); - $hash2 = spl_object_hash($collection2); - - $this->assertEquals($hash, spl_object_hash($indexes->getCollection())); - $indexes->setCollection($collection2); - $this->assertEquals($hash2, spl_object_hash($indexes->getCollection())); - } - - public function testCreate() - { - $index = array('field' => 1); - $mock = m::mock('MongoCollection'); - $mock->shouldReceive('ensureIndex') - ->with($index, array()); - - $i = new i($mock); - $i->create($index); - } - - public function testGeo() - { - $index = array('field' => '2d'); - $mock = m::mock('MongoCollection'); - $mock->shouldReceive('ensureIndex') - ->with($index, array()); - - $i = new i($mock); - $i->geo('field'); - } - - public function testPrepareIndex() - { - $i = new i(m::mock('MongoCollection')); - - $expected = array( - 'asc' => 1, - 'desc' => -1, - 'geo' => '2d', - 'field' => 1, - ); - - $reflection = new ReflectionObject($i); - $method = $reflection->getMethod('prepareIndex'); - $method->setAccessible(true); - $result = $method->invoke($i, array( - 'asc' => 'asc', - 'desc' => 'desc', - 'geo' => 'geo', - 'field' => 1, - )); - - $this->assertEquals($expected, $result); - } - - public function testDrop() - { - $mock = m::mock('MongoCollection'); - $mock->shouldReceive('deleteIndex'); - $i = new i($mock); - - $i->drop('index_name'); - } - - public function testDropAll() - { - $mock = m::mock('MongoCollection'); - - $mock->shouldReceive('deleteIndexes') - ->withNoArgs(); - - $i = new i($mock); - $i->dropAll(); - } -} \ No newline at end of file + protected $indexes; + + public function tearDown() + { + m::close(); + } + + public function testGetCollection() + { + $collection = m::mock('MongoCollection'); + $indexes = new i($collection); + $this->assertInstanceOf('MongoCollection', $indexes->getCollection()); + } + + public function testSetCollection() + { + $collection = m::mock('MongoCollection'); + $collection2 = m::mock('MongoCollection'); + $indexes = new i($collection); + + $hash = spl_object_hash($collection); + $hash2 = spl_object_hash($collection2); + + $this->assertEquals($hash, spl_object_hash($indexes->getCollection())); + $indexes->setCollection($collection2); + $this->assertEquals($hash2, spl_object_hash($indexes->getCollection())); + } + + public function testCreate() + { + $index = array('field' => 1); + $mock = m::mock('MongoCollection'); + $mock->shouldReceive('ensureIndex') + ->with($index, array()); + + $i = new i($mock); + $i->create($index); + } + + public function testGeo() + { + $index = array('field' => '2d'); + $mock = m::mock('MongoCollection'); + $mock->shouldReceive('ensureIndex') + ->with($index, array()); + + $i = new i($mock); + $i->geo('field'); + } + + public function testPrepareIndex() + { + $i = new i(m::mock('MongoCollection')); + + $expected = array( + 'asc' => 1, + 'desc' => -1, + 'geo' => '2d', + 'field' => 1, + ); + + $reflection = new ReflectionObject($i); + $method = $reflection->getMethod('prepareIndex'); + $method->setAccessible(true); + $result = $method->invoke($i, array( + 'asc' => 'asc', + 'desc' => 'desc', + 'geo' => 'geo', + 'field' => 1, + )); + + $this->assertEquals($expected, $result); + } + + public function testDrop() + { + $mock = m::mock('MongoCollection'); + $mock->shouldReceive('deleteIndex'); + $i = new i($mock); + + $i->drop('index_name'); + } + + public function testDropAll() + { + $mock = m::mock('MongoCollection'); + + $mock->shouldReceive('deleteIndexes') + ->withNoArgs(); + + $i = new i($mock); + $i->dropAll(); + } +} diff --git a/tests/QueryRemoveTests.php b/tests/QueryRemoveTests.php index e651253..20dede9 100644 --- a/tests/QueryRemoveTests.php +++ b/tests/QueryRemoveTests.php @@ -2,45 +2,45 @@ class QueryRemoveTests extends PHPUnit_Framework_TestCase { - protected $remove; + protected $remove; - public function setUp() - { - $this->remove = new League\Monga\Query\Remove(); - } + public function setUp() + { + $this->remove = new League\Monga\Query\Remove(); + } - public function getProperty($property) - { - $reflection = new ReflectionObject($this->remove); - $property = $property = $reflection->getProperty($property); - $property->setAccessible(true); - return $property->getValue($this->remove); - } + public function getProperty($property) + { + $reflection = new ReflectionObject($this->remove); + $property = $property = $reflection->getProperty($property); + $property->setAccessible(true); + return $property->getValue($this->remove); + } - public function testSingle() - { - $this->remove->single(); - $this->assertTrue($this->getProperty('justOne')); - $this->remove->single(false); - $this->assertFalse($this->getProperty('justOne')); - } + public function testSingle() + { + $this->remove->single(); + $this->assertTrue($this->getProperty('justOne')); + $this->remove->single(false); + $this->assertFalse($this->getProperty('justOne')); + } - public function testMultiple() - { - $this->remove->multiple(); - $this->assertFalse($this->getProperty('justOne')); - $this->remove->multiple(false); - $this->assertTrue($this->getProperty('justOne')); - } + public function testMultiple() + { + $this->remove->multiple(); + $this->assertFalse($this->getProperty('justOne')); + $this->remove->multiple(false); + $this->assertTrue($this->getProperty('justOne')); + } - public function testGetOptions() - { - $this->remove->single(); - $this->assertEquals(array( - 'w' => 0, - 'fsync' => false, - 'timeout' => MongoCursor::$timeout, - 'justOne' => true - ), $this->remove->getOptions()); - } -} \ No newline at end of file + public function testGetOptions() + { + $this->remove->single(); + $this->assertEquals(array( + 'w' => 0, + 'fsync' => false, + 'timeout' => MongoCursor::$timeout, + 'justOne' => true + ), $this->remove->getOptions()); + } +} diff --git a/tests/QueryUpdateTests.php b/tests/QueryUpdateTests.php index d9bb406..32b35f8 100644 --- a/tests/QueryUpdateTests.php +++ b/tests/QueryUpdateTests.php @@ -2,184 +2,184 @@ class QueryUpdateTests extends PHPUnit_Framework_TestCase { - protected $update; - - public function setUp() - { - $this->update = new League\Monga\Query\Update(); - } - - public function getProperty($property) - { - $reflection = new ReflectionObject($this->update); - $property = $property = $reflection->getProperty($property); - $property->setAccessible(true); - return $property->getValue($this->update); - } - - public function testSingle() - { - $this->assertTrue($this->getProperty('multiple')); - $this->update->single(); - $this->assertFalse($this->getProperty('multiple')); - $this->update->single(false); - $this->assertTrue($this->getProperty('multiple')); - } - - public function testMultiple() - { - $this->assertTrue($this->getProperty('multiple')); - $this->update->multiple(false); - $this->assertFalse($this->getProperty('multiple')); - $this->update->multiple(); - $this->assertTrue($this->getProperty('multiple')); - } - - public function testUpsert() - { - $this->update->upsert(false); - $this->assertFalse($this->getProperty('upsert')); - $this->update->upsert(); - $this->assertTrue($this->getProperty('upsert')); - } - - public function testSet() - { - $this->update->set('field', 'value'); - - $this->assertEquals(array( - 'field' => array('$set', 'value'), - ), - $this->getProperty('update')); - } - - public function testRemove() - { - $this->update->remove('one', 'two'); - - $this->assertEquals(array( - 'one' => array('$unset', 1), - 'two' => array('$unset', 1), - ), - $this->getProperty('update')); - } - - public function testRename() - { - $this->update->rename('one', 'two'); - - $this->assertEquals(array( - 'one' => array('$rename', 'two'), - ), - $this->getProperty('update')); - } - - public function testPush() - { - $this->update->push('field', 'value'); - $this->update->push('field2', 'value', true); - - $this->assertEquals(array( - 'field' => array('$push', 'value'), - 'field2' => array('$pushAll', 'value'), - ), - $this->getProperty('update')); - } - - public function testPushAll() - { - $this->update->pushAll('field', array('value', 'other')); - - $this->assertEquals(array( - 'field' => array('$pushAll', array('value', 'other')), - ), - $this->getProperty('update')); - } - - public function testPull() - { - $this->update->pull('field', 'value'); - $this->update->pull('field2', 10, '$gt', true); - - $this->assertEquals(array( - 'field' => array('$pull', 'value'), - 'field2' => array('$pullAll', array('$gt' => 10)), - ), - $this->getProperty('update')); - } - - public function testPullAll() - { - $this->update->pullAll('field', array('value', 'other')); - - $this->assertEquals(array( - 'field' => array('$pullAll', array('value', 'other')), - ), - $this->getProperty('update')); - } - - public function testAddToSet() - { - $this->update->addToSet('field', array('value', 'other')); - - $this->assertEquals(array( - 'field' => array('$addToSet', array('value', 'other')), - ), - $this->getProperty('update')); - } - - public function testPop() - { - $this->update->pop('field'); - - $this->assertEquals(array( - 'field' => array('$pop', 1), - ), - $this->getProperty('update')); - } - - public function testUnshift() - { - $this->update->unshift('field'); - - $this->assertEquals(array( - 'field' => array('$pop', -1), - ), - $this->getProperty('update')); - } - - public function testIncrement() - { - $this->update->increment('field', 2); - - $this->assertEquals(array( - 'field' => array('$inc', 2), - ), - $this->getProperty('update')); - } - - public function testGetUpdate() - { - $expected = array( - '$set' => array('field' => 'value'), - '$inc' => array('downloads' => 1), - '$atomic' => 1 - ); - - $result = $this->update->increment('downloads', 1)->set('field', 'value')->atomic()->getUpdate(); - - $this->assertEquals($expected, $result); - } - - public function testGetOptions() - { - $this->update->single(); - $this->assertEquals(array( - 'w' => 0, - 'fsync' => false, - 'timeout' => MongoCursor::$timeout, - 'upsert' => false, - 'multiple' => false, - ), $this->update->getOptions()); - } -} \ No newline at end of file + protected $update; + + public function setUp() + { + $this->update = new League\Monga\Query\Update(); + } + + public function getProperty($property) + { + $reflection = new ReflectionObject($this->update); + $property = $property = $reflection->getProperty($property); + $property->setAccessible(true); + return $property->getValue($this->update); + } + + public function testSingle() + { + $this->assertTrue($this->getProperty('multiple')); + $this->update->single(); + $this->assertFalse($this->getProperty('multiple')); + $this->update->single(false); + $this->assertTrue($this->getProperty('multiple')); + } + + public function testMultiple() + { + $this->assertTrue($this->getProperty('multiple')); + $this->update->multiple(false); + $this->assertFalse($this->getProperty('multiple')); + $this->update->multiple(); + $this->assertTrue($this->getProperty('multiple')); + } + + public function testUpsert() + { + $this->update->upsert(false); + $this->assertFalse($this->getProperty('upsert')); + $this->update->upsert(); + $this->assertTrue($this->getProperty('upsert')); + } + + public function testSet() + { + $this->update->set('field', 'value'); + + $this->assertEquals(array( + 'field' => array('$set', 'value'), + ), + $this->getProperty('update')); + } + + public function testRemove() + { + $this->update->remove('one', 'two'); + + $this->assertEquals(array( + 'one' => array('$unset', 1), + 'two' => array('$unset', 1), + ), + $this->getProperty('update')); + } + + public function testRename() + { + $this->update->rename('one', 'two'); + + $this->assertEquals(array( + 'one' => array('$rename', 'two'), + ), + $this->getProperty('update')); + } + + public function testPush() + { + $this->update->push('field', 'value'); + $this->update->push('field2', 'value', true); + + $this->assertEquals(array( + 'field' => array('$push', 'value'), + 'field2' => array('$pushAll', 'value'), + ), + $this->getProperty('update')); + } + + public function testPushAll() + { + $this->update->pushAll('field', array('value', 'other')); + + $this->assertEquals(array( + 'field' => array('$pushAll', array('value', 'other')), + ), + $this->getProperty('update')); + } + + public function testPull() + { + $this->update->pull('field', 'value'); + $this->update->pull('field2', 10, '$gt', true); + + $this->assertEquals(array( + 'field' => array('$pull', 'value'), + 'field2' => array('$pullAll', array('$gt' => 10)), + ), + $this->getProperty('update')); + } + + public function testPullAll() + { + $this->update->pullAll('field', array('value', 'other')); + + $this->assertEquals(array( + 'field' => array('$pullAll', array('value', 'other')), + ), + $this->getProperty('update')); + } + + public function testAddToSet() + { + $this->update->addToSet('field', array('value', 'other')); + + $this->assertEquals(array( + 'field' => array('$addToSet', array('value', 'other')), + ), + $this->getProperty('update')); + } + + public function testPop() + { + $this->update->pop('field'); + + $this->assertEquals(array( + 'field' => array('$pop', 1), + ), + $this->getProperty('update')); + } + + public function testUnshift() + { + $this->update->unshift('field'); + + $this->assertEquals(array( + 'field' => array('$pop', -1), + ), + $this->getProperty('update')); + } + + public function testIncrement() + { + $this->update->increment('field', 2); + + $this->assertEquals(array( + 'field' => array('$inc', 2), + ), + $this->getProperty('update')); + } + + public function testGetUpdate() + { + $expected = array( + '$set' => array('field' => 'value'), + '$inc' => array('downloads' => 1), + '$atomic' => 1 + ); + + $result = $this->update->increment('downloads', 1)->set('field', 'value')->atomic()->getUpdate(); + + $this->assertEquals($expected, $result); + } + + public function testGetOptions() + { + $this->update->single(); + $this->assertEquals(array( + 'w' => 0, + 'fsync' => false, + 'timeout' => MongoCursor::$timeout, + 'upsert' => false, + 'multiple' => false, + ), $this->update->getOptions()); + } +} diff --git a/tests/QueryWhereTests.php b/tests/QueryWhereTests.php index cd80931..2e185dd 100644 --- a/tests/QueryWhereTests.php +++ b/tests/QueryWhereTests.php @@ -3,1540 +3,1540 @@ class QueryWhereTests extends PHPUnit_Framework_TestCase { - protected $query; - - public function setUp() - { - $this->query = new League\Monga\Query\Where(); - } - - public function getProperty($property) - { - $reflection = new ReflectionObject($this->query); - $property = $property = $reflection->getProperty($property); - $property->setAccessible(true); - return $property->getValue($this->query); - } - - public function testSetWhere() - { - $this->assertNull($this->getProperty('where')); - $this->query->setWhere(array('name' => 'John')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => 'John'), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - $this->query->setWhere(array()); - $this->assertNull($this->getProperty('where')); - } - - public function testWhere() - { - $this->query->where('name', 'John'); - $this->query->where(array( - 'surname' => 'Doe', - 'age' => 25, - )); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - 'name' => 'John', - 'surname' => 'Doe', - 'age' => 25, - ), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhere() - { - $this->query->andWhere('name', 'John'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => 'John'), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhere() - { - $this->query->where('name', 'John') - ->orWhere('name', 'Steve') - ->orWhere(array('name' => 'Jack', 'surname' => 'Johnes')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => 'John'), - ) - ), - array( - '$and' => array( - array('name' => 'Steve'), - ) - ), - array( - '$and' => array( - array('name' => 'Jack'), - ) - ), - array( - '$and' => array( - array('surname' => 'Johnes'), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereNot() - { - $this->query->whereNot('name', 'John'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$ne' => 'John')), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereNot() - { - $this->query->andWhereNot('name', 'John'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$ne' => 'John')), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereNot() - { - $this->query->whereNot('name', 'John') - ->orWhereNot('name', 'Steve'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$ne' => 'John')), - ) - ), - array( - '$and' => array( - array('name' => array('$ne' => 'Steve')), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereLike() - { - $this->query->whereLike('field', '%value') - ->whereLike('field', 'value%') - ->whereLike('other', 'value', 'imxs'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$and' => array( - array( - 'field' => new MongoRegex('/value$/imxsu'), - ), - array( - 'field' => new MongoRegex('/^value/imxsu'), - 'other' => new MongoRegex('/^value$/imxs'), - ) - ), - ) - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereLike() - { - $this->query->whereLike('field', '%value') - ->andWhereLike('field', 'value%') - ->andWhereLike('other', 'value', 'imxs'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$and' => array( - array( - 'field' => new MongoRegex('/value$/imxsu'), - ), - array( - 'field' => new MongoRegex('/^value/imxsu'), - 'other' => new MongoRegex('/^value$/imxs'), - ) - ), - ) - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereLike() - { - $this->query->whereLike('field', '%value') - ->andWhereLike('field', 'value%') - ->andWhereLike('other', 'value', 'imxs') - ->orWhereLike('field', 'value'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$and' => array( - array( - 'field' => new MongoRegex('/value$/imxsu'), - ), - array( - 'field' => new MongoRegex('/^value/imxsu'), - 'other' => new MongoRegex('/^value$/imxs'), - ) - ), - ) - ) - ), - array( - '$and' => array( - array( - 'field' => new MongoRegex('/^value$/imxsu'), - ), - ), - ), - ), - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereRegex() - { - $this->query->whereRegex('field', '/^value/imxsu'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('field' => new MongoRegex('/^value/imxsu')) - ), - ), - ), - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereRegex() - { - $this->query->whereRegex('field', '/^value/imxsu') - ->orWhereRegex('field', '/value$/imxsu'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('field' => new MongoRegex('/^value/imxsu')) - ), - ), - array( - '$and' => array( - array('field' => new MongoRegex('/value$/imxsu')) - ), - ), - ), - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereRegex() - { - $this->query->whereRegex('field', '/^value/imxsu') - ->andWhereRegex('other', '/value$/imxsu'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - 'field' => new MongoRegex('/^value/imxsu'), - 'other' => new MongoRegex('/value$/imxsu'), - ), - ), - ), - ), - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereExists() - { - $this->query->whereExists('name'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$exists' => true)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereExists() - { - $this->query->andWhereExists('name'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$exists' => true)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereExists() - { - $this->query->whereExists('name') - ->orWhereExists('name'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$exists' => true)), - ) - ), - array( - '$and' => array( - array('name' => array('$exists' => true)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereNotExists() - { - $this->query->whereNotExists('name'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$exists' => false)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereNotExists() - { - $this->query->andWhereNotExists('name'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$exists' => false)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereNotExists() - { - $this->query->whereNotExists('name') - ->orWhereNotExists('name'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$exists' => false)), - ) - ), - array( - '$and' => array( - array('name' => array('$exists' => false)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereIn() - { - $this->query->whereIn('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$in' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereIn() - { - $this->query->andWhereIn('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$in' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereIn() - { - $this->query->whereIn('name', array('key' => 1, 2, '3')) - ->orWhereIn('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$in' => array(1, 2, '3'))), - ) - ), - array( - '$and' => array( - array('name' => array('$in' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereAll() - { - $this->query->whereAll('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$all' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereAll() - { - $this->query->andWhereAll('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$all' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereAll() - { - $this->query->whereAll('name', array('key' => 1, 2, '3')) - ->orWhereAll('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$all' => array(1, 2, '3'))), - ) - ), - array( - '$and' => array( - array('name' => array('$all' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereNotIn() - { - $this->query->whereNotIn('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$nin' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereNotIn() - { - $this->query->andWhereNotIn('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$nin' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereNotIn() - { - $this->query->whereNotIn('name', array('key' => 1, 2, '3')) - ->orWhereNotIn('name', array('key' => 1, 2, '3')); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$nin' => array(1, 2, '3'))), - ) - ), - array( - '$and' => array( - array('name' => array('$nin' => array(1, 2, '3'))), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereSize() - { - $this->query->whereSize('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$size' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereSize() - { - $this->query->andWhereSize('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$size' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereSize() - { - $this->query->whereSize('name', 10) - ->orWhereSize('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$size' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$size' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereType() - { - $this->query->whereType('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$type' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - /** + protected $query; + + public function setUp() + { + $this->query = new League\Monga\Query\Where(); + } + + public function getProperty($property) + { + $reflection = new ReflectionObject($this->query); + $property = $property = $reflection->getProperty($property); + $property->setAccessible(true); + return $property->getValue($this->query); + } + + public function testSetWhere() + { + $this->assertNull($this->getProperty('where')); + $this->query->setWhere(array('name' => 'John')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => 'John'), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + $this->query->setWhere(array()); + $this->assertNull($this->getProperty('where')); + } + + public function testWhere() + { + $this->query->where('name', 'John'); + $this->query->where(array( + 'surname' => 'Doe', + 'age' => 25, + )); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + 'name' => 'John', + 'surname' => 'Doe', + 'age' => 25, + ), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhere() + { + $this->query->andWhere('name', 'John'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => 'John'), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhere() + { + $this->query->where('name', 'John') + ->orWhere('name', 'Steve') + ->orWhere(array('name' => 'Jack', 'surname' => 'Johnes')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => 'John'), + ) + ), + array( + '$and' => array( + array('name' => 'Steve'), + ) + ), + array( + '$and' => array( + array('name' => 'Jack'), + ) + ), + array( + '$and' => array( + array('surname' => 'Johnes'), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereNot() + { + $this->query->whereNot('name', 'John'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$ne' => 'John')), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereNot() + { + $this->query->andWhereNot('name', 'John'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$ne' => 'John')), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereNot() + { + $this->query->whereNot('name', 'John') + ->orWhereNot('name', 'Steve'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$ne' => 'John')), + ) + ), + array( + '$and' => array( + array('name' => array('$ne' => 'Steve')), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereLike() + { + $this->query->whereLike('field', '%value') + ->whereLike('field', 'value%') + ->whereLike('other', 'value', 'imxs'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$and' => array( + array( + 'field' => new MongoRegex('/value$/imxsu'), + ), + array( + 'field' => new MongoRegex('/^value/imxsu'), + 'other' => new MongoRegex('/^value$/imxs'), + ) + ), + ) + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereLike() + { + $this->query->whereLike('field', '%value') + ->andWhereLike('field', 'value%') + ->andWhereLike('other', 'value', 'imxs'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$and' => array( + array( + 'field' => new MongoRegex('/value$/imxsu'), + ), + array( + 'field' => new MongoRegex('/^value/imxsu'), + 'other' => new MongoRegex('/^value$/imxs'), + ) + ), + ) + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereLike() + { + $this->query->whereLike('field', '%value') + ->andWhereLike('field', 'value%') + ->andWhereLike('other', 'value', 'imxs') + ->orWhereLike('field', 'value'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$and' => array( + array( + 'field' => new MongoRegex('/value$/imxsu'), + ), + array( + 'field' => new MongoRegex('/^value/imxsu'), + 'other' => new MongoRegex('/^value$/imxs'), + ) + ), + ) + ) + ), + array( + '$and' => array( + array( + 'field' => new MongoRegex('/^value$/imxsu'), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereRegex() + { + $this->query->whereRegex('field', '/^value/imxsu'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('field' => new MongoRegex('/^value/imxsu')) + ), + ), + ), + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereRegex() + { + $this->query->whereRegex('field', '/^value/imxsu') + ->orWhereRegex('field', '/value$/imxsu'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('field' => new MongoRegex('/^value/imxsu')) + ), + ), + array( + '$and' => array( + array('field' => new MongoRegex('/value$/imxsu')) + ), + ), + ), + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereRegex() + { + $this->query->whereRegex('field', '/^value/imxsu') + ->andWhereRegex('other', '/value$/imxsu'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + 'field' => new MongoRegex('/^value/imxsu'), + 'other' => new MongoRegex('/value$/imxsu'), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereExists() + { + $this->query->whereExists('name'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$exists' => true)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereExists() + { + $this->query->andWhereExists('name'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$exists' => true)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereExists() + { + $this->query->whereExists('name') + ->orWhereExists('name'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$exists' => true)), + ) + ), + array( + '$and' => array( + array('name' => array('$exists' => true)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereNotExists() + { + $this->query->whereNotExists('name'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$exists' => false)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereNotExists() + { + $this->query->andWhereNotExists('name'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$exists' => false)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereNotExists() + { + $this->query->whereNotExists('name') + ->orWhereNotExists('name'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$exists' => false)), + ) + ), + array( + '$and' => array( + array('name' => array('$exists' => false)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereIn() + { + $this->query->whereIn('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$in' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereIn() + { + $this->query->andWhereIn('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$in' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereIn() + { + $this->query->whereIn('name', array('key' => 1, 2, '3')) + ->orWhereIn('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$in' => array(1, 2, '3'))), + ) + ), + array( + '$and' => array( + array('name' => array('$in' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereAll() + { + $this->query->whereAll('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$all' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereAll() + { + $this->query->andWhereAll('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$all' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereAll() + { + $this->query->whereAll('name', array('key' => 1, 2, '3')) + ->orWhereAll('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$all' => array(1, 2, '3'))), + ) + ), + array( + '$and' => array( + array('name' => array('$all' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereNotIn() + { + $this->query->whereNotIn('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$nin' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereNotIn() + { + $this->query->andWhereNotIn('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$nin' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereNotIn() + { + $this->query->whereNotIn('name', array('key' => 1, 2, '3')) + ->orWhereNotIn('name', array('key' => 1, 2, '3')); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$nin' => array(1, 2, '3'))), + ) + ), + array( + '$and' => array( + array('name' => array('$nin' => array(1, 2, '3'))), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereSize() + { + $this->query->whereSize('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$size' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereSize() + { + $this->query->andWhereSize('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$size' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereSize() + { + $this->query->whereSize('name', 10) + ->orWhereSize('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$size' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$size' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereType() + { + $this->query->whereType('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$type' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + /** * @expectedException InvalidArgumentException */ - public function testInvalidWhereType() - { - $this->query->whereType('name', 'invalid'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$type' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereType() - { - $this->query->andWhereType('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$type' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereType() - { - $this->query->whereType('name', 10) - ->orWhereType('name', 'string'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$type' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$type' => 2)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereLt() - { - $this->query->whereLt('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereLt() - { - $this->query->andWhereLt('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereLt() - { - $this->query->whereLt('name', 10) - ->orWhereLt('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lt' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$lt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereLte() - { - $this->query->whereLte('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lte' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereLte() - { - $this->query->andWhereLte('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lte' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereLte() - { - $this->query->whereLte('name', 10) - ->orWhereLte('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lte' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$lte' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereGt() - { - $this->query->whereGt('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$gt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereGt() - { - $this->query->andWhereGt('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$gt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereGt() - { - $this->query->whereGt('name', 10) - ->orWhereGt('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$gt' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$gt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereGte() - { - $this->query->whereGte('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$gte' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereGte() - { - $this->query->andWhereGte('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$gte' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereGte() - { - $this->query->whereGte('name', 10) - ->orWhereGte('name', 10); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$gte' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$gte' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereNear() - { - $this->query->whereNear('location', 10, 10, array('$maxDistance' => 5)); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereNear() - { - $this->query->andWhereNear('location', 10, 10, array('$maxDistance' => 5)); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereNear() - { - $this->query->whereNear('location', 10, 10, array('$maxDistance' => 5)) - ->orWhereNear('location', 10, 10, array('$maxDistance' => 5)); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), - ) - ), - array( - '$and' => array( - array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereWithin() - { - $this->query->whereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereWithin() - { - $this->query->andWhereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereWithin() - { - $this->query->whereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)) - ->orWhereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), - ) - ), - array( - '$and' => array( - array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereBetween() - { - $this->query->whereBetween('name', 10, 15); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lt' => 15, '$gt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereBetween() - { - $this->query->andWhereBetween('name', 10, 15); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lt' => 15, '$gt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereBetween() - { - $this->query->whereBetween('name', 10, 15) - ->orWhereBetween('name', 10, 15); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('name' => array('$lt' => 15, '$gt' => 10)), - ) - ), - array( - '$and' => array( - array('name' => array('$lt' => 15, '$gt' => 10)), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testWhereId() - { - $this->query->whereId('50a2cdf711fa67c551000001'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('_id' => new MongoId('50a2cdf711fa67c551000001')), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testAndWhereId() - { - $this->query->andWhereId('50a2cdf711fa67c551000001'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('_id' => new MongoId('50a2cdf711fa67c551000001')), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrWhereId() - { - $this->query->orWhereId('50a2cdf711fa67c551000001') - ->orWhereId(new MongoId('50a2cdf711fa67c551000001'), 'id'); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('_id' => new MongoId('50a2cdf711fa67c551000001')), - ) - ), - array( - '$and' => array( - array('id' => new MongoId('50a2cdf711fa67c551000001')), - ) - ) - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testNorWhere() - { - $this->query->norWhere(function($query){ - $query->where('field', 'value'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$nor' => array( - array('field' => 'value'), - ), - ), - ) - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testEmptyNorWhere() - { - $this->query->norWhere(function(){}); - $this->assertNull($this->getProperty('where')); - } - - /** + public function testInvalidWhereType() + { + $this->query->whereType('name', 'invalid'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$type' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereType() + { + $this->query->andWhereType('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$type' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereType() + { + $this->query->whereType('name', 10) + ->orWhereType('name', 'string'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$type' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$type' => 2)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereLt() + { + $this->query->whereLt('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereLt() + { + $this->query->andWhereLt('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereLt() + { + $this->query->whereLt('name', 10) + ->orWhereLt('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lt' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$lt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereLte() + { + $this->query->whereLte('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lte' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereLte() + { + $this->query->andWhereLte('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lte' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereLte() + { + $this->query->whereLte('name', 10) + ->orWhereLte('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lte' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$lte' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereGt() + { + $this->query->whereGt('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$gt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereGt() + { + $this->query->andWhereGt('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$gt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereGt() + { + $this->query->whereGt('name', 10) + ->orWhereGt('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$gt' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$gt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereGte() + { + $this->query->whereGte('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$gte' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereGte() + { + $this->query->andWhereGte('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$gte' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereGte() + { + $this->query->whereGte('name', 10) + ->orWhereGte('name', 10); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$gte' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$gte' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereNear() + { + $this->query->whereNear('location', 10, 10, array('$maxDistance' => 5)); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereNear() + { + $this->query->andWhereNear('location', 10, 10, array('$maxDistance' => 5)); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereNear() + { + $this->query->whereNear('location', 10, 10, array('$maxDistance' => 5)) + ->orWhereNear('location', 10, 10, array('$maxDistance' => 5)); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), + ) + ), + array( + '$and' => array( + array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereWithin() + { + $this->query->whereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereWithin() + { + $this->query->andWhereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereWithin() + { + $this->query->whereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)) + ->orWhereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5)); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), + ) + ), + array( + '$and' => array( + array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereBetween() + { + $this->query->whereBetween('name', 10, 15); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lt' => 15, '$gt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereBetween() + { + $this->query->andWhereBetween('name', 10, 15); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lt' => 15, '$gt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereBetween() + { + $this->query->whereBetween('name', 10, 15) + ->orWhereBetween('name', 10, 15); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('name' => array('$lt' => 15, '$gt' => 10)), + ) + ), + array( + '$and' => array( + array('name' => array('$lt' => 15, '$gt' => 10)), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testWhereId() + { + $this->query->whereId('50a2cdf711fa67c551000001'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('_id' => new MongoId('50a2cdf711fa67c551000001')), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testAndWhereId() + { + $this->query->andWhereId('50a2cdf711fa67c551000001'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('_id' => new MongoId('50a2cdf711fa67c551000001')), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrWhereId() + { + $this->query->orWhereId('50a2cdf711fa67c551000001') + ->orWhereId(new MongoId('50a2cdf711fa67c551000001'), 'id'); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('_id' => new MongoId('50a2cdf711fa67c551000001')), + ) + ), + array( + '$and' => array( + array('id' => new MongoId('50a2cdf711fa67c551000001')), + ) + ) + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testNorWhere() + { + $this->query->norWhere(function ($query) { + $query->where('field', 'value'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$nor' => array( + array('field' => 'value'), + ), + ), + ) + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testEmptyNorWhere() + { + $this->query->norWhere(function () {}); + $this->assertNull($this->getProperty('where')); + } + + /** * @expectedException InvalidArgumentException */ - public function testInvalidNorWhere() - { - $this->query->norWhere(false); - } - - public function testAndNorWhere() - { - $this->query->andNorWhere(function($query){ - $query->where('field', 'value'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$nor' => array( - array('field' => 'value'), - ), - ), - ) - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrNorWhere() - { - $this->query->norWhere(function($query){ - $query->where('field', 'value'); - }) - ->orNorWhere(function($query){ - $query->where('field', 'value'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$nor' => array( - array('field' => 'value'), - ), - ), - ) - ), - array( - '$and' => array( - array( - '$nor' => array( - array('field' => 'value'), - ), - ), - ) - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testNotWhere() - { - $this->query->notWhere(function($query){ - $query->where('field', 'value'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$not' => array( - array('field' => 'value'), - ), - ), - ) - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testEmptyNotWhere() - { - $this->query->notWhere(function(){}); - $this->assertNull($this->getProperty('where')); - } - - /** + public function testInvalidNorWhere() + { + $this->query->norWhere(false); + } + + public function testAndNorWhere() + { + $this->query->andNorWhere(function ($query) { + $query->where('field', 'value'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$nor' => array( + array('field' => 'value'), + ), + ), + ) + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrNorWhere() + { + $this->query->norWhere(function ($query) { + $query->where('field', 'value'); + }) + ->orNorWhere(function ($query) { + $query->where('field', 'value'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$nor' => array( + array('field' => 'value'), + ), + ), + ) + ), + array( + '$and' => array( + array( + '$nor' => array( + array('field' => 'value'), + ), + ), + ) + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testNotWhere() + { + $this->query->notWhere(function ($query) { + $query->where('field', 'value'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$not' => array( + array('field' => 'value'), + ), + ), + ) + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testEmptyNotWhere() + { + $this->query->notWhere(function () {}); + $this->assertNull($this->getProperty('where')); + } + + /** * @expectedException InvalidArgumentException */ - public function testInvalidNotWhere() - { - $this->query->notWhere(false); - } - - public function testAndNotWhere() - { - $this->query->andNotWhere(function($query){ - $query->where('field', 'value'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$not' => array( - array('field' => 'value'), - ), - ), - ) - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testOrNotWhere() - { - $this->query->notWhere(function($query){ - $query->where('field', 'value'); - }) - ->orNotWhere(function($query){ - $query->where('field', 'value'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$not' => array( - array('field' => 'value'), - ), - ), - ) - ), - array( - '$and' => array( - array( - '$not' => array( - array('field' => 'value'), - ), - ), - ) - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } - - public function testGetWhere() - { - $this->assertEquals(array(), $this->query->getWhere()); - $this->query->where('this', 'that'); - $this->assertEquals(array('this' => 'that'), $this->query->getWhere()); - $this->query->orWhere(function($query){ - $query->where('one', 1) - ->orWhere('two', 2); - }); - $this->query->where(function(){}); - $this->assertEquals(array( - '$or' => array( - array('this' => 'that'), - array( - '$or' => array( - array('one' => 1), - array('two' => 2), - ) - ), - ), - ), $this->query->getWhere()); - - $this->query = new League\Monga\Query\Where(); - - $this->query->where(function($query){ - $query->where('one', 1) - ->orWhere('two', 2); - }); - - $this->assertEquals(array( - '$or' => array( - array('one' => 1), - array('two' => 2), - ), - ), $this->query->getWhere()); - - $this->query = new League\Monga\Query\Where(); - - $this->query->setWhere(array( - '$or' => array( - array('$and' => array( - array('$or' => array( - array('this' => 'that'), - )), - )) - ) - )); - - $this->assertEquals(array('this' => 'that'), $this->query->getWhere()); - - $this->query->setWhere(array()); - - $this->query->norWhere(function($query){ - $query->where('one', 1) - ->where('two', 2); - })->norWhere(function($query){ - $query->where('three', 1) - ->where('four', 2); - }); - - $this->assertEquals(array( - '$nor' => array( - array('one' => 1, 'two' => 2), - array('three' => 1, 'four' => 2), - )), $this->query->getWhere()); - - $this->query->setWhere(array()); - $this->query->where('name', 'john') - ->where('name', 'jim') - ->where(function($query){ - $query->where('this', 'one') - ->where('is', 'tricky'); - }); - - $this->assertEquals(array( - '$and' => array( - array( - '$and' => array( - array('name' => 'john'), - array('name' => 'jim'), - ), - ), - array( - 'this' => 'one', - 'is' => 'tricky', - ), - ), - ), $this->query->getWhere()); - - $this->query->setWhere(array( - '$or' => array( - array( - '$and' => array( - array( - '$and' => array( - array( - 'name' => 'Frank', - 'surname' => 'de Jonge', - ), - ), - ), - ), - ), - ), - )); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array( - '$and' => array( - array( - 'name' => 'Frank', - 'surname' => 'de Jonge', - ), - array( - 'name' => 'Billy', - ), - ), - ), - ), - ), - ), - ); - - $this->query->where('name', 'Billy'); - } - - public function testOrWhereClosure() - { - $this->query->orWhere(function($query){ - $query->where('something', 'broken'); - }); - - $expected = array( - '$or' => array( - array( - '$and' => array( - array('something' => 'broken') - ), - ), - ) - ); - - $this->assertEquals($expected, $this->getProperty('where')); - } -} \ No newline at end of file + public function testInvalidNotWhere() + { + $this->query->notWhere(false); + } + + public function testAndNotWhere() + { + $this->query->andNotWhere(function ($query) { + $query->where('field', 'value'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$not' => array( + array('field' => 'value'), + ), + ), + ) + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testOrNotWhere() + { + $this->query->notWhere(function ($query) { + $query->where('field', 'value'); + }) + ->orNotWhere(function ($query) { + $query->where('field', 'value'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$not' => array( + array('field' => 'value'), + ), + ), + ) + ), + array( + '$and' => array( + array( + '$not' => array( + array('field' => 'value'), + ), + ), + ) + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } + + public function testGetWhere() + { + $this->assertEquals(array(), $this->query->getWhere()); + $this->query->where('this', 'that'); + $this->assertEquals(array('this' => 'that'), $this->query->getWhere()); + $this->query->orWhere(function ($query) { + $query->where('one', 1) + ->orWhere('two', 2); + }); + $this->query->where(function () {}); + $this->assertEquals(array( + '$or' => array( + array('this' => 'that'), + array( + '$or' => array( + array('one' => 1), + array('two' => 2), + ) + ), + ), + ), $this->query->getWhere()); + + $this->query = new League\Monga\Query\Where(); + + $this->query->where(function ($query) { + $query->where('one', 1) + ->orWhere('two', 2); + }); + + $this->assertEquals(array( + '$or' => array( + array('one' => 1), + array('two' => 2), + ), + ), $this->query->getWhere()); + + $this->query = new League\Monga\Query\Where(); + + $this->query->setWhere(array( + '$or' => array( + array('$and' => array( + array('$or' => array( + array('this' => 'that'), + )), + )) + ) + )); + + $this->assertEquals(array('this' => 'that'), $this->query->getWhere()); + + $this->query->setWhere(array()); + + $this->query->norWhere(function ($query) { + $query->where('one', 1) + ->where('two', 2); + })->norWhere(function ($query) { + $query->where('three', 1) + ->where('four', 2); + }); + + $this->assertEquals(array( + '$nor' => array( + array('one' => 1, 'two' => 2), + array('three' => 1, 'four' => 2), + )), $this->query->getWhere()); + + $this->query->setWhere(array()); + $this->query->where('name', 'john') + ->where('name', 'jim') + ->where(function ($query) { + $query->where('this', 'one') + ->where('is', 'tricky'); + }); + + $this->assertEquals(array( + '$and' => array( + array( + '$and' => array( + array('name' => 'john'), + array('name' => 'jim'), + ), + ), + array( + 'this' => 'one', + 'is' => 'tricky', + ), + ), + ), $this->query->getWhere()); + + $this->query->setWhere(array( + '$or' => array( + array( + '$and' => array( + array( + '$and' => array( + array( + 'name' => 'Frank', + 'surname' => 'de Jonge', + ), + ), + ), + ), + ), + ), + )); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array( + '$and' => array( + array( + 'name' => 'Frank', + 'surname' => 'de Jonge', + ), + array( + 'name' => 'Billy', + ), + ), + ), + ), + ), + ), + ); + + $this->query->where('name', 'Billy'); + } + + public function testOrWhereClosure() + { + $this->query->orWhere(function ($query) { + $query->where('something', 'broken'); + }); + + $expected = array( + '$or' => array( + array( + '$and' => array( + array('something' => 'broken') + ), + ), + ) + ); + + $this->assertEquals($expected, $this->getProperty('where')); + } +}