Skip to content
Snippets Groups Projects
Commit aaf753bc authored by Bernhard Posselt's avatar Bernhard Posselt Committed by Thomas Müller
Browse files

fix mappertestutility

parent 7b2fdbfb
No related branches found
No related tags found
No related merge requests found
...@@ -57,110 +57,114 @@ abstract class MapperTestUtility extends \Test\TestCase { ...@@ -57,110 +57,114 @@ abstract class MapperTestUtility extends \Test\TestCase {
} }
/**
* Create mocks and set expected results for database queries /**
* @param string $sql the sql query that you expect to receive * Create mocks and set expected results for database queries
* @param array $arguments the expected arguments for the prepare query * @param string $sql the sql query that you expect to receive
* method * @param array $arguments the expected arguments for the prepare query
* @param array $returnRows the rows that should be returned for the result * method
* of the database query. If not provided, it wont be assumed that fetch * @param array $returnRows the rows that should be returned for the result
* will be called on the result * of the database query. If not provided, it wont be assumed that fetch
*/ * will be called on the result
protected function setMapperResult($sql, $arguments=array(), $returnRows=array(), */
$limit=null, $offset=null, $expectClose=false){ protected function setMapperResult($sql, $arguments=array(), $returnRows=array(),
if($limit === null && $offset === null) { $limit=null, $offset=null, $expectClose=false){
$this->db->expects($this->at($this->prepareAt)) if($limit === null && $offset === null) {
->method('prepare') $this->db->expects($this->at($this->prepareAt))
->with($this->equalTo($sql)) ->method('prepare')
->will(($this->returnValue($this->query))); ->with($this->equalTo($sql))
} elseif($limit !== null && $offset === null) { ->will(($this->returnValue($this->query)));
$this->db->expects($this->at($this->prepareAt)) } elseif($limit !== null && $offset === null) {
->method('prepare') $this->db->expects($this->at($this->prepareAt))
->with($this->equalTo($sql), $this->equalTo($limit)) ->method('prepare')
->will(($this->returnValue($this->query))); ->with($this->equalTo($sql), $this->equalTo($limit))
} elseif($limit === null && $offset !== null) { ->will(($this->returnValue($this->query)));
$this->db->expects($this->at($this->prepareAt)) } elseif($limit === null && $offset !== null) {
->method('prepare') $this->db->expects($this->at($this->prepareAt))
->with($this->equalTo($sql), ->method('prepare')
$this->equalTo(null), ->with($this->equalTo($sql),
$this->equalTo($offset)) $this->equalTo(null),
->will(($this->returnValue($this->query))); $this->equalTo($offset))
} else { ->will(($this->returnValue($this->query)));
$this->db->expects($this->at($this->prepareAt)) } else {
->method('prepare') $this->db->expects($this->at($this->prepareAt))
->with($this->equalTo($sql), ->method('prepare')
$this->equalTo($limit), ->with($this->equalTo($sql),
$this->equalTo($offset)) $this->equalTo($limit),
->will(($this->returnValue($this->query))); $this->equalTo($offset))
} ->will(($this->returnValue($this->query)));
}
$this->iterators[] = new ArgumentIterator($returnRows);
$this->iterators[] = new ArgumentIterator($returnRows);
$iterators = $this->iterators;
$fetchAt = $this->fetchAt; $iterators = $this->iterators;
$fetchAt = $this->fetchAt;
$this->query->expects($this->any())
->method('fetch') $this->query->expects($this->any())
->will($this->returnCallback( ->method('fetch')
function() use ($iterators, $fetchAt){ ->will($this->returnCallback(
$iterator = $iterators[$fetchAt]; function() use ($iterators, $fetchAt){
$result = $iterator->next(); $iterator = $iterators[$fetchAt];
$result = $iterator->next();
if($result === false) {
$fetchAt++; if($result === false) {
} $fetchAt++;
}
$this->queryAt++;
$this->queryAt++;
return $result;
} return $result;
)); }
));
$index = 1;
foreach($arguments as $argument) { $index = 1;
switch (gettype($argument)) { foreach($arguments as $argument) {
case 'integer': switch (gettype($argument)) {
$pdoConstant = \PDO::PARAM_INT; case 'integer':
break; $pdoConstant = \PDO::PARAM_INT;
break;
case 'NULL':
$pdoConstant = \PDO::PARAM_NULL; case 'NULL':
break; $pdoConstant = \PDO::PARAM_NULL;
break;
case 'boolean':
$pdoConstant = \PDO::PARAM_BOOL; case 'boolean':
break; $pdoConstant = \PDO::PARAM_BOOL;
break;
default:
$pdoConstant = \PDO::PARAM_STR; default:
break; $pdoConstant = \PDO::PARAM_STR;
} break;
$this->query->expects($this->at($this->queryAt)) }
->method('bindValue') $this->query->expects($this->at($this->queryAt))
->with($this->equalTo($index), ->method('bindValue')
$this->equalTo($argument), ->with($this->equalTo($index),
$this->equalTo($pdoConstant)); $this->equalTo($argument),
$index++; $this->equalTo($pdoConstant));
$this->queryAt++; $index++;
} $this->queryAt++;
}
$this->query->expects($this->at($this->queryAt))
->method('execute'); $this->query->expects($this->at($this->queryAt))
$this->queryAt++; ->method('execute')
->will($this->returnCallback(function($sql, $p=null, $o=null, $s=null) {
}));
if ($expectClose) { $this->queryAt++;
$closing = $this->once();
} else {
$closing = $this->any();
} if ($expectClose) {
$this->query->expects($closing)->method('closeCursor'); $closing = $this->at($this->queryAt);
$this->queryAt++; } else {
$closing = $this->any();
$this->prepareAt++; }
$this->fetchAt++; $this->query->expects($closing)->method('closeCursor');
} $this->queryAt++;
$this->prepareAt++;
$this->fetchAt++;
}
} }
...@@ -168,19 +172,19 @@ abstract class MapperTestUtility extends \Test\TestCase { ...@@ -168,19 +172,19 @@ abstract class MapperTestUtility extends \Test\TestCase {
class ArgumentIterator { class ArgumentIterator {
private $arguments; private $arguments;
public function __construct($arguments){ public function __construct($arguments){
$this->arguments = $arguments; $this->arguments = $arguments;
} }
public function next(){ public function next(){
$result = array_shift($this->arguments); $result = array_shift($this->arguments);
if($result === null){ if($result === null){
return false; return false;
} else { } else {
return $result; return $result;
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment