Advertisement
rfv123

Q32451215Test_02A_EmulatesOff__WithSuperUserAsBooleanParamTy

Sep 15th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.59 KB | None | 0 0
  1. <?php // Q32451215Test_02A_EmulatesOff__WithSuperUserAsBooleanParamType
  2.  
  3. include 'P:/developer/xampp/htdocs/testmysql/Q32451215/IMysqlGeneralLogging.php';
  4. include 'P:/developer/xampp/htdocs/testmysql/Q32451215/MysqlGeneralLogging.php';
  5.  
  6. class Q23451215Test extends PHPUnit_Framework_TestCase {
  7.  
  8.     /**
  9.      * Test Data
  10.      */
  11.     protected $guid             = '420D4B65565311E5958000FFD7CBE75F';
  12.     protected $displayname     = 'Example User 42';
  13.     protected $hash             = '$2y$17$12345678901234567890121234567890123456789012345678942';
  14.     protected $salt             = '$2y$17$1234567890123456789042$';
  15.     protected $email            = 'example42@example.com';
  16.     protected $superuser       =  true;
  17.  
  18.     /**
  19.      * I will also record all the return codes from the PDO functions
  20.      * so that I can see what was returned with what happened vs what we expect.
  21.      *
  22.      * This will be useful with the later test.
  23.      * Must be static so they are not reset with each test
  24.      */
  25.     protected static $rcActual = array(
  26.         'prepare'       => false,
  27.         'execute'       => false,
  28.         'exception'     => false,
  29.         'exceptionMsg'  => '',
  30.         'errorCode'       => '',  // return code from the statement or connection
  31.         'errorInfo'       => ''   // return message from the statement or connection
  32.     );
  33.  
  34.     /**
  35.      * Expected Results
  36.      * Must be static so they are not reset with each test
  37.      */
  38.     protected static $rcExpected = array(
  39.         'prepare'       => true,
  40.         'execute'       => true,
  41.         'exception'     => false,
  42.         'exceptionMsg'  => '',
  43.     );
  44.  
  45.     /**
  46.      * Testing settings:
  47.      *
  48.      *   PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION);
  49.      *   PDO::ATTR_EMULATE_PREPARES  => FALSE;
  50.      */
  51.  
  52.     /**
  53.      * Expected Results:
  54.      *
  55.      * All Ok -
  56.      *
  57.      * 1) An inserted row of the supplied test data
  58.      * 2) An 'INSERT' statement in the trace log
  59.      */
  60.  
  61.     public function testEmululatesPreparesOff02A_WithSuperUserAsBooleanParamType() {
  62.         echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
  63.  
  64.         $this->userPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  65.         $this->userPDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  66.  
  67.         $testFilename = $this->logging->newTraceFilename($this->parseMethod(__METHOD__));
  68.         $this->logging->setLogFile($testFilename);
  69.         $this->logging->clearFileLog();
  70.         $this->logging->startLoggingToFile(); // start logging...
  71.         // --------------------------------------------------------
  72.  
  73.         var_dump(__METHOD__.__LINE__ .' PDO Param constants:',
  74.                   PDO::PARAM_BOOL, PDO::PARAM_INT, false, bin2hex(false));
  75.         /*
  76.          * The actual code to be tested is here...
  77.          */
  78.  
  79.         try
  80.         {
  81.             self::$rcActual['exception'] = false;
  82.  
  83.             $stmt = $this->userPDO->prepare(
  84.                        "INSERT INTO `account`
  85.                                (`guid`,       `displayname`, `password_hash`, `password_salt`, `email`, `superuser`)
  86.                        VALUES
  87.                                (UNHEX(:guid), :displayname,  :passwordhash,   :passwordsalt,   :email,  :superuser);");
  88.  
  89.             self::$rcActual['prepare'] = $stmt !== false;
  90.  
  91.             $stmt->bindValue(':guid',         $this->guid,         PDO::PARAM_STR);
  92.             $stmt->bindValue(':displayname',  $this->displayname, PDO::PARAM_STR);
  93.             $stmt->bindValue(':passwordhash', $this->hash,         PDO::PARAM_STR);
  94.             $stmt->bindValue(':passwordsalt', $this->salt,         PDO::PARAM_STR);
  95.             $stmt->bindValue(':email',        $this->email,        PDO::PARAM_STR);
  96.             $stmt->bindValue(':superuser',    $this->superuser,   PDO::PARAM_BOOL);
  97.  
  98.             self::$rcActual['execute'] = $stmt->execute();
  99.  
  100.             /*
  101.               var_dump(__METHOD__.__LINE__ .'error codes',
  102.                  'pdo:',  $this->userPDO->errorCode(), $this->userPDO->errorInfo(),
  103.                  'stmt:', $stmt->errorCode(), $stmt->errorInfo()
  104.                  );
  105.              */
  106.  
  107.             self::$rcActual['errorCode'] = $stmt->errorCode();
  108.             self::$rcActual['errorInfo'] = $stmt->errorInfo();
  109.  
  110.             self::$rcActual['errorCode'] = $this->userPDO->errorCode();
  111.             self::$rcActual['errorInfo'] = $this->userPDO->errorInfo();
  112.  
  113.             $this->assertFalse(self::$rcActual['exception']);
  114.             $this->assertTrue(self::$rcActual['execute']);
  115.         }
  116.         catch (PDOException $e) {
  117.             self::$rcActual['exception'] = true;
  118.             self::$rcActual['exceptionMsg'] = $e->getCode() .' : '. $e->getMessage();
  119.  
  120.             $this->assertTrue(self::$rcActual['exception']);
  121.         }
  122.  
  123.         // --------------------------------------------------------
  124.         $this->logging->stopLogging();
  125.         echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
  126.     }
  127.  
  128.     /**
  129.      * Show the results of the
  130.      */
  131.     public function testShowResults() {
  132.         echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
  133.             echo PHP_EOL, "prepare   : Expected:  ", var_export(self::$rcExpected['prepare'], true),
  134.                                    ", Actual: ",   var_export(self::$rcActual['prepare'], true);
  135.  
  136.  
  137.             echo PHP_EOL, "execute   : Expected:  ", var_export(self::$rcExpected['execute'], true),
  138.                                    ", Actual: ",     var_export(self::$rcActual['execute'], true);
  139.  
  140.             echo PHP_EOL, "exception : Expected:  ", var_export(self::$rcExpected['exception'], true),
  141.                                      ", Actual: ",    var_export(self::$rcActual['exception'], true);
  142.  
  143.             echo PHP_EOL, "exceptionMsg : Expected:  ", var_export(self::$rcExpected['exceptionMsg'], true),
  144.                                        ", Actual: ",    var_export(self::$rcActual['exceptionMsg'], true);
  145.  
  146.             echo PHP_EOL, "errorCode : ", var_export(self::$rcActual['errorCode'], true),
  147.                                        ", ErrorMsg: ",    var_export(self::$rcActual['errorInfo'], true);
  148.  
  149.         echo PHP_EOL, "----------- ". __METHOD__.__LINE__ ." -----------", PHP_EOL;
  150.  
  151.         $this->assertEquals(self::$rcExpected['prepare'],   self::$rcActual['prepare']);
  152.         $this->assertEquals(self::$rcExpected['execute'],   self::$rcActual['execute']);
  153.         $this->assertEquals(self::$rcExpected['exception'], self::$rcActual['exception']);
  154.     }
  155.  
  156.     /* -----------------------------------------------------------------------
  157.      * Standard setup stuff after this...
  158.      * No need to change it once you have set it up to work.
  159.      */
  160.  
  161.     /**
  162.      * @var MysqlGeneralLogging
  163.      */
  164.     protected $logging;
  165.  
  166.     /**
  167.      * SQL Testing Connection
  168.      *
  169.      * @var PDO
  170.      */
  171.     protected $userPDO = null;
  172.  
  173.     public function parseMethod($method, $which = 'method')
  174.     {
  175.         $fields = explode('::', $method);
  176.         if ($which == 'method') {
  177.            return ($fields[1]);
  178.         }
  179.         return $fields[0];
  180.     }
  181.  
  182.  
  183.     protected static $runAtStart = true;
  184.  
  185.     /**
  186.      * Sets up the fixture, for example, opens a network connection.
  187.      * This method is called before a test is executed.
  188.      */
  189.     protected function setUp() {
  190.         $this->logging = MysqlGeneralLogging::newInstanceFromParams(
  191.                                 'localhost', 'root', 'root', 'Q32451215',
  192.                                  __DIR__);
  193.  
  194.         // MySql User account
  195.         $host      = 'localhost';
  196.         $username  = 'test';
  197.         $password  = 'test';
  198.         $dbname    = 'test';
  199.  
  200.         $dsn = "mysql:host={$host};" . (!empty($dbname) ? "dbname={$dbname};" : '');
  201.         $pdoConnection = new \PDO($dsn, $username, $password);
  202.         $pdoConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  203.         $pdoConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
  204.         $this->userPDO = $pdoConnection;
  205.  
  206. //        echo PHP_EOL;
  207.         $this->runAtStart();
  208.         // echo PHP_EOL, ' <<<<<<<<<< test starting >>>>>>>>>', PHP_EOL;
  209.     }
  210.  
  211.    public function initAccountTable()
  212.    {
  213.        $sql = "CREATE TABLE IF NOT EXISTS `account` (
  214.                    `guid` binary(16) NOT NULL,
  215.                    `displayname` varchar(32) NOT NULL,
  216.                    `password_hash` binary(60) NOT NULL,
  217.                    `password_salt` binary(30) NOT NULL,
  218.                    `email` varchar(256) NOT NULL,
  219.                    `superuser` tinyint(1) NOT NULL DEFAULT '0',
  220.                    `account_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  221.                PRIMARY KEY (`guid`))
  222.                ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  223.              ";
  224.  
  225.        $this->userPDO->exec($sql);
  226.        $this->userPDO->exec("truncate table `test`.`account`;");
  227.  
  228.        $dataArray = array(
  229.         "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');",
  230.         "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');",
  231.         "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');",
  232.         "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');",
  233.         "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');",
  234.        );
  235.  
  236.        foreach ($dataArray as $insertSql) {
  237.             $this->userPDO->exec($insertSql);
  238.        }
  239.  
  240.        // confirm all ok...
  241.        $resultSet = $this->userPDO->query("SELECT count(*) as `rowCount` FROM `account`");
  242.        $result = current($resultSet->fetchAll(PDO::FETCH_ASSOC));
  243.        $this->assertEquals(count($dataArray), $result['rowCount']);
  244.    }
  245.  
  246.     protected function runAtStart()
  247.     {
  248.         if (!self::$runAtStart) { return; }
  249.         self::$runAtStart = false;
  250.  
  251.  
  252.         $this->initAccountTable();
  253.  
  254.         // Clear all the current trace logs and table
  255.         $this->logging->stopLogging();
  256.         $this->logging->clearLogs();
  257.         $testLogFile = $this->logging->newTraceFilename(
  258.                 $this->parseMethod(__METHOD__.__LINE__));
  259.         $this->logging->setLogFile($testLogFile);
  260.         $files = $this->logging->deleteAllTraceFiles();
  261.         // var_dump(__METHOD__.__LINE__, $files);
  262.     }
  263.  
  264.     /**
  265.      * Tears down the fixture, for example, closes a network connection.
  266.      * This method is called after a test is executed.
  267.      */
  268.     protected function tearDown() {
  269.         $this->logging->stopLogging();
  270.         $this->logging = null;
  271.         $this->userPDO = null;
  272.         // echo ' <<<<<<<<<< test ending >>>>>>>>>', PHP_EOL;
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement