]> git.mxchange.org Git - friendica.git/commitdiff
Adapt tests
authorPhilipp <admin@philipp.info>
Mon, 23 Aug 2021 13:47:41 +0000 (15:47 +0200)
committerPhilipp <admin@philipp.info>
Wed, 25 Aug 2021 12:22:42 +0000 (14:22 +0200)
src/DI.php
src/Network/HTTPClient.php
src/Network/IHTTPClient.php
tests/datasets/curl/image.content
tests/src/Core/InstallerTest.php
tests/src/Core/StorageManagerTest.php
tests/src/Network/HTTPRequestTest.php
tests/src/Util/ImagesTest.php [new file with mode: 0644]

index 5c56b45e132e250f8719a0d5bcd74a5c91a8614f..7f6f28f33cb45257772361a6524a27ceb76e678d 100644 (file)
@@ -39,6 +39,17 @@ abstract class DI
                self::$dice = $dice;
        }
 
+       /**
+        * Returns a clone of the current dice instance
+        * This usefull for overloading the current instance with mocked methods during tests
+        *
+        * @return Dice
+        */
+       public static function getDice()
+       {
+               return clone self::$dice;
+       }
+
        //
        // common instances
        //
index d83b805df0d278229769228aa58f032d00ef2501..c90dbc89642816512dcdf239e65d98abc4d85095 100644 (file)
@@ -92,11 +92,6 @@ class HTTPClient implements IHTTPClient
                        return CurlResult::createErrorCurl($url);
                }
 
-               if (Network::isRedirectBlocked($url)) {
-                       $this->logger->info('Domain should not be redirected.', ['url' => $url]);
-                       return CurlResult::createErrorCurl($url);
-               }
-
                $conf = [];
 
                if (!empty($opts['cookiejar'])) {
index 180908eede4dd449115e20f6e42a49468343441d..c8611e8f5b2dddae6c92e2fb916712def153db6d 100644 (file)
@@ -107,7 +107,6 @@ interface IHTTPClient
         *
         * @return string A canonical URL
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @see   ParseUrl::getSiteinfo
         */
        public function finalUrl(string $url);
 }
index eea1b7e46350ecea68f98f1d84f0f8786fddf9aa..98285c3e11ad2ad3a7e0610ee356cea43f7fbb49 100644 (file)
Binary files a/tests/datasets/curl/image.content and b/tests/datasets/curl/image.content differ
index 8c72b7b2b909b1c03d4cc5b6647efcb1150d49ff..1263fe7e67b73f0f7e72c0ed3f3af80287ec87a3 100644 (file)
@@ -26,7 +26,7 @@ use Dice\Dice;
 use Friendica\Core\Config\Cache;
 use Friendica\DI;
 use Friendica\Network\IHTTPResult;
-use Friendica\Network\IHTTPRequest;
+use Friendica\Network\IHTTPClient;
 use Friendica\Test\MockedTest;
 use Friendica\Test\Util\VFSTrait;
 use Mockery;
@@ -331,7 +331,7 @@ class InstallerTest extends MockedTest
                        ->andReturn('test Error');
 
                // Mocking the CURL Request
-               $networkMock = Mockery::mock(IHTTPRequest::class);
+               $networkMock = Mockery::mock(IHTTPClient::class);
                $networkMock
                        ->shouldReceive('fetchFull')
                        ->with('https://test/install/testrewrite')
@@ -342,7 +342,7 @@ class InstallerTest extends MockedTest
                        ->andReturn($IHTTPResult);
 
                $this->dice->shouldReceive('create')
-                    ->with(IHTTPRequest::class)
+                    ->with(IHTTPClient::class)
                     ->andReturn($networkMock);
 
                DI::init($this->dice);
@@ -378,7 +378,7 @@ class InstallerTest extends MockedTest
                        ->andReturn('204');
 
                // Mocking the CURL Request
-               $networkMock = Mockery::mock(IHTTPRequest::class);
+               $networkMock = Mockery::mock(IHTTPClient::class);
                $networkMock
                        ->shouldReceive('fetchFull')
                        ->with('https://test/install/testrewrite')
@@ -389,7 +389,7 @@ class InstallerTest extends MockedTest
                        ->andReturn($IHTTPResultW);
 
                $this->dice->shouldReceive('create')
-                          ->with(IHTTPRequest::class)
+                          ->with(IHTTPClient::class)
                           ->andReturn($networkMock);
 
                DI::init($this->dice);
index 9e8e3aa2c242ebe1bce2db699aac4aae1aaa66bc..93fc0b6648faa9459f4f8d8ed81d4c12c96c828c 100644 (file)
@@ -34,7 +34,7 @@ use Friendica\Factory\ConfigFactory;
 use Friendica\Model\Config\Config;
 use Friendica\Model\Storage;
 use Friendica\Core\Session;
-use Friendica\Network\HTTPRequest;
+use Friendica\Network\HTTPClient;
 use Friendica\Test\DatabaseTest;
 use Friendica\Test\Util\Database\StaticDatabase;
 use Friendica\Test\Util\VFSTrait;
@@ -55,7 +55,7 @@ class StorageManagerTest extends DatabaseTest
        private $logger;
        /** @var L10n */
        private $l10n;
-       /** @var HTTPRequest */
+       /** @var HTTPClient */
        private $httpRequest;
 
        protected function setUp(): void
@@ -84,7 +84,7 @@ class StorageManagerTest extends DatabaseTest
 
                $this->l10n = \Mockery::mock(L10n::class);
 
-               $this->httpRequest = \Mockery::mock(HTTPRequest::class);
+               $this->httpRequest = \Mockery::mock(HTTPClient::class);
        }
 
        /**
index 4fd0ab179486b4c414f9e1f26ceadc3c90bb72f3..230dc3eec3e2dbe8a7ee7a58088f2c018101f355 100644 (file)
@@ -3,58 +3,87 @@
 namespace Friendica\Test\src\Network;
 
 use Dice\Dice;
-use Friendica\App\BaseURL;
-use Friendica\Core\Config\IConfig;
 use Friendica\DI;
-use Friendica\Network\HTTPRequest;
-use Friendica\Network\IHTTPRequest;
+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
 {
-       public function testImageFetch()
+       /** @var HandlerStack */
+       protected $handler;
+
+       protected function setUp(): void
        {
-               $mock = new MockHandler([
-                       new Response(200, [
-                               'Server' => 'tsa_b',
-                               'Content-Type' => 'image/png',
-                               'Cache-Control' => 'max-age=604800, must-revalidate',
-                               'Content-Length' => 24875,
-                       ], file_get_contents(__DIR__ . '/../../datasets/curl/image.content'))
-               ]);
-
-               $config = \Mockery::mock(IConfig::class);
-               $config->shouldReceive('get')->with('system', 'curl_range_bytes', 0)->once()->andReturn(null);
-               $config->shouldReceive('get')->with('system', 'verifyssl')->once();
-               $config->shouldReceive('get')->with('system', 'proxy')->once();
-               $config->shouldReceive('get')->with('system', 'ipv4_resolve', false)->once()->andReturnFalse();
-               $config->shouldReceive('get')->with('system', 'blocklist', [])->once()->andReturn([]);
-
-               $baseUrl = \Mockery::mock(BaseURL::class);
-               $baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
+               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();
 
-               $httpRequest = new HTTPRequest(new NullLogger(), $profiler, $config, $baseUrl);
+               $httpClient = new HTTPClient(new NullLogger(), $profiler, $client, $resolver);
 
-               self::assertInstanceOf(IHTTPRequest::class, $httpRequest);
+               $dice    = DI::getDice();
+               $newDice = \Mockery::mock($dice)->makePartial();
+               $newDice->shouldReceive('create')->with(IHTTPClient::class)->andReturn($httpClient);
+               DI::init($newDice);
+       }
 
-               $dice = \Mockery::mock(Dice::class);
-               $dice->shouldReceive('create')->with(IHTTPRequest::class)->andReturn($httpRequest)->once();
-               $dice->shouldReceive('create')->with(BaseURL::class)->andReturn($baseUrl);
-               $dice->shouldReceive('create')->with(IConfig::class)->andReturn($config)->once();
+       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',
+                               ]
+                       ]
+               ];
+       }
 
-               DI::init($dice);
+       /**
+        * @dataProvider dataImages
+        */
+       public function testGetInfoFromURL(string $url, array $headers, string $data, array $assertion)
+       {
+               $this->handler->setHandler(new MockHandler([
+                       new Response(200, $headers, $data),
+               ]));
 
-               print_r(Images::getInfoFromURL('https://pbs.twimg.com/profile_images/2365515285/9re7kx4xmc0eu9ppmado.png'));
+               self::assertArraySubset($assertion, Images::getInfoFromURL($url));
        }
 }
diff --git a/tests/src/Util/ImagesTest.php b/tests/src/Util/ImagesTest.php
new file mode 100644 (file)
index 0000000..d35f9c4
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace Friendica\Test\src\Util;
+
+use Friendica\Test\MockedTest;
+
+class ImagesTest extends MockedTest
+{
+       public function testGetInfoFromURL()
+       {
+
+       }
+}