]> git.mxchange.org Git - friendica.git/blobdiff - tests/Util/DBAMockTrait.php
Merge pull request #6732 from MrPetovan/bug/1777-fix-blocked-contact-group
[friendica.git] / tests / Util / DBAMockTrait.php
index 2ee54adaa17e923397cc413f76b6e52b6513f242..ab5bc6059edec3765c9fa132610ec7f692f6c0fc 100644 (file)
@@ -2,8 +2,14 @@
 
 namespace Friendica\Test\Util;
 
+use Friendica\Database\DBA;
 use Mockery\MockInterface;
 
+class DBAStub
+{
+       public static $connected = true;
+}
+
 /**
  * Trait to mock the DBA connection status
  */
@@ -14,6 +20,13 @@ trait DBAMockTrait
         */
        private $dbaMock;
 
+       private function checkMock()
+       {
+               if (!isset($this->dbaMock)) {
+                       $this->dbaMock = \Mockery::namedMock(DBA::class, DBAStub::class);
+               }
+       }
+
        /**
         * Mocking DBA::connect()
         *
@@ -22,9 +35,7 @@ trait DBAMockTrait
         */
        public function mockConnect($return = true, $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                $this->dbaMock
                        ->shouldReceive('connect')
@@ -40,9 +51,7 @@ trait DBAMockTrait
         */
        public function mockConnected($return = true, $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                $this->dbaMock
                        ->shouldReceive('connected')
@@ -59,9 +68,7 @@ trait DBAMockTrait
         */
        public function mockFetchFirst($arg, $return = true, $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                $this->dbaMock
                        ->shouldReceive('fetchFirst')
@@ -70,6 +77,45 @@ trait DBAMockTrait
                        ->andReturn($return);
        }
 
+       /**
+        * Mocking each DBA::fetch() call of an statement
+        *
+        * @param array $stmt The result statement (array)
+        * @param null|int $times How often the method will get used
+        */
+       public function mockFetchLoop($stmt = [], $times = null)
+       {
+               $this->checkMock();
+
+               foreach ($stmt as $item) {
+                       $this->dbaMock
+                               ->shouldReceive('fetch')
+                               ->times($times)
+                               ->andReturn($item);
+               }
+
+               // The last mock call of a fetch (=> breaking the loop)
+               $this->dbaMock
+                       ->shouldReceive('fetch')
+                       ->times($times)
+                       ->andReturn(false);
+       }
+
+       /**
+        * Mocking DBA::close()
+        *
+        * @param array $return The return per fetch
+        * @param null|int $times How often the method will get used
+        */
+       public function mockDbaClose($return = [], $times = null)
+       {
+               $this->checkMock();
+
+               $this->dbaMock
+                       ->shouldReceive('close')
+                       ->times($times)
+                       ->andReturn($return);
+       }
 
        /**
         * Mocking DBA::select()
@@ -82,9 +128,7 @@ trait DBAMockTrait
         */
        public function mockSelect($tableName, $select = [], $where = [], $return = null, $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                $this->dbaMock
                        ->shouldReceive('select')
@@ -94,23 +138,102 @@ trait DBAMockTrait
        }
 
        /**
-        * Mocking DBA::selectFirst()
+        * Mocking DBA::delete()
         *
         * @param string $tableName The name of the table
-        * @param array $select The Select Array (Default is [])
         * @param array $where The Where Array (Default is [])
+        * @param bool $return The array to return (Default is true)
+        * @param null|int $times How often the method will get used
+        */
+       public function mockDBADelete($tableName, $where = [], $return = true, $times = null)
+       {
+               $this->checkMock();
+
+               $this->dbaMock
+                       ->shouldReceive('delete')
+                       ->with($tableName, $where)
+                       ->times($times)
+                       ->andReturn($return);
+       }
+
+       /**
+        * Mocking DBA::update()
+        *
+        * @param string $expTableName The name of the table
+        * @param array $expFields The Fields Array
+        * @param array $expCondition The Condition Array
+        * @param array $expOld_fields The Old Fieldnames (Default is [])
+        * @param bool $return true if the update was successful
+        * @param null|int $times How often the method will get used
+        */
+       public function mockDBAUpdate($expTableName, $expFields, $expCondition, $expOld_fields = [], $return = true, $times = null)
+       {
+               $this->checkMock();
+
+               $closure = function ($tableName, $fields, $condition, $old_fields = []) use ($expTableName, $expFields, $expCondition, $expOld_fields) {
+                       return
+                               $tableName == $expTableName &&
+                               $fields == $expFields &&
+                               $condition == $expCondition &&
+                               $old_fields == $expOld_fields;
+               };
+
+               $this->dbaMock
+                       ->shouldReceive('update')
+                       ->withArgs($closure)
+                       ->times($times)
+                       ->andReturn($return);
+       }
+
+       /**
+        * Mocking DBA::insert()
+        *
+        * @param string $expTableName    The name of the table
+        * @param array  $expParam        The Parameters Array
+        * @param bool   $expOnDuplUpdate Update on a duplicated entry
+        * @param bool   $return          True if the insert was successful
+        * @param null|int $times How often the method will get used
+        */
+       public function mockDBAInsert($expTableName, $expParam, $expOnDuplUpdate = false, $return = true, $times = null)
+       {
+               $this->checkMock();
+
+               $closure = function ($tableName, $param, $on_duplicate_update = false) use ($expTableName, $expParam, $expOnDuplUpdate) {
+                       return $tableName            == $expTableName
+                               && $param                == $expParam
+                               && $on_duplicate_update  == $expOnDuplUpdate;
+
+               };
+
+               $this->dbaMock
+                       ->shouldReceive('insert')
+                       ->withArgs($closure)
+                       ->times($times)
+                       ->andReturn($return);
+       }
+
+       /**
+        * Mocking DBA::selectFirst()
+        *
+        * @param string $expTableName The name of the table
+        * @param array $expSelect The Select Array (Default is [])
+        * @param array $expWhere The Where Array (Default is [])
         * @param array $return The array to return (Default is [])
         * @param null|int $times How often the method will get used
         */
-       public function mockSelectFirst($tableName, $select = [], $where = [], $return = [], $times = null)
+       public function mockSelectFirst($expTableName, $expSelect = [], $expWhere = [], $return = [], $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
+
+               $closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
+                       return $tableName === $expTableName
+                               && $select === $expSelect
+                               && $where === $expWhere;
+               };
 
                $this->dbaMock
                        ->shouldReceive('selectFirst')
-                       ->with($tableName, $select, $where)
+                       ->withArgs($closure)
                        ->times($times)
                        ->andReturn($return);
        }
@@ -124,9 +247,7 @@ trait DBAMockTrait
         */
        public function mockIsResult($record, $return = true, $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                $this->dbaMock
                        ->shouldReceive('isResult')
@@ -144,9 +265,7 @@ trait DBAMockTrait
         */
        public function mockToArray($record = null, $return = [], $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                $this->dbaMock
                        ->shouldReceive('toArray')
@@ -155,7 +274,6 @@ trait DBAMockTrait
                        ->andReturn($return);
        }
 
-
        /**
         * Mocking DBA::p()
         *
@@ -165,9 +283,7 @@ trait DBAMockTrait
         */
        public function mockP($sql = null, $return = null, $times = null)
        {
-               if (!isset($this->dbaMock)) {
-                       $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               }
+               $this->checkMock();
 
                if (!isset($sql)) {
                        $this->dbaMock
@@ -182,4 +298,38 @@ trait DBAMockTrait
                                ->andReturn($return);
                }
        }
+
+       /**
+        * Mocking DBA::lock()
+        *
+        * @param string $table The table to lock
+        * @param bool $return True, if the lock is set successful
+        * @param null|int $times How often the method will get used
+        */
+       public function mockDbaLock($table, $return = true, $times = null)
+       {
+               $this->checkMock();
+
+               $this->dbaMock
+                       ->shouldReceive('lock')
+                       ->with($table)
+                       ->times($times)
+                       ->andReturn($return);
+       }
+
+       /**
+        * Mocking DBA::unlock()
+        *
+        * @param bool $return True, if the lock is set successful
+        * @param null|int $times How often the method will get used
+        */
+       public function mockDbaUnlock( $return = true, $times = null)
+       {
+               $this->checkMock();
+
+               $this->dbaMock
+                       ->shouldReceive('unlock')
+                       ->times($times)
+                       ->andReturn($return);
+       }
 }