]> git.mxchange.org Git - friendica.git/commitdiff
Introduce DiceTestTrait for partial mocking DI:: calls
authorPhilipp <admin@philipp.info>
Tue, 24 Aug 2021 11:19:17 +0000 (13:19 +0200)
committerPhilipp <admin@philipp.info>
Wed, 25 Aug 2021 12:22:43 +0000 (14:22 +0200)
tests/DiceTestTrait.php [new file with mode: 0644]
tests/src/Network/HTTPRequestTest.php [deleted file]
tests/src/Util/ImagesTest.php

diff --git a/tests/DiceTestTrait.php b/tests/DiceTestTrait.php
new file mode 100644 (file)
index 0000000..2426c20
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test;
+
+use Friendica\DI;
+use Friendica\Network\HTTPClient;
+use Friendica\Network\IHTTPClient;
+use GuzzleHttp\Client;
+use GuzzleHttp\HandlerStack;
+use mattwright\URLResolver;
+
+/**
+ * This class mocks some DICE dependencies because they're not direct usable for test environments
+ * (Like fetching data from external endpoints)
+ */
+trait DiceTestTrait
+{
+       /**
+        * Handler for mocking requests anywhere for testing purpose
+        *
+        * @var HandlerStack
+        */
+       protected static $httpRequestHandler;
+
+       protected static function setUpDice(): void
+       {
+               if (!empty(self::$httpRequestHandler) && self::$httpRequestHandler instanceof HandlerStack) {
+                       return;
+               }
+
+               self::$httpRequestHandler = HandlerStack::create();
+
+               $client = new Client(['handler' => self::$httpRequestHandler]);
+
+               $resolver = \Mockery::mock(URLResolver::class);
+
+               $httpClient = new HTTPClient(DI::logger(), DI::profiler(), $client, $resolver);
+
+               $dice    = DI::getDice();
+               $newDice = \Mockery::mock($dice)->makePartial();
+               $newDice->shouldReceive('create')->with(IHTTPClient::class)->andReturn($httpClient);
+               DI::init($newDice);
+       }
+
+       protected function tearDown() : void
+       {
+               \Mockery::close();
+
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Network/HTTPRequestTest.php b/tests/src/Network/HTTPRequestTest.php
deleted file mode 100644 (file)
index 230dc3e..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-<?php
-
-namespace Friendica\Test\src\Network;
-
-use Dice\Dice;
-use Friendica\DI;
-use Friendica\Network\HTTPClient;
-use Friendica\Network\IHTTPClient;
-use Friendica\Test\MockedTest;
-use Friendica\Util\Images;
-use Friendica\Util\Profiler;
-use GuzzleHttp\Client;
-use GuzzleHttp\Handler\MockHandler;
-use GuzzleHttp\HandlerStack;
-use GuzzleHttp\Psr7\Response;
-use mattwright\URLResolver;
-use Psr\Log\NullLogger;
-
-require_once __DIR__ . '/../../../static/dbstructure.config.php';
-
-class HTTPRequestTest extends MockedTest
-{
-       /** @var HandlerStack */
-       protected $handler;
-
-       protected function setUp(): void
-       {
-               parent::setUp();
-
-               $this->handler = HandlerStack::create();
-
-               $client = new Client(['handler' => $this->handler]);
-
-               $resolver = \Mockery::mock(URLResolver::class);
-
-               $profiler = \Mockery::mock(Profiler::class);
-               $profiler->shouldReceive('startRecording')->andReturnTrue();
-               $profiler->shouldReceive('stopRecording')->andReturnTrue();
-
-               $httpClient = new HTTPClient(new NullLogger(), $profiler, $client, $resolver);
-
-               $dice    = DI::getDice();
-               $newDice = \Mockery::mock($dice)->makePartial();
-               $newDice->shouldReceive('create')->with(IHTTPClient::class)->andReturn($httpClient);
-               DI::init($newDice);
-       }
-
-       public function dataImages()
-       {
-               return [
-                       'image1' => [
-                               'url'     => 'https://pbs.twimg.com/profile_images/2365515285/9re7kx4xmc0eu9ppmado.png',
-                               'headers' => [
-                                       'Server'                        => 'tsa_b',
-                                       'Content-Type'                  => 'image/png',
-                                       'Cache-Control'                 => 'max-age=604800,must-revalidate',
-                                       'Last-Modified'                 => 'Thu,04Nov201001:42:54GMT',
-                                       'Content-Length'                => '24875',
-                                       'Access-Control-Allow-Origin'   => '*',
-                                       'Access-Control-Expose-Headers' => 'Content-Length',
-                                       'Date'                          => 'Mon,23Aug202112:39:00GMT',
-                                       'Connection'                    => 'keep-alive',
-                               ],
-                               'data'      => file_get_contents(__DIR__ . '/../../datasets/curl/image.content'),
-                               'assertion' => [
-                                       '0'    => '400',
-                                       '1'    => '400',
-                                       '2'    => '3',
-                                       '3'    => 'width="400" height="400"',
-                                       'bits' => '8',
-                                       'mime' => 'image/png',
-                                       'size' => '24875',
-                               ]
-                       ]
-               ];
-       }
-
-       /**
-        * @dataProvider dataImages
-        */
-       public function testGetInfoFromURL(string $url, array $headers, string $data, array $assertion)
-       {
-               $this->handler->setHandler(new MockHandler([
-                       new Response(200, $headers, $data),
-               ]));
-
-               self::assertArraySubset($assertion, Images::getInfoFromURL($url));
-       }
-}
index d35f9c4eca5179587c9a1231df215d91b9d0046e..a7cc682f9853ff2623f79095e6f7274160ac0957 100644 (file)
@@ -2,12 +2,70 @@
 
 namespace Friendica\Test\src\Util;
 
+use Friendica\Test\DiceTestTrait;
 use Friendica\Test\MockedTest;
+use Friendica\Util\Images;
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\Psr7\Response;
 
 class ImagesTest extends MockedTest
 {
-       public function testGetInfoFromURL()
+       use DiceTestTrait;
+
+       public static function setUpBeforeClass(): void
+       {
+               parent::setUpBeforeClass();
+
+               self::setUpDice();
+       }
+
+       public function dataImages()
+       {
+               return [
+                       'image' => [
+                               'url'     => 'https://pbs.twimg.com/profile_images/2365515285/9re7kx4xmc0eu9ppmado.png',
+                               'headers' => [
+                                       'Server'                        => 'tsa_b',
+                                       'Content-Type'                  => 'image/png',
+                                       'Cache-Control'                 => 'max-age=604800,must-revalidate',
+                                       'Last-Modified'                 => 'Thu,04Nov201001:42:54GMT',
+                                       'Content-Length'                => '24875',
+                                       'Access-Control-Allow-Origin'   => '*',
+                                       'Access-Control-Expose-Headers' => 'Content-Length',
+                                       'Date'                          => 'Mon,23Aug202112:39:00GMT',
+                                       'Connection'                    => 'keep-alive',
+                               ],
+                               'data'      => file_get_contents(__DIR__ . '/../../datasets/curl/image.content'),
+                               'assertion' => [
+                                       '0'    => '400',
+                                       '1'    => '400',
+                                       '2'    => '3',
+                                       '3'    => 'width="400" height="400"',
+                                       'bits' => '8',
+                                       'mime' => 'image/png',
+                                       'size' => '24875',
+                               ]
+                       ],
+                       'emptyUrl' => [
+                               'url'       => '',
+                               'headers'   => [],
+                               'data'      => '',
+                               'assertion' => [],
+                       ],
+               ];
+       }
+
+       /**
+        * Test the Images::getInfoFromURL() method
+        *
+        * @dataProvider dataImages
+        */
+       public function testGetInfoFromURL(string $url, array $headers, string $data, array $assertion)
        {
+               self::$httpRequestHandler->setHandler(new MockHandler([
+                       new Response(200, $headers, $data),
+               ]));
 
+               self::assertArraySubset($assertion, Images::getInfoFromURL($url));
        }
 }