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 testDistinct() { $key = time() . mt_rand(1000, 9999); $res = User::distinct($key, array()); $this->assertArrayHasKey('values', $res); $this->assertEquals(0, count($res['values'])); $user = User::one(); $user->age = 20; $user->{$key} = (string) $user->getId(); $user->save(); $res = User::distinct($key, array('age' => 20)); $this->assertEquals(1, count($res['values'])); $res = User::distinct($key, array('age' => 21)); $this->assertEquals(0, count($res['values'])); $user = new User(); $user->age = 21; $user->save(); $user->{$key} = (string) $user->getId(); $user->save(); $res = User::distinct($key, array()); $this->assertEquals(2, count($res['values'])); $res = User::distinct($key, array('age' => 20)); $this->assertEquals(1, count($res['values'])); }
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']); }