/** * Create a Database\Result object. * * @param resource $database The connection resource. * * @param FakeResult $resResult The database result. * * @param string $strQuery The query string. */ public function __construct($database, $resResult, $strQuery) { $this->database = $database; $this->result = $resResult; $this->query = $this->database->getQueryCollection()->findQuery($strQuery); $this->cursor = 0; }
/** * {@inheritdoc} * * @SuppressWarnings(PHPMD.CamelCaseMethodName) */ protected function insert_id() { $query = $this->database->getQueryCollection()->findQuery($this->strQuery); if ($query) { return $query->result()->getRow($query->result()->count()); } return null; }
/** * Mock a MetaModel instance. * * @return IMetaModel */ private function mockMetaModel() { $dataBase = Database::getNewTestInstance(); $serviceContainer = new MetaModelsServiceContainer(); $serviceContainer->setDatabase($dataBase); $metaModel = new MetaModel(array('id' => '1', 'sorting' => '1', 'tstamp' => '0', 'name' => 'MetaModel', 'tableName' => 'mm_test', 'mode' => '', 'translated' => '1', 'languages' => array('en' => array('isfallback' => '1'), 'de' => array('isfallback' => '')), 'varsupport' => '1')); $metaModel->setServiceContainer($serviceContainer); return $metaModel; }
/** * Mock a MetaModel. * * @param string $tableName The table name of the MetaModel to mock (optional, defaults to "mm_unittest"). * * @return IMetaModel */ protected function mockMetaModel($tableName = 'mm_unittest') { $metaModel = $this->getMock('MetaModels\\MetaModel', array('getTableName', 'getServiceContainer'), array(array())); $serviceContainer = new MetaModelsServiceContainer(); $serviceContainer->setDatabase(Database::getNewTestInstance())->setEventDispatcher(new EventDispatcher()); $metaModel->expects($this->any())->method('getTableName')->will($this->returnValue($tableName)); $metaModel->expects($this->any())->method('getServiceContainer')->will($this->returnValue($serviceContainer)); return $metaModel; }
/** * Test the build process. * * @return void */ public function testBuild() { $metaModel = new MetaModel(array('id' => '1', 'sorting' => '1', 'tstamp' => '0', 'name' => 'MetaModel', 'tableName' => 'mm_test', 'mode' => '', 'translated' => '1', 'languages' => array('en' => array('isfallback' => '1'), 'de' => array('isfallback' => '')), 'varsupport' => '1')); /** @var \MetaModels\Attribute\Base $attribute */ $attribute = $this->getMockForAbstractClass('MetaModels\\Attribute\\Base', array($metaModel, array('colname' => 'test1'))); $metaModel->addAttribute($attribute); $config = DefaultConfig::init(); $config->setFilter(array(array('operation' => '=', 'property' => 'foo', 'value' => 0))); $dataBase = Database::getNewTestInstance(); $builder = new FilterBuilderSql($metaModel->getTableName(), 'AND', $dataBase); $dataBase->getQueryCollection()->theQuery('SELECT id FROM mm_test WHERE ((test = ?))')->with(0)->result()->addRows(array(array('id' => 0), array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5))); $this->assertTrue($builder->isEmpty()); $this->assertEquals($builder, $builder->addChild(array('operation' => '=', 'property' => 'test', 'value' => 0))); $this->assertEquals(array(0, 1, 2, 3, 4, 5), $builder->build()->getMatchingIds()); }
/** * Test that the database can retrieve data. * * @return void */ public function testRetrieve() { $database = Database::getNewTestInstance(); $database->getQueryCollection()->theQuery('SELECT * FROM test WHERE id=?')->with(1)->result()->addRow(array('id' => 1, 'tstamp' => 343094400)); $statement = $database->prepare('SELECT * FROM test WHERE id=?'); $result = $statement->execute(1); $this->assertInstanceOf('MetaModels\\Test\\Contao\\Database\\Statement', $statement); $this->assertInstanceOf('Contao\\Database\\Statement', $statement); $this->assertInstanceOf('Database\\Statement', $statement); $this->assertInstanceOf('MetaModels\\Test\\Contao\\Database\\Result', $result); $this->assertInstanceOf('Contao\\Database\\Result', $result); $this->assertInstanceOf('Database\\Result', $result); $this->assertSame(1, $result->numRows); $this->assertSame(array('id' => 1, 'tstamp' => 343094400), $result->row()); $statement = $database->prepare('SELECT * FROM test WHERE id=?'); $result = $statement->execute(1); $counter = 0; while ($result->next()) { $counter++; } $this->assertEquals(1, $counter); }
/** * Ensure the getCount works correctly. * * @return void */ public function testGetCount() { $metaModel = $this->getMock('MetaModels\\MetaModel', array('getMatchingIds'), array(array('tableName' => 'mm_test_retrieve'))); $metaModel->expects($this->any())->method('getMatchingIds')->will($this->returnValue(array())); $this->assertEquals(0, $metaModel->getCount($metaModel->getEmptyFilter())); $metaModel = $this->getMock('MetaModels\\MetaModel', array('getMatchingIds'), array(array('tableName' => 'mm_test_retrieve'))); $metaModel->expects($this->any())->method('getMatchingIds')->will($this->returnValue(array(4, 3, 2, 1))); /** @var MetaModel $metaModel */ $database = Database::getNewTestInstance(); $metaModel->setServiceContainer($this->mockServiceContainer($database)); $database->getQueryCollection()->theQuery('SELECT COUNT(id) AS count FROM mm_test_retrieve WHERE id IN(?,?,?,?)')->with(4, 3, 2, 1)->result()->addRow(array('count' => 4)); $this->assertEquals(4, $metaModel->getCount($metaModel->getEmptyFilter())); }
/** * Set our database. * * @return void */ public function setUp() { Database::register(); }