aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-03-20 08:53:14 +0100
committerMorris Jobke <hey@morrisjobke.de>2015-03-20 08:53:14 +0100
commit1de6fa19de7849b4bc2ca5758e45e932aed3865d (patch)
tree30bb84841106555fa52404f45a8b79a8ee93ef2c /tests/lib
parente8680e6637c7bb4aa5a331d81ba5ed76e0dfd1ae (diff)
parentdf24a014b8bc205e6bc047ead2af393b3e169542 (diff)
downloadnextcloud-server-1de6fa19de7849b4bc2ca5758e45e932aed3865d.tar.gz
nextcloud-server-1de6fa19de7849b4bc2ca5758e45e932aed3865d.zip
Merge pull request #15035 from owncloud/assocmapper
If the execute method on the mapper receives an assoc array, it binds by...
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/appframework/db/mappertest.php10
-rw-r--r--tests/lib/appframework/db/mappertestutility.php69
2 files changed, 54 insertions, 25 deletions
diff --git a/tests/lib/appframework/db/mappertest.php b/tests/lib/appframework/db/mappertest.php
index 8e585c479bb..c8b999ad62c 100644
--- a/tests/lib/appframework/db/mappertest.php
+++ b/tests/lib/appframework/db/mappertest.php
@@ -47,6 +47,7 @@ class ExampleMapper extends Mapper {
public function findOneEntity($table, $id){ return $this->findEntity($table, $id); }
public function findAllEntities($table){ return $this->findEntities($table); }
public function mapRow($row){ return $this->mapRowToEntity($row); }
+ public function execSql($sql, $params){ return $this->execute($sql, $params); }
}
@@ -187,6 +188,15 @@ class MapperTest extends MapperTestUtility {
}
+ public function testAssocParameters() {
+ $sql = 'test';
+ $params = [':test' => 1, ':a' => 2];
+
+ $this->setMapperResult($sql, $params);
+ $this->mapper->execSql($sql, $params);
+ }
+
+
public function testUpdate(){
$sql = 'UPDATE `*PREFIX*table` ' .
'SET ' .
diff --git a/tests/lib/appframework/db/mappertestutility.php b/tests/lib/appframework/db/mappertestutility.php
index c87ad528c03..818e4a59b21 100644
--- a/tests/lib/appframework/db/mappertestutility.php
+++ b/tests/lib/appframework/db/mappertestutility.php
@@ -56,7 +56,30 @@ abstract class MapperTestUtility extends \Test\TestCase {
$this->fetchAt = 0;
}
+ /**
+ * Checks if an array is associative
+ * @param array $array
+ * @return bool true if associative
+ */
+ private function isAssocArray(array $array) {
+ return array_values($array) !== $array;
+ }
+ /**
+ * Returns the correct PDO constant based on the value type
+ * @param $value
+ * @return PDO constant
+ */
+ private function getPDOType($value) {
+ switch (gettype($value)) {
+ case 'integer':
+ return \PDO::PARAM_INT;
+ case 'boolean':
+ return \PDO::PARAM_BOOL;
+ default:
+ return \PDO::PARAM_STR;
+ }
+ }
/**
* Create mocks and set expected results for database queries
@@ -117,32 +140,28 @@ abstract class MapperTestUtility extends \Test\TestCase {
}
));
- $index = 1;
- foreach($arguments as $argument) {
- switch (gettype($argument)) {
- case 'integer':
- $pdoConstant = \PDO::PARAM_INT;
- break;
-
- case 'NULL':
- $pdoConstant = \PDO::PARAM_NULL;
- break;
-
- case 'boolean':
- $pdoConstant = \PDO::PARAM_BOOL;
- break;
-
- default:
- $pdoConstant = \PDO::PARAM_STR;
- break;
+ if ($this->isAssocArray($arguments)) {
+ foreach($arguments as $key => $argument) {
+ $pdoConstant = $this->getPDOType($argument);
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($key),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $this->queryAt++;
+ }
+ } else {
+ $index = 1;
+ foreach($arguments as $argument) {
+ $pdoConstant = $this->getPDOType($argument);
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($index),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $index++;
+ $this->queryAt++;
}
- $this->query->expects($this->at($this->queryAt))
- ->method('bindValue')
- ->with($this->equalTo($index),
- $this->equalTo($argument),
- $this->equalTo($pdoConstant));
- $index++;
- $this->queryAt++;
}
$this->query->expects($this->at($this->queryAt))