public function testRelation1TO1() { $user = new Student(array("name" => "John")); $user->save(); $user = Student::one(); $id = $user->getId(); $this->assertInstanceOf("\\MongoId", $user->getId()); $book = new Book(); $bookName = "Book"; $book->name = $bookName; $book->save(); $this->assertInstanceOf("\\MongoId", $book->getId()); $user->book_fav = $book; $user->save(); $user = Student::id($id); $book_fav = $user->book_fav; $this->assertEquals($bookName, $book_fav->name); }
public function testRelation1TOMany() { $user = new User(array("age" => 40, "name" => "John")); $user->save(); $user = User::one(); $id = $user->getId(); $this->assertInstanceOf("\\MongoId", $user->getId()); $book1 = new Book(); $book1->name = "book1"; $book1->save(); $book2 = new Book(); $book2->name = "book2"; $book2->save(); $this->assertInstanceOf("\\MongoId", $book1->getId()); $this->assertInstanceOf("\\MongoId", $book2->getId()); $user->books = Collection::make(array($book1, $book2)); $user->save(); $user = User::id($id); $books = $user->books; $this->assertEquals(2, $books->count()); $this->assertEquals("book1", $books->get((string) $book1->getId())->name); $this->assertEquals("book2", $books->get((string) $book2->getId())->name); }
public function testDelete() { $books = Book::all(); $this->assertNotEmpty($books->count()); $books->delete(); $this->assertEquals($books->count(), 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']); }