summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2022-10-09 21:46:22 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2022-10-10 08:18:32 +0200
commit60ee8744859a829f6c3911d0fb44ce0e5dc19237 (patch)
tree7bf91ea033c0d7f591e600ce6528089bc11618c0 /tests
parent00485eff56436dca388127c557712ab0b00666e7 (diff)
downloadnextcloud-server-60ee8744859a829f6c3911d0fb44ce0e5dc19237.tar.gz
nextcloud-server-60ee8744859a829f6c3911d0fb44ce0e5dc19237.zip
Remove long depreated AppFramework/Db/Mapper
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/AppFramework/Db/MapperTest.php300
-rw-r--r--tests/lib/AppFramework/Db/MapperTestUtility.php206
2 files changed, 0 insertions, 506 deletions
diff --git a/tests/lib/AppFramework/Db/MapperTest.php b/tests/lib/AppFramework/Db/MapperTest.php
deleted file mode 100644
index e5a4b63b7a3..00000000000
--- a/tests/lib/AppFramework/Db/MapperTest.php
+++ /dev/null
@@ -1,300 +0,0 @@
-<?php
-
-/**
- * ownCloud - App Framework
- *
- * @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace Test\AppFramework\Db;
-
-use OCP\AppFramework\Db\DoesNotExistException;
-use OCP\AppFramework\Db\Entity;
-use OCP\AppFramework\Db\Mapper;
-use OCP\AppFramework\Db\MultipleObjectsReturnedException;
-use OCP\IDBConnection;
-
-/**
- * @method integer getId()
- * @method void setId(integer $id)
- * @method string getEmail()
- * @method void setEmail(string $email)
- * @method string getPreName()
- * @method void setPreName(string $preName)
- */
-class Example extends Entity {
- protected $preName;
- protected $email;
-};
-
-
-class ExampleMapper extends Mapper {
- public function __construct(IDBConnection $db) {
- parent::__construct($db, 'table');
- }
- public function find($table, $id) {
- return $this->findOneQuery($table, $id);
- }
- 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);
- }
-}
-
-
-class MapperTest extends MapperTestUtility {
-
- /**
- * @var Mapper
- */
- private $mapper;
-
- protected function setUp(): void {
- parent::setUp();
- $this->mapper = new ExampleMapper($this->db);
- }
-
-
- public function testMapperShouldSetTableName() {
- $this->assertEquals('*PREFIX*table', $this->mapper->getTableName());
- }
-
-
- public function testFindQuery() {
- $sql = 'hi';
- $params = ['jo'];
- $rows = [
- ['hi']
- ];
- $this->setMapperResult($sql, $params, $rows);
- $this->mapper->find($sql, $params);
- }
-
- public function testFindEntity() {
- $sql = 'hi';
- $params = ['jo'];
- $rows = [
- ['pre_name' => 'hi']
- ];
- $this->setMapperResult($sql, $params, $rows, null, null, true);
- $this->mapper->findOneEntity($sql, $params);
- }
-
- public function testFindNotFound() {
- $sql = 'hi';
- $params = ['jo'];
- $rows = [];
- $this->setMapperResult($sql, $params, $rows);
- $this->expectException(DoesNotExistException::class);
- $this->mapper->find($sql, $params);
- }
-
- public function testFindEntityNotFound() {
- $sql = 'hi';
- $params = ['jo'];
- $rows = [];
- $this->setMapperResult($sql, $params, $rows, null, null, true);
- $this->expectException(DoesNotExistException::class);
- $this->mapper->findOneEntity($sql, $params);
- }
-
- public function testFindMultiple() {
- $sql = 'hi';
- $params = ['jo'];
- $rows = [
- ['jo'], ['ho']
- ];
- $this->setMapperResult($sql, $params, $rows, null, null, true);
- $this->expectException(MultipleObjectsReturnedException::class);
- $this->mapper->find($sql, $params);
- }
-
- public function testFindEntityMultiple() {
- $sql = 'hi';
- $params = ['jo'];
- $rows = [
- ['jo'], ['ho']
- ];
- $this->setMapperResult($sql, $params, $rows, null, null, true);
- $this->expectException(MultipleObjectsReturnedException::class);
- $this->mapper->findOneEntity($sql, $params);
- }
-
-
- public function testDelete() {
- $sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?';
- $params = [2];
-
- $this->setMapperResult($sql, $params, [], null, null, true);
- $entity = new Example();
- $entity->setId($params[0]);
-
- $this->mapper->delete($entity);
- }
-
-
- public function testCreate() {
- $this->db->expects($this->once())
- ->method('lastInsertId')
- ->with($this->equalTo('*PREFIX*table'))
- ->willReturn(3);
- $this->mapper = new ExampleMapper($this->db);
-
- $sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
- 'VALUES(?,?)';
- $params = ['john', 'my@email'];
- $entity = new Example();
- $entity->setPreName($params[0]);
- $entity->setEmail($params[1]);
-
- $this->setMapperResult($sql, $params, [], null, null, true);
-
- $this->mapper->insert($entity);
- }
-
-
- public function testCreateShouldReturnItemWithCorrectInsertId() {
- $this->db->expects($this->once())
- ->method('lastInsertId')
- ->with($this->equalTo('*PREFIX*table'))
- ->willReturn(3);
- $this->mapper = new ExampleMapper($this->db);
-
- $sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
- 'VALUES(?,?)';
- $params = ['john', 'my@email'];
- $entity = new Example();
- $entity->setPreName($params[0]);
- $entity->setEmail($params[1]);
-
- $this->setMapperResult($sql, $params);
-
- $result = $this->mapper->insert($entity);
-
- $this->assertEquals(3, $result->getId());
- }
-
-
- 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 ' .
- '`pre_name` = ?,'.
- '`email` = ? ' .
- 'WHERE `id` = ?';
-
- $params = ['john', 'my@email', 1];
- $entity = new Example();
- $entity->setPreName($params[0]);
- $entity->setEmail($params[1]);
- $entity->setId($params[2]);
-
- $this->setMapperResult($sql, $params, [], null, null, true);
-
- $this->mapper->update($entity);
- }
-
-
- public function testUpdateNoId() {
- $params = ['john', 'my@email'];
- $entity = new Example();
- $entity->setPreName($params[0]);
- $entity->setEmail($params[1]);
-
- $this->expectException(\InvalidArgumentException::class);
-
- $this->mapper->update($entity);
- }
-
-
- public function testUpdateNothingChangedNoQuery() {
- $params = ['john', 'my@email'];
- $entity = new Example();
- $entity->setId(3);
- $entity->setEmail($params[1]);
- $entity->resetUpdatedFields();
-
- $this->db->expects($this->never())
- ->method('prepare');
-
- $this->mapper->update($entity);
- }
-
-
- public function testMapRowToEntity() {
- $entity1 = $this->mapper->mapRow(['pre_name' => 'test1', 'email' => 'test2']);
- $entity2 = new Example();
- $entity2->setPreName('test1');
- $entity2->setEmail('test2');
- $entity2->resetUpdatedFields();
- $this->assertEquals($entity2, $entity1);
- }
-
- public function testFindEntities() {
- $sql = 'hi';
- $rows = [
- ['pre_name' => 'hi']
- ];
- $entity = new Example();
- $entity->setPreName('hi');
- $entity->resetUpdatedFields();
- $this->setMapperResult($sql, [], $rows, null, null, true);
- $result = $this->mapper->findAllEntities($sql);
- $this->assertEquals([$entity], $result);
- }
-
- public function testFindEntitiesNotFound() {
- $sql = 'hi';
- $rows = [];
- $this->setMapperResult($sql, [], $rows);
- $result = $this->mapper->findAllEntities($sql);
- $this->assertEquals([], $result);
- }
-
- public function testFindEntitiesMultiple() {
- $sql = 'hi';
- $rows = [
- ['pre_name' => 'jo'], ['email' => 'ho']
- ];
- $entity1 = new Example();
- $entity1->setPreName('jo');
- $entity1->resetUpdatedFields();
- $entity2 = new Example();
- $entity2->setEmail('ho');
- $entity2->resetUpdatedFields();
- $this->setMapperResult($sql, [], $rows);
- $result = $this->mapper->findAllEntities($sql);
- $this->assertEquals([$entity1, $entity2], $result);
- }
-}
diff --git a/tests/lib/AppFramework/Db/MapperTestUtility.php b/tests/lib/AppFramework/Db/MapperTestUtility.php
deleted file mode 100644
index e17b875e4c4..00000000000
--- a/tests/lib/AppFramework/Db/MapperTestUtility.php
+++ /dev/null
@@ -1,206 +0,0 @@
-<?php
-
-/**
- * ownCloud - App Framework
- *
- * @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace Test\AppFramework\Db;
-
-use OCP\DB\IPreparedStatement;
-use OCP\DB\IResult;
-
-/**
- * Simple utility class for testing mappers
- */
-abstract class MapperTestUtility extends \Test\TestCase {
- protected $db;
- private $statement;
- private $queryAt;
- private $prepareAt;
- private $fetchAt;
- private $iterators;
-
-
- /**
- * Run this function before the actual test to either set or initialize the
- * db. After this the db can be accessed by using $this->db
- */
- protected function setUp(): void {
- parent::setUp();
-
- $this->db = $this->getMockBuilder(
- '\OCP\IDBConnection')
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->statement = $this->createMock(IPreparedStatement::class);
- $this->queryAt = 0;
- $this->prepareAt = 0;
- $this->iterators = [];
- $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 int 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
- * @param string $sql the sql query that you expect to receive
- * @param array $arguments the expected arguments for the prepare query
- * method
- * @param array $returnRows the rows that should be returned for 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 = [], $returnRows = [],
- $limit = null, $offset = null, $expectClose = false) {
- if ($limit === null && $offset === null) {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepare')
- ->with($this->equalTo($sql))
- ->will(($this->returnValue($this->statement)));
- } elseif ($limit !== null && $offset === null) {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepare')
- ->with($this->equalTo($sql), $this->equalTo($limit))
- ->will(($this->returnValue($this->statement)));
- } elseif ($limit === null && $offset !== null) {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepare')
- ->with($this->equalTo($sql),
- $this->equalTo(null),
- $this->equalTo($offset))
- ->will(($this->returnValue($this->statement)));
- } else {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepare')
- ->with($this->equalTo($sql),
- $this->equalTo($limit),
- $this->equalTo($offset))
- ->will(($this->returnValue($this->statement)));
- }
-
- $this->iterators[] = new ArgumentIterator($returnRows);
-
- $iterators = $this->iterators;
- $fetchAt = $this->fetchAt;
-
- $this->statement->expects($this->any())
- ->method('fetch')
- ->willReturnCallback(
- function () use ($iterators, $fetchAt) {
- $iterator = $iterators[$fetchAt];
- $result = $iterator->next();
-
- if ($result === false) {
- $fetchAt++;
- }
-
- $this->queryAt++;
-
- return $result;
- }
- );
-
- if ($this->isAssocArray($arguments)) {
- foreach ($arguments as $key => $argument) {
- $pdoConstant = $this->getPDOType($argument);
- $this->statement->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->statement->expects($this->at($this->queryAt))
- ->method('bindValue')
- ->with($this->equalTo($index),
- $this->equalTo($argument),
- $this->equalTo($pdoConstant));
- $index++;
- $this->queryAt++;
- }
- }
-
- $this->statement->expects($this->at($this->queryAt))
- ->method('execute')
- ->willReturnCallback(function ($sql, $p = null, $o = null, $s = null) {
- return $this->createMock(IResult::class);
- });
- $this->queryAt++;
-
-
-
- if ($expectClose) {
- $closing = $this->at($this->queryAt);
- } else {
- $closing = $this->any();
- }
- $this->statement->expects($closing)->method('closeCursor');
- $this->queryAt++;
-
- $this->prepareAt++;
- $this->fetchAt++;
- }
-}
-
-
-class ArgumentIterator {
- private $arguments;
-
- public function __construct($arguments) {
- $this->arguments = $arguments;
- }
-
- public function next() {
- $result = array_shift($this->arguments);
- if ($result === null) {
- return false;
- } else {
- return $result;
- }
- }
-}