PHP Classes

File: tests/GetAttributeTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   EasyDB   tests/GetAttributeTest.php   Download  
File: tests/GetAttributeTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: EasyDB
Simple Database Abstraction Layer around PDO
Author: By
Last change: manually changing to squish "method name is not in camel caps format" style error
single-lining test classes after composer run fix-style
running composer run fix-style
Date: 6 years ago
Size: 3,039 bytes
 

Contents

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

namespace
ParagonIE\EasyDB\Tests;

use
ParagonIE\EasyDB\EasyDB;
use
PDO;
use
PDOException;
use
ReflectionClass;

class
GetAttributeTest extends EasyDBTest
{

   
/**
    * EasyDB data provider
    * Returns an array of callables that return instances of EasyDB
    * @return array
    * @see EasyDBTest::goodFactoryCreateArgument2EasyDBProvider()
    */
   
public function goodFactoryCreateArgument2EasyDBWithPDOAttributeProvider()
    {
       
$ref = new ReflectionClass(PDO::class);
        if (
defined('ARRAY_FILTER_USE_KEY')) {
           
$attrs = array_filter(
               
$ref->getConstants(),
                function (
$attrName) {
                    return (
strpos($attrName, 'ATTR_') === 0);
                },
               
ARRAY_FILTER_USE_KEY
           
);
        } else {
           
$constants = $ref->getConstants();
           
$attrs = array_reduce(
               
array_keys($constants),
                function (array
$was, $attrName) use ($constants) {
                    if (
strpos($attrName, 'ATTR_') === 0) {
                       
$was[$attrName] = $constants[$attrName];
                    }
                    return
$was;
                },
                []
            );
        }
        return
array_reduce(
           
$this->goodFactoryCreateArgument2EasyDBProvider(),
            function (array
$was, array $cbArgs) use ($attrs) {
                foreach (
$attrs as $attrName => $attr) {
                   
$args = [$attr, $attrName];
                    foreach (
array_reverse($cbArgs) as $cbArg) {
                       
array_unshift($args, $cbArg);
                    }
                   
$was[] = $args;
                }
                return
$was;
            },
            []
        );
    }

   
/**
     * @dataProvider goodFactoryCreateArgument2EasyDBWithPDOAttributeProvider
     * @param callable $cb
     * @param $attr
     * @param string $attrName
     */
   
public function testAttribute(callable $cb, $attr, string $attrName)
    {
       
$db = $this->easyDBExpectedFromCallable($cb);
        try {
           
$this->assertSame(
               
$db->getAttribute($attr),
               
$db->getPdo()->getAttribute($attr)
            );
        } catch (
PDOException $e) {
            if (
strpos(
               
$e->getMessage(),
               
': Driver does not support this function: driver does not support that attribute'
           
) !== false
           
) {
               
$this->markTestSkipped(
                   
'Skipping tests for ' .
                   
EasyDB::class .
                   
'::getAttribute(' .
                       
PDO::class .
                       
'::' .
                       
$attrName .
                   
'), as driver "' .
                   
$db->getDriver() .
                   
'" does not support that attribute'
               
);
               
$this->markTestSkipped($e->getMessage());
            } else {
                throw
$e;
            }
        }
    }
}