/** * @expectedException \Packaged\Dal\Exceptions\DataStore\TooManyResultsException * @expectedExceptionMessage Too many results located */ public function testMultipleExistsFailure() { $datastore = new MockQlDataStore(); $connection = new MockPdoConnection(); $connection->config(); $datastore->setConnection($connection); $resolver = new DalResolver(); $resolver->boot(); $resolver->addDataStore('mockql', $datastore); $connection->setResolver($resolver); $u1 = new MockQlDao(); $u1->username = '******'; $u1->display = 'Test One'; $u1->save(); $u2 = new MockQlDao(); $u2->username = '******'; $u2->display = 'Test Two'; $u2->save(); $test = new MockNonUniqueKeyDao(); $test->username = '******'; $test->exists(); MockNonUniqueKeyDao::loadById('TestMultiple'); }
/** * @param bool $persistent * * @dataProvider switchDBProvider * @throws ConnectionException */ public function testSharedConnection($persistent) { $tmpConn = new MockPdoConnection(); $tmpConn->setResolver(new DalResolver()); $tmpConn->addConfig('options', [\PDO::ATTR_PERSISTENT => $persistent]); $numProcs = count($tmpConn->fetchQueryResults("SHOW FULL PROCESSLIST")); $tmpConn->runQuery("DROP DATABASE IF EXISTS packaged_dal_a"); $tmpConn->runQuery("DROP DATABASE IF EXISTS packaged_dal_b"); $tmpConn->runQuery("CREATE DATABASE packaged_dal_a"); $tmpConn->runQuery("CREATE DATABASE packaged_dal_b"); $tmpConn->runQuery("CREATE TABLE packaged_dal_a.table_a (id int, value varchar(200))"); $tmpConn->runQuery("CREATE TABLE packaged_dal_b.table_b (id int, value varchar(200))"); $tmpConn->runQuery("INSERT INTO packaged_dal_a.table_a VALUES (1, 'test_a')"); $tmpConn->runQuery("INSERT INTO packaged_dal_b.table_b VALUES (1, 'test_b')"); $conn1 = new MockPdoConnection(); $conn1->setResolver(new DalResolver()); $conn1->addConfig('options', [\PDO::ATTR_PERSISTENT => $persistent]); $conn1->addConfig('database', 'packaged_dal_a'); $conn2 = new MockPdoConnection(); $conn2->setResolver(new DalResolver()); $conn2->addConfig('options', [\PDO::ATTR_PERSISTENT => $persistent]); $conn2->addConfig('database', 'packaged_dal_b'); $conn1->connect(); $conn2->connect(); $testSelect = function (MockPdoConnection $conn, $isA) use($tmpConn, $persistent) { $tbl = 'table_' . ($isA ? 'a' : 'b'); $db = 'packaged_dal_' . ($isA ? 'a' : 'b'); $this->assertEquals([['id' => 1, 'value' => $isA ? 'test_a' : 'test_b']], $conn->fetchQueryResults("SELECT * FROM " . $tbl)); if ($persistent) { $this->assertEquals([['db' => $db]], $tmpConn->fetchQueryResults('SELECT DATABASE() AS db')); } }; $testSelect($conn1, true); $testSelect($conn2, false); $testSelect($conn1, true); $testSelect($conn2, false); $this->assertEquals($persistent ? $numProcs : $numProcs + 1, count($tmpConn->fetchQueryResults("SHOW FULL PROCESSLIST"))); $tmpConn->runQuery("DROP DATABASE IF EXISTS packaged_dal_a"); $tmpConn->runQuery("DROP DATABASE IF EXISTS packaged_dal_b"); }
public function testQueried() { Dao::setDalResolver(new DalResolver()); $datastore = new MockQlDataStore(); $connection = new MockPdoConnection(); $connection->config(); $datastore->setConnection($connection); MockQlDao::getDalResolver()->addDataStore('mockql', $datastore); $connection->setResolver(MockQlDao::getDalResolver()); $u = new MockQlDao(); $datastore->getConnection()->connect()->runQuery("TRUNCATE " . $u->getTableName()); $collection = MockQlDao::collection(); $this->assertNull($collection->min('id')); $this->assertNull($collection->max('id')); $this->assertNull($collection->avg('id')); $this->assertNull($collection->sum('id')); $this->assertEmpty($collection->distinct('username')); $u->display = 'queried'; $u->username = '******'; $u->id = 1; $u->save(); $u->markDaoAsLoaded(false); $u->username = '******'; $u->id = 2; $u->save(); $u->markDaoAsLoaded(false); $u->username = '******'; $u->id = 5; $u->save(); $u->markDaoAsLoaded(false); $u->username = '******'; $u->id = 8; $u->save(); $this->assertEquals(4, $collection->count()); $this->assertEquals(1, $collection->min('id')); $this->assertEquals(8, $collection->max('id')); $this->assertEquals(4, $collection->avg('id')); $this->assertEquals(16, $collection->sum('id')); $this->assertEquals(["Test", "User", "Testing"], array_values($collection->distinct('username'))); $collection->loadWhere(['username' => 'Test']); $this->assertCount(2, $collection); $collection = MockQlDao::collection(['username' => 'Test']); $this->assertCount(2, $collection->getRawArray()); $first = MockQlDao::collection(['username' => 'Test'])->first(); $this->assertInstanceOf(MockQlDao::class, $first); $first = MockQlDao::collection(['username' => 'NotExisting'])->first('abc'); $this->assertEquals('abc', $first); $count = MockQlDao::collection(['username' => 'NotExisting'])->orderBy('username')->count(); $this->assertEquals(0, $count); $col = MockQlDao::collection(['username' => 'Test'])->limit(10); $first = $col->first(); $this->assertTrue($col->hasClause('LIMIT')); $limit = $col->getClause('LIMIT'); if ($limit instanceof LimitClause) { $this->assertEquals(10, $limit->getLimit()->getValue()); } $this->assertInstanceOf(MockQlDao::class, $first); $col = MockQlDao::collection(['username' => 'Test'])->load(); $this->assertCount(2, $collection); $first = $col->first(); $this->assertInstanceOf(MockQlDao::class, $first); $countCollection = MockQlDao::collection(); $countCollection->limit(1); $this->assertEquals(1, $countCollection->count()); $countCollection->limit(0); $this->assertEquals(0, $countCollection->count()); $datastore->getConnection()->runQuery("TRUNCATE " . $u->getTableName()); Dao::unsetDalResolver(); }
public function testCounters() { $datastore = new MockQlDataStore(); $connection = new MockPdoConnection(); $connection->config(); $datastore->setConnection($connection); $connection->connect(); $resolver = new DalResolver(); $resolver->boot(); $connection->setResolver($resolver); Dao::getDalResolver()->addDataStore('mockql', $datastore); $datastore->getConnection()->runQuery('TRUNCATE TABLE mock_counter_daos'); $dao = MockCounterDao::loadOrNew('test1'); $dao->c1->increment(10); $dao->c1->decrement(5); $datastore->save($dao); $dao->c2->increment(1); $dao->c2->decrement(3); $datastore->save($dao); $dao = new MockCounterDao(); $dao->id = 'test1'; $dao->c2->increment(1); $dao->c2->decrement(3); $datastore->save($dao); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(5, $dao->c1->calculated()); $this->assertEquals(-4, $dao->c2->calculated()); $dao = MockCounterDao::loadById('test1'); $dao->c1->increment(0); $datastore->save($dao); $dao->c1->increment(10); $datastore->save($dao); $dao->c1->increment(15); $datastore->save($dao); $dao->c3->increment(8); $datastore->save($dao); $dao->c3->increment(9.699999999999999); $datastore->save($dao); $dao->c3->increment(99); $datastore->save($dao); $dao->c3->increment(1.3); $datastore->save($dao); $dao->c3->increment(0.0); $datastore->save($dao); $dao->c3->increment(null); $datastore->save($dao); $dao->c3->increment('goat'); $datastore->save($dao); $dao->c3->increment('99 a'); $datastore->save($dao); $dao->c2->increment(0); $datastore->save($dao); $dao1 = MockCounterDao::loadById('test1'); $dao1->c1->increment(7); $dao2 = MockCounterDao::loadById('test1'); $dao2->c1->increment(3); $dao1->save(); $dao2->save(); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(40, $dao->c1->calculated()); $this->assertEquals(217, $dao->c3->calculated()); $dao = new MockCounterDao(); $dao->id = 'test1'; $dao->markDaoAsLoaded(); $dao->markDaoDatasetAsSaved(); $dao->c3->increment('0.00'); $datastore->save($dao); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(217, $dao->c3->calculated()); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(40, $dao->c1->calculated()); $this->assertEquals(-4, $dao->c2->calculated()); $this->assertEquals(217, $dao->c3->calculated()); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(40, $dao->c1->calculated()); $dao->c1 = 100; $dao->save(); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(100, $dao->c1->calculated()); $dao->c1->setValue(500); $dao->save(); $dao = MockCounterDao::loadById('test1'); $this->assertEquals(500, $dao->c1->calculated()); $json = json_encode($dao); $this->assertEquals('{"id":"test1","c1":"500","c2":"-4","c3":"217.00"}', $json); $dao = new MockCounterDao(); $dao->id = 'test1'; $dao->c1->setValue(6); $dao->c2->setValue(-8); $datastore->save($dao); $json = json_encode($dao); $this->assertEquals('{"id":"test1","c1":"6","c2":"-8","c3":"0"}', $json); }
protected function _getStatement($query) { $res = parent::_getStatement($query); $this->_lastQueryCacheKey = $this->_stmtCacheKey($query); return $res; }