public function run() { $this->prepareTables(); $repeats = $this->getRepeats(); $sql = 'SELECT txt FROM test'; $bar = new CliProgressBar($repeats); for ($i = 1; $i <= $repeats; ++$i) { Timer::start(); $result = ODb::rows($sql); foreach ($result as $value) { } Timer::stop(); $bar->update($i); } $this->addResult('non-generator', Timer::get()); Timer::reset(); $bar = new CliProgressBar($repeats); // Db class initialization $pool = new ConnectionPool(); $conn = new PdoConnection('master'); $conn->setDsn("mysql:host=localhost;dbname={$this->database};charset=utf8")->setUserName('root')->setPassword('')->setOptions([PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]); $pool->addConnection($conn, true); PdoAdapter::setPool($pool); Db::setAdapter(PdoAdapter::instance()); for ($i = 1; $i <= $repeats; ++$i) { Timer::start(); $result = Db::rows($sql); foreach ($result as $value) { } Timer::stop(); $bar->update($i); } $this->addResult('generator', Timer::get()); $this->cleanup(); }
/** * @covers Veles\Auth\Strategies\CookieStrategy::identify * @covers Veles\Auth\Strategies\AbstractAuthStrategy::findUser * @dataProvider identifyProvider * * @param $id * @param $hash * @param $expected * @param $user_result */ public function testIdentify($id, $hash, $expected, $user_result) { $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['row'])->getMock(); $adapter->expects($this->once())->method('row')->willReturn($user_result); Db::setAdapter($adapter); $object = new CookieStrategy($id, $hash, new User()); $actual = $object->identify(); $msg = 'CookieStrategy::identify() returns wrong result!'; $this->assertSame($expected, $actual, $msg); }
/** * @covers Veles\Auth\Strategies\LoginFormStrategy::identify * @covers Veles\Auth\Strategies\AbstractAuthStrategy::setCookie * @covers Veles\Auth\Strategies\AbstractAuthStrategy::delCookie * @dataProvider identifyProvider * * @param $mail * @param $pass * @param $expected */ public function testIdentify($mail, $pass, $expected) { $user_result = ['id' => 1, 'email' => '*****@*****.**', 'hash' => '$2a$07$usesomesillystringforeGlOaUExBSD9HxuEYk2ZFaeDhggU716O', 'group' => 'uzzy', 'last_login' => '1980-12-12']; $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['row'])->getMock(); $adapter->expects($this->once())->method('row')->willReturn($user_result); Db::setAdapter($adapter); $object = new LoginFormStrategy($mail, $pass, new User()); $result = $object->identify(); $msg = 'LoginFormStrategy::identify() returns wrong result!'; $this->assertSame($expected, $result, $msg); }
/** * @covers Veles\Model\QueryBuilder::update * @covers Veles\Model\QueryBuilder::sanitize */ public function testUpdate() { $group = UsrGroup::GUEST; $hash = md5('lalala'); $user = new User(); $user->id = 1; $user->email = '*****@*****.**'; $user->hash = $hash; $user->group = $group; $user->last_login = '******'; $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['escape'])->getMock(); $adapter->expects($this->exactly(3))->method('escape')->willReturn('\'escaped-string\''); Db::setAdapter($adapter); $expected = "\n\t\t\tUPDATE\n\t\t\t\t\"users\"\n\t\t\tSET\n\t\t\t\t\"email\" = 'escaped-string', \"hash\" = 'escaped-string', \"group\" = 16, \"last_login\" = 'escaped-string'\n\t\t\tWHERE\n\t\t\t\tid = 1\n\t\t"; $actual = $this->object->update($user); $msg = 'QueryBuilder::update() returns wrong result!'; $this->assertSame($expected, $actual, $msg); }
/** * Функция безопасности переменных * @param ActiveRecord $model * @param $property * @throws Exception * @return mixed */ private function sanitize(ActiveRecord $model, $property) { if (!isset($model->{$property})) { return null; } switch ($model->getMap()[$property]) { case 'int': $value = (int) $model->{$property}; break; case 'float': $value = (double) $model->{$property}; break; case 'string': $value = Db::escape($model->{$property}); break; default: $value = null; break; } return $value; }
public function run() { $this->prepareTables(); $repeats = $this->getRepeats(); $value1 = 'string one'; $value2 = 'string two'; $value3 = 'string three'; $bar = new CliProgressBar($repeats); for ($i = 1; $i <= $repeats; ++$i) { Timer::start(); $sql = ' INSERT INTO test (txt) VALUES (?), (?), (?) '; Db::query($sql, [$value1, $value2, $value3]); Timer::stop(); $bar->update($i); } $this->addResult('Real', Timer::get()); PdoAdapter::instance()->getConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); Timer::reset(); $bar = new CliProgressBar($repeats); for ($i = 1; $i <= $repeats; ++$i) { Timer::start(); $sql = ' INSERT INTO test (txt) VALUES (?), (?), (?) '; Db::query($sql, [$value1, $value2, $value3]); Timer::stop(); $bar->update($i); } $this->addResult('Emulated', Timer::get()); $this->cleanup(); }
/** * Query with pagination * * @param string $sql Query * @param bool|DbPaginator $pager Pagination object * * @todo Add Pager type hints * @return array|bool */ public function query($sql, $pager = false) { if ($pager instanceof DbPaginator) { $sql = $this->builder->setPage($sql, $pager); } $result = Db::rows($sql); if (empty($result)) { return false; } if ($pager instanceof DbPaginator) { $pager->calcMaxPages(); } return $result; }
/** * Pages quantity calculation */ public function calcMaxPages() { $this->page_nums = (int) ceil(Db::getFoundRows() / $this->getLimit()); }
public static final function cleanup() { $sql = 'DROP TABLE test'; Db::query($sql); }
/** * @covers Veles\Model\User::getGroup */ public function testGetGroup() { $expected = 16; $actual = $this->object->getGroup(); $msg = 'User::getGroup() returns wrong result!'; $this->assertSame($expected, $actual, $msg); $expected = UsrGroup::ADMIN; $db_result = ['id' => 1, 'email' => '*****@*****.**', 'hash' => '$2a$07$usesomesillystringforeGlOaUExBSD9HxuEYk2ZFaeDhggU716O', 'group' => $expected, 'last_login' => 'string']; $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['row'])->getMock(); $adapter->expects($this->once())->method('row')->willReturn($db_result); Db::setAdapter($adapter); $this->object->getById(1); $actual = $this->object->getGroup(); $this->assertSame($expected, $actual, $msg); }
/** * @covers Veles\DataBase\DbPaginator::calcMaxPages */ public function testCalcMaxPages() { $expected = 3; $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['getFoundRows'])->getMock(); $adapter->expects($this->once())->method('getFoundRows')->willReturn(15); Db::setAdapter($adapter); $this->object->calcMaxPages(); $msg = 'Wrong DbPaginator::calcMaxPages() behavior!'; $this->assertAttributeSame($expected, 'page_nums', $this->object, $msg); }
/** * @covers Veles\DataBase\Db::escape */ public function testEscape() { $expected = 'string'; $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['escape'])->getMock(); $adapter->expects($this->once())->method('escape')->with($expected)->willReturn($adapter); Db::setAdapter($adapter); Db::escape($expected); }
/** * @covers Veles\Model\ActiveRecord::query * @dataProvider queryProvider * * @param $pager * @param $expected * @param $found_rows */ public function testQuery($pager, $expected, $found_rows) { $adapter = $this->getMockBuilder('\\Veles\\DataBase\\Adapters\\PdoAdapter')->setMethods(['rows', 'getFoundRows'])->getMock(); $adapter->expects($this->once())->method('rows')->willReturn($expected); if ($found_rows) { $adapter->expects($this->once())->method('getFoundRows')->willReturn($found_rows); } Db::setAdapter($adapter); $sql = "SELECT * FROM user"; $news = new News(); $result = $news->query($sql, $pager); $msg = 'ActiveRecord::query() returns wrong result!'; $this->assertSame($expected, $result, $msg); }