public function testMutateFail() { $user = new User(); $user->name = 'michael'; $user->save(); $user = User::one(); $id = $user->getId(); $age = $user->age; $fail = $user->mutate(array('$set' => array('ages' => array('$inc' => 1)))); $this->assertFalse($fail); }
public function testAggregate() { $time = microtime(); $user = new User(array("time" => $time, "name" => "John", "money" => 100)); $user->save(); $user1 = new User(array("time" => $time, "name" => "John1", "money" => 100)); $user1->save(); $user2 = new User(array("time" => $time, "name" => "John2", "money" => 100)); $user2->save(); $user3 = new User(array("time" => $time, "name" => "John3", "money" => 100)); $user3->save(); $user4 = new User(array("age" => 40, "name" => "John4", "money" => 100)); $user4->save(); $user5 = new User(array("time" => $time, "name" => "John6", "money" => 100)); $user5->save(); $user6 = new User(array("time" => $time, "name" => "John6", "money" => 100)); $user6->save(); $result = User::aggregate(array(array('$match' => array("time" => $time)))); $this->assertCount(6, $result['result']); }
public function testChainedMethods() { $someUser = new User(); $result = $someUser->save(); $this->assertTrue($result); $someUser->val = 1; $result = $someUser->saveChain(); $this->assertInstanceOf('\\Purekid\\Mongodm\\Model', $result); $val = $someUser->setValChain(2)->saveChain()->setValChain(5)->saveChain()->getVal(); $this->assertEquals(5, $val); }
public function testToArrayRecursive() { $user = new User(array('name' => 'michael')); $user->save(); $book = $this->createBook(array('name' => 'book1', 'price' => 5)); $books = array($book); $user->books = $books; $user->save(); $id = $user->getId(); $user1 = User::id($id); $result = $user1->toArray(array('_type'), true); $this->assertEquals($result['books'][0]['name'], 'book1'); $this->assertNotEquals(count($result['books'][0]), count($user->books[0])); }
public function testReferences() { $expected = 'field'; $user = new User(); $user->save(); $id = $user->getId(); $ref1 = new Book(); $ref1->fieldMappingRefs = $expected; $ref1->save(); $ref2 = new Book(); $ref2->fieldMappingRefs = $expected . '2'; $ref2->save(); $user->fieldMappingRefs = Collection::make(array($ref1, $ref2)); $user->save(); $user = User::id($id); $refs = $user->fieldMappingRefs; $this->assertEquals(2, $refs->count()); $this->assertEquals($expected, $refs->get((string) $ref1->getId())->fieldMappingRefs); $this->assertEquals($expected . '2', $refs->get((string) $ref2->getId())->fieldMappingRefs); $userRaw = self::$db->getDB()->{User::$collection}->findOne(array('_id' => $id)); $book1Raw = self::$db->getDB()->{Book::$collection}->findOne(array('_id' => $ref1->getId())); $book2Raw = self::$db->getDB()->{Book::$collection}->findOne(array('_id' => $ref2->getId())); $this->assertArrayHasKey('field_mapping_refs', $userRaw, 'Field `fieldMappingRefs` was not mapped correctly'); $this->assertArrayHasKey('$ref', $userRaw['field_mapping_refs'][0], 'Field `fieldMappingRefs.0` was not mapped correctly'); $this->assertArrayHasKey('$id', $userRaw['field_mapping_refs'][0], 'Field `fieldMappingRefs.0` was not mapped correctly'); $this->assertEquals(Book::$collection, $userRaw['field_mapping_refs'][0]['$ref'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($ref1->getId(), $userRaw['field_mapping_refs'][0]['$id'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($expected, $book1Raw['field_mapping_refs']); $this->assertArrayHasKey('$ref', $userRaw['field_mapping_refs'][1], 'Field `fieldMappingRefs.1` was not mapped correctly'); $this->assertArrayHasKey('$id', $userRaw['field_mapping_refs'][1], 'Field `fieldMappingRefs.1` was not mapped correctly'); $this->assertEquals(Book::$collection, $userRaw['field_mapping_refs'][1]['$ref'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($ref2->getId(), $userRaw['field_mapping_refs'][1]['$id'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($expected . '2', $book2Raw['field_mapping_refs']); }