예제 #1
0
 public function testMutate()
 {
     $john = new User();
     $john->age = 18;
     $john->save();
     $user = User::one();
     $id = $user->getId();
     $age = $user->age;
     $user->mutate(array('$inc' => array('age' => 1)));
     $user = User::id($id);
     $this->assertEquals($age + 1, $user->age);
 }
예제 #2
0
 public function testOmitUndefinedObjectInstance()
 {
     $object = new \stdClass();
     $object->name = 'object';
     $user = User::one();
     $id = $user->getId();
     $user->tmpObject = $object;
     $user->save();
     $this->assertEquals($object, $user->tmpObject);
     $user = User::id($id);
     $this->assertNull($user->tmpObject);
 }
예제 #3
0
 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]));
 }
예제 #4
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']);
 }