PHP Classes
elePHPant
Icontem

File: tests/SafeQueryTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  EasyDB  >  tests/SafeQueryTest.php  >  Download  
File: tests/SafeQueryTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: EasyDB
Simple Database Abstraction Layer around PDO
Author: By
Last change: re paragonie/easydb#85, adding additional argument to give a slightly enhanced error message on the assumption that a > 1d array being passed to `EasyDB::safeQuery()` was done in error via a method like `EasyDB::run($q, $args)` instead of `EasyDB::run($q, ...$args)`
Date: 6 months ago
Size: 1,555 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

namespace
ParagonIE\EasyDB\Tests;

use
ParagonIE\EasyDB\EasyDB;
use
ParagonIE\EasyDB\Factory;

class
SafeQueryTest extends RunTest
{
    protected function
getResultForMethod(EasyDB $db, $statement, $offset, $params)
    {
        return
$db->safeQuery($statement, $params);
    }

   
/**
     * @dataProvider goodFactoryCreateArgumentProvider
     * @param $dsn
     * @param null $username
     * @param null $password
     * @param array $options
     */
   
public function testSafeQueryCalledWithVariadicParamsThrowsException(
       
$expectedDriver,
       
$dsn,
       
$username = null,
       
$password = null,
       
$options = []
    ) {
       
$db = Factory::create($dsn, $username, $password, $options);
       
$args = [1, 2, 3, 4];
       
$results = $db->run('SELECT ? AS foo, ? AS bar UNION SELECT ? AS foo, ? AS bar', ...$args);
       
$this->assertInternalType('array', $results);

       
$expectedResult = [['foo' => 1, 'bar' => 2], ['foo' => 3, 'bar' => 4]];

        foreach (
$results as $i => $result) {
           
$this->assertInternalType('array', $result);
           
$this->assertEquals(array_diff_assoc($result, $expectedResult[$i]), []);
        }

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage(
           
'Only one-dimensional arrays are allowed, please use ' .
           
EasyDB::class .
           
'::safeQuery()'
       
);
       
$db->run('SELECT ? AS foo, ? AS bar UNION SELECT ? AS foo, ? AS bar', $args);
    }
}