Пример #1
0
 public function testFetchObjects()
 {
     $testEntity = new TestEntity($this->database, $this->metadata);
     $testEntity->primary = 1;
     $testEntity->testField = 1;
     $testEntity2 = new TestEntity($this->database, $this->metadata);
     $testEntity2->primary = 2;
     $testEntity2->testField = 2;
     $data = [$testEntity, $testEntity2];
     $stmt = $this->createMock(\PDOStatement::class);
     $stmt->method('fetchAll')->willReturn($data);
     $this->driver->method('query')->willReturn($stmt);
     $objects = $this->database->fetchObjects('SELECT column1, column2 FROM `table`', TestEntity::class, $this->metadata->primaryField);
     // Always return array
     static::assertTrue(is_array($objects));
     // Check that resulting array is grouped by primary keys
     static::assertArrayHasKey(1, $objects);
     static::assertArrayHasKey(2, $objects);
     static::assertArrayNotHasKey(99, $objects);
     // Check returned type
     static::assertInstanceOf(TestEntity::class, $objects[1]);
     static::assertInstanceOf(TestEntity::class, $objects[2]);
 }