Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // Q32451215Test_02A_EmulatesOff__WithSuperUserAsBooleanParamType
- include 'P:/developer/xampp/htdocs/testmysql/Q32451215/IMysqlGeneralLogging.php';
- include 'P:/developer/xampp/htdocs/testmysql/Q32451215/MysqlGeneralLogging.php';
- class Q23451215Test extends PHPUnit_Framework_TestCase {
- /**
- * Test Data
- */
- protected $guid = '420D4B65565311E5958000FFD7CBE75F';
- protected $displayname = 'Example User 42';
- protected $hash = '$2y$17$12345678901234567890121234567890123456789012345678942';
- protected $salt = '$2y$17$1234567890123456789042$';
- protected $email = 'example42@example.com';
- protected $superuser = true;
- /**
- * I will also record all the return codes from the PDO functions
- * so that I can see what was returned with what happened vs what we expect.
- *
- * This will be useful with the later test.
- * Must be static so they are not reset with each test
- */
- protected static $rcActual = array(
- 'prepare' => false,
- 'execute' => false,
- 'exception' => false,
- 'exceptionMsg' => '',
- 'errorCode' => '', // return code from the statement or connection
- 'errorInfo' => '' // return message from the statement or connection
- );
- /**
- * Expected Results
- * Must be static so they are not reset with each test
- */
- protected static $rcExpected = array(
- 'prepare' => true,
- 'execute' => true,
- 'exception' => false,
- 'exceptionMsg' => '',
- );
- /**
- * Testing settings:
- *
- * PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
- * PDO::ATTR_EMULATE_PREPARES => FALSE;
- */
- /**
- * Expected Results:
- *
- * All Ok -
- *
- * 1) An inserted row of the supplied test data
- * 2) An 'INSERT' statement in the trace log
- */
- public function testEmululatesPreparesOff02A_WithSuperUserAsBooleanParamType() {
- echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
- $this->userPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->userPDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $testFilename = $this->logging->newTraceFilename($this->parseMethod(__METHOD__));
- $this->logging->setLogFile($testFilename);
- $this->logging->clearFileLog();
- $this->logging->startLoggingToFile(); // start logging...
- // --------------------------------------------------------
- var_dump(__METHOD__.__LINE__ .' PDO Param constants:',
- PDO::PARAM_BOOL, PDO::PARAM_INT, false, bin2hex(false));
- /*
- * The actual code to be tested is here...
- */
- try
- {
- self::$rcActual['exception'] = false;
- $stmt = $this->userPDO->prepare(
- "INSERT INTO `account`
- (`guid`, `displayname`, `password_hash`, `password_salt`, `email`, `superuser`)
- VALUES
- (UNHEX(:guid), :displayname, :passwordhash, :passwordsalt, :email, :superuser);");
- self::$rcActual['prepare'] = $stmt !== false;
- $stmt->bindValue(':guid', $this->guid, PDO::PARAM_STR);
- $stmt->bindValue(':displayname', $this->displayname, PDO::PARAM_STR);
- $stmt->bindValue(':passwordhash', $this->hash, PDO::PARAM_STR);
- $stmt->bindValue(':passwordsalt', $this->salt, PDO::PARAM_STR);
- $stmt->bindValue(':email', $this->email, PDO::PARAM_STR);
- $stmt->bindValue(':superuser', $this->superuser, PDO::PARAM_BOOL);
- self::$rcActual['execute'] = $stmt->execute();
- /*
- var_dump(__METHOD__.__LINE__ .'error codes',
- 'pdo:', $this->userPDO->errorCode(), $this->userPDO->errorInfo(),
- 'stmt:', $stmt->errorCode(), $stmt->errorInfo()
- );
- */
- self::$rcActual['errorCode'] = $stmt->errorCode();
- self::$rcActual['errorInfo'] = $stmt->errorInfo();
- self::$rcActual['errorCode'] = $this->userPDO->errorCode();
- self::$rcActual['errorInfo'] = $this->userPDO->errorInfo();
- $this->assertFalse(self::$rcActual['exception']);
- $this->assertTrue(self::$rcActual['execute']);
- }
- catch (PDOException $e) {
- self::$rcActual['exception'] = true;
- self::$rcActual['exceptionMsg'] = $e->getCode() .' : '. $e->getMessage();
- $this->assertTrue(self::$rcActual['exception']);
- }
- // --------------------------------------------------------
- $this->logging->stopLogging();
- echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
- }
- /**
- * Show the results of the
- */
- public function testShowResults() {
- echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
- echo PHP_EOL, "prepare : Expected: ", var_export(self::$rcExpected['prepare'], true),
- ", Actual: ", var_export(self::$rcActual['prepare'], true);
- echo PHP_EOL, "execute : Expected: ", var_export(self::$rcExpected['execute'], true),
- ", Actual: ", var_export(self::$rcActual['execute'], true);
- echo PHP_EOL, "exception : Expected: ", var_export(self::$rcExpected['exception'], true),
- ", Actual: ", var_export(self::$rcActual['exception'], true);
- echo PHP_EOL, "exceptionMsg : Expected: ", var_export(self::$rcExpected['exceptionMsg'], true),
- ", Actual: ", var_export(self::$rcActual['exceptionMsg'], true);
- echo PHP_EOL, "errorCode : ", var_export(self::$rcActual['errorCode'], true),
- ", ErrorMsg: ", var_export(self::$rcActual['errorInfo'], true);
- echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
- $this->assertEquals(self::$rcExpected['prepare'], self::$rcActual['prepare']);
- $this->assertEquals(self::$rcExpected['execute'], self::$rcActual['execute']);
- $this->assertEquals(self::$rcExpected['exception'], self::$rcActual['exception']);
- }
- /* -----------------------------------------------------------------------
- * Standard setup stuff after this...
- * No need to change it once you have set it up to work.
- */
- /**
- * @var MysqlGeneralLogging
- */
- protected $logging;
- /**
- * SQL Testing Connection
- *
- * @var PDO
- */
- protected $userPDO = null;
- public function parseMethod($method, $which = 'method')
- {
- $fields = explode('::', $method);
- if ($which == 'method') {
- return ($fields[1]);
- }
- return $fields[0];
- }
- protected static $runAtStart = true;
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp() {
- $this->logging = MysqlGeneralLogging::newInstanceFromParams(
- 'localhost', 'root', 'root', 'Q32451215',
- __DIR__);
- // MySql User account
- $host = 'localhost';
- $username = 'test';
- $password = 'test';
- $dbname = 'test';
- $dsn = "mysql:host={$host};" . (!empty($dbname) ? "dbname={$dbname};" : '');
- $pdoConnection = new \PDO($dsn, $username, $password);
- $pdoConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $pdoConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
- $this->userPDO = $pdoConnection;
- // echo PHP_EOL;
- $this->runAtStart();
- // echo PHP_EOL, ' <<<<<<<<<< test starting >>>>>>>>>', PHP_EOL;
- }
- public function initAccountTable()
- {
- $sql = "CREATE TABLE IF NOT EXISTS `account` (
- `guid` binary(16) NOT NULL,
- `displayname` varchar(32) NOT NULL,
- `password_hash` binary(60) NOT NULL,
- `password_salt` binary(30) NOT NULL,
- `email` varchar(256) NOT NULL,
- `superuser` tinyint(1) NOT NULL DEFAULT '0',
- `account_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`guid`))
- ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- ";
- $this->userPDO->exec($sql);
- $this->userPDO->exec("truncate table `test`.`account`;");
- $dataArray = array(
- "INSERT INTO `account` (`guid`, `displayname`, `password_hash`, `password_salt`, `email`) VALUES (UNHEX(REPLACE(UUID(), '-', '')), 'Example User 1', '$2y$17$12345678901234567890121234567890123456789012345678901', '$2y$17$1234567890123456789012$', 'example1@example.com');",
- "INSERT INTO `account` (`guid`, `displayname`, `password_hash`, `password_salt`, `email`) VALUES (UNHEX(REPLACE(UUID(), '-', '')), 'Example User 2', '$2y$17$12345678901234567890121234567890123456789012345678901', '$2y$17$1234567890123456789012$', 'example2@example.com');",
- "INSERT INTO `account` (`guid`, `displayname`, `password_hash`, `password_salt`, `email`) VALUES (UNHEX(REPLACE(UUID(), '-', '')), 'Example User 3', '$2y$17$12345678901234567890121234567890123456789012345678901', '$2y$17$1234567890123456789012$', 'example3@example.com');",
- "INSERT INTO `account` (`guid`, `displayname`, `password_hash`, `password_salt`, `email`) VALUES (UNHEX(REPLACE(UUID(), '-', '')), 'Example User 4', '$2y$17$12345678901234567890121234567890123456789012345678901', '$2y$17$1234567890123456789012$', 'example4@example.com');",
- "INSERT INTO `account` (`guid`, `displayname`, `password_hash`, `password_salt`, `email`) VALUES (UNHEX(REPLACE(UUID(), '-', '')), 'Example User 5', '$2y$17$12345678901234567890121234567890123456789012345678901', '$2y$17$1234567890123456789012$', 'example5@example.com');",
- );
- foreach ($dataArray as $insertSql) {
- $this->userPDO->exec($insertSql);
- }
- // confirm all ok...
- $resultSet = $this->userPDO->query("SELECT count(*) as `rowCount` FROM `account`");
- $result = current($resultSet->fetchAll(PDO::FETCH_ASSOC));
- $this->assertEquals(count($dataArray), $result['rowCount']);
- }
- protected function runAtStart()
- {
- if (!self::$runAtStart) { return; }
- self::$runAtStart = false;
- $this->initAccountTable();
- // Clear all the current trace logs and table
- $this->logging->stopLogging();
- $this->logging->clearLogs();
- $testLogFile = $this->logging->newTraceFilename(
- $this->parseMethod(__METHOD__.__LINE__));
- $this->logging->setLogFile($testLogFile);
- $files = $this->logging->deleteAllTraceFiles();
- // var_dump(__METHOD__.__LINE__, $files);
- }
- /**
- * Tears down the fixture, for example, closes a network connection.
- * This method is called after a test is executed.
- */
- protected function tearDown() {
- $this->logging->stopLogging();
- $this->logging = null;
- $this->userPDO = null;
- // echo ' <<<<<<<<<< test ending >>>>>>>>>', PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement