dimanche 23 août 2015

PDO::FETCH_CLASS return null when used over pdo mock

i am testing this class, or more accurately, the getAdsGroup() method :

    class AdRule {
      public $id;
      public $site;
      public $placement;

      protected $dbh = null;

  function getDbh() {
    if ($this->dbh === null){
      $this->dbh = Slim::getInstance()->db->getConnection();
    }
    return $this->dbh;
  }

  function setDbh($dbh) {
    $this->dbh = $dbh;
  }

      /* more methods...*/

      function getAdsGroup() {
        $query = "SELECT ads.*, providers.name AS provider_name"
               ." FROM ads"
               ." INNER JOIN providers ON ads.provider_id = providers.id"
               ." WHERE group_id = {$this->id}";
        $DBH = $this->getDbh();
        $STH = $DBH->query($query);
        $ads = $STH->fetchAll(PDO::FETCH_CLASS, 'AdUnit');
        $retun $ads;
      }

now i am using a PDO mock , i created with pseudoto initialize the Dbh and my test lock something like this (after few modifications to make point clearer, though the test make no sense as is):

public function testGetAdsGroup()
{
  $id = 111;
  $ad_1 = array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123');

  $pdo = new Pseudo\Pdo();
  $expected = new Pseudo\Result();
  $expected->addRow($ad_1);
  $pdo->mock('SELECT ads.*, providers.name AS provider_name FROM ads INNER JOIN providers ON ads.provider_id = providers.id WHERE group_id = 111', $expected);

  $fixture = new AdRule();
  $fixture->setDbh($pdo);
  $fixture->id = $id;

  $STH = $pdo->query('SELECT ads.*, providers.name AS provider_name FROM ads INNER JOIN providers ON ads.provider_id = providers.id WHERE group_id = 111');
  $ads = $STH->fetchAll(PDO::FETCH_CLASS, 'AdUnit');

  var_dump($ads);
}

the problem is now var_dump($ads) dumps NULL instead of $ad_1 and i can't understand why. when i change $ads = $STH->fetchAll(PDO::FETCH_CLASS, 'AdUnit'); to $ads = $STH->fetchAll(PDO::FETCH_ASSOC); i get the expected details of $ad_1. any idea how can i make it work with the FETCH_CLASS?

this is the pdo object:

 ["mockedQueries":"Pseudo\Pdo":private]=>
  object(Pseudo\ResultCollection)#397 (1) {
    ["queries":"Pseudo\ResultCollection":private]=>
    array(1) {
      ["b8e807680da5a916cba3e3bc5f231efa1ce6c93d"]=>
      object(Pseudo\Result)#396 (7) {
        ["rows":"Pseudo\Result":private]=>
        array(1) {
          [0]=>
          array(5) {
            ["id"]=>
            string(1) "1"
            ["name"]=>
            string(4) "Ad_1"
            ["description"]=>
            string(8) "great ad"
            ["code"]=>
            string(5) "alpha"
            ["widget_id"]=>
            string(3) "123"
          }
        }
        ["isParameterized":"Pseudo\Result":private]=>
        bool(false)
        ["errorCode":"Pseudo\Result":private]=>
        NULL
        ["errorInfo":"Pseudo\Result":private]=>
        NULL
        ["affectedRowCount":"Pseudo\Result":private]=>
        int(0)
        ["insertId":"Pseudo\Result":private]=>
        int(0)
        ["rowOffset":"Pseudo\Result":private]=>
        int(0)
      }
    }
  }
  ["inTransaction":"Pseudo\Pdo":private]=>
  bool(false)
  ["queryLog":"Pseudo\Pdo":private]=>
  object(Pseudo\QueryLog)#398 (1) {
    ["queries":"Pseudo\QueryLog":private]=>
    array(1) {
      [0]=>
      object(Pseudo\ParsedQuery)#394 (3) {
        ["parsedQuery":"Pseudo\ParsedQuery":private]=>
        array(3) {
          ["SELECT"]=>
          array(2) {
            [0]=>
            array(4) {
              ["expr_type"]=>
              string(6) "colref"
              ["alias"]=>
              bool(false)
              ["base_expr"]=>
              string(5) "ads.*"
              ["sub_tree"]=>
              bool(false)
            }
            [1]=>
            array(4) {
              ["expr_type"]=>
              string(6) "colref"
              ["alias"]=>
              array(3) {
                ["as"]=>
                bool(true)
                ["name"]=>
                string(13) "provider_name"
                ["base_expr"]=>
                string(16) "AS provider_name"
              }
              ["base_expr"]=>
              string(14) "providers.name"
              ["sub_tree"]=>
              bool(false)
            }
          }
          ["FROM"]=>
          array(2) {
            [0]=>
            array(8) {
              ["expr_type"]=>
              string(5) "table"
              ["table"]=>
              string(3) "ads"
              ["alias"]=>
              bool(false)
              ["join_type"]=>
              string(4) "JOIN"
              ["ref_type"]=>
              bool(false)
              ["ref_clause"]=>
              bool(false)
              ["base_expr"]=>
              string(3) "ads"
              ["sub_tree"]=>
              bool(false)
            }
            [1]=>
            array(8) {
              ["expr_type"]=>
              string(5) "table"
              ["table"]=>
              string(9) "providers"
              ["alias"]=>
              bool(false)
              ["join_type"]=>
              string(4) "JOIN"
              ["ref_type"]=>
              string(2) "ON"
              ["ref_clause"]=>
              array(3) {
                [0]=>
                array(3) {
                  ["expr_type"]=>
                  string(6) "colref"
                  ["base_expr"]=>
                  string(15) "ads.provider_id"
                  ["sub_tree"]=>
                  bool(false)
                }
                [1]=>
                array(3) {
                  ["expr_type"]=>
                  string(8) "operator"
                  ["base_expr"]=>
                  string(1) "="
                  ["sub_tree"]=>
                  bool(false)
                }
                [2]=>
                array(3) {
                  ["expr_type"]=>
                  string(6) "colref"
                  ["base_expr"]=>
                  string(12) "providers.id"
                  ["sub_tree"]=>
                  bool(false)
                }
              }
              ["base_expr"]=>
              string(43) "providers ON ads.provider_id = providers.id"
              ["sub_tree"]=>
              bool(false)
            }
          }
          ["WHERE"]=>
          array(3) {
            [0]=>
            array(3) {
              ["expr_type"]=>
              string(6) "colref"
              ["base_expr"]=>
              string(8) "group_id"
              ["sub_tree"]=>
              bool(false)
            }
            [1]=>
            array(3) {
              ["expr_type"]=>
              string(8) "operator"
              ["base_expr"]=>
              string(1) "="
              ["sub_tree"]=>
              bool(false)
            }
            [2]=>
            array(3) {
              ["expr_type"]=>
              string(5) "const"
              ["base_expr"]=>
              string(3) "111"
              ["sub_tree"]=>
              bool(false)
            }
          }
        }
        ["rawQuery":"Pseudo\ParsedQuery":private]=>
        string(130) "SELECT ads.*, providers.name AS provider_name FROM ads INNER JOIN providers ON ads.provider_id = providers.id WHERE group_id = 111"
        ["hash":"Pseudo\ParsedQuery":private]=>
        string(40) "b8e807680da5a916cba3e3bc5f231efa1ce6c93d"
      }
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire