diff --git a/.gitattributes b/.gitattributes index 852b663..4337333 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,12 @@ # LF for text type files -* text=auto eol=lf \ No newline at end of file +* text=auto eol=lf + +/.github export-ignore +/test export-ignore +/tests export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.phpcs.xml export-ignore +mockutils.iml export-ignore +phpstan.neon export-ignore +phpunit.xml.dist export-ignore \ No newline at end of file diff --git a/composer.json b/composer.json index 7989995..333f57a 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,8 @@ "require-dev": { "phpstan/phpstan": "^1.10", "phpunit/phpunit": "11.*", - "php-mock/php-mock": "^2.5", - "squizlabs/php_codesniffer": "^3.8" + "squizlabs/php_codesniffer": "^3.8", + "ernestmarcinko/mockutils": "^1.0" }, "require": { "php": "^8.3" diff --git a/src/RequestHandler.php b/src/RequestHandler.php index 61e0864..a178596 100644 --- a/src/RequestHandler.php +++ b/src/RequestHandler.php @@ -15,9 +15,9 @@ interface RequestHandler { * @param string|array|false|null $post_fields */ public function make( - RequestMethods $method, - string $endpoint, - array|null $header = null, + RequestMethods $method, + string $endpoint, + array|null $header = null, array|string|bool|null $post_fields = null ): static; diff --git a/src/WaifuApi.php b/src/WaifuApi.php index c6ba06a..5c3dcdd 100644 --- a/src/WaifuApi.php +++ b/src/WaifuApi.php @@ -68,7 +68,7 @@ function ($v, $k) { ARRAY_FILTER_USE_BOTH )); if ($params !== '') { - $url .= '?' . $params; + $url .= '?' . $params; } if (isset($args['url'])) { $post_fields['url'] = $args['url']; diff --git a/src/WaifuRequestHandler.php b/src/WaifuRequestHandler.php index 65d01e5..82805ce 100644 --- a/src/WaifuRequestHandler.php +++ b/src/WaifuRequestHandler.php @@ -27,9 +27,9 @@ class WaifuRequestHandler implements RequestHandler { * @throws Exception */ public function make( - RequestMethods $method, - string $endpoint, - array|null $header = null, + RequestMethods $method, + string $endpoint, + array|null $header = null, array|string|bool|null $post_fields = null ): static { $curl = curl_init(); diff --git a/tests/WaifuTests/GlobalMock.php b/tests/WaifuTests/GlobalMock.php deleted file mode 100644 index aad53c9..0000000 --- a/tests/WaifuTests/GlobalMock.php +++ /dev/null @@ -1,52 +0,0 @@ - - */ - private static array $mocked = array(); - - /** - * Creates a global function mock with a pre-determined response - * - * @param string $function_name - * @param mixed $return - * @throws MockEnabledException - */ - public static function mock(string $function_name, mixed $return): void { - self::disable($function_name); - $builder = new MockBuilder(); - $builder->setNamespace("ErnestMarcinko\\WaifuVault") - ->setName($function_name) - ->setFunction(is_callable($return) ? $return : fn()=>$return); - $mock = $builder->build(); - $mock->enable(); - self::$mocked[$function_name] = $mock; - } - - /** - * Disables all global function mocks or a single function mock if $function_name is set. - * @param string $function_name - * @return void - */ - public static function disable(string $function_name = ''): void { - if ($function_name === '') { - foreach (self::$mocked as $name => $return) { - self::$mocked[$name]->disable(); - unset(self::$mocked[$name]); - } - } elseif (isset(self::$mocked[$function_name])) { - self::$mocked[$function_name]->disable(); - unset(self::$mocked[$function_name]); - } - } -} diff --git a/tests/WaifuTests/WaifuApiTest.php b/tests/WaifuTests/WaifuApiTest.php index 71dcb8f..b29031b 100644 --- a/tests/WaifuTests/WaifuApiTest.php +++ b/tests/WaifuTests/WaifuApiTest.php @@ -125,7 +125,6 @@ public function testGetFile(): void { } public function setUp(): void { - GlobalMock::disable(); $args = [ "token" => "13b2485a-1010-4e3e-8f75-20f2a0c50b56", "url" => "https://waifuvault.moe/f/1711098733870/image.jpg", diff --git a/tests/WaifuTests/WaifuRequestHandlerTest.php b/tests/WaifuTests/WaifuRequestHandlerTest.php index ff14bf3..176937f 100644 --- a/tests/WaifuTests/WaifuRequestHandlerTest.php +++ b/tests/WaifuTests/WaifuRequestHandlerTest.php @@ -7,6 +7,7 @@ namespace ErnestMarcinko\WaifuTests; +use ErnestMarcinko\MockUtils\MockUtils; use ErnestMarcinko\WaifuVault\Exceptions\WaifuException; use ErnestMarcinko\WaifuVault\RequestMethods; use ErnestMarcinko\WaifuVault\WaifuRequestHandler; @@ -16,6 +17,8 @@ use PHPUnit\Framework\TestCase; class WaifuRequestHandlerTest extends TestCase { + use MockUtils; + private WaifuResponse $waifuResponse; public function testMake(): void { $handler = new WaifuRequestHandler(); @@ -29,7 +32,7 @@ public function testMake(): void { }, 'curl_exec' => 'result', 'curl_getinfo' => 200, - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler->make(RequestMethods::GET, 'fake1'); $this->setGlobalMocks([ @@ -41,7 +44,7 @@ public function testMake(): void { $this->assertEquals([1, '1', 'hey'], $curl_options[CURLOPT_HTTPHEADER]); $this->addToAssertionCount(5); } - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler->make(RequestMethods::POST, 'fake2', [1, '1', 'hey']); // @phpstan-ignore-line $this->setGlobalMocks([ @@ -53,7 +56,7 @@ public function testMake(): void { $this->assertEquals([1, '1', 'hey'], $curl_options[CURLOPT_POSTFIELDS]); $this->addToAssertionCount(5); } - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler->make(RequestMethods::PUT, 'fake3', null, [1, '1', 'hey']); // @phpstan-ignore-line $this->setGlobalMocks([ @@ -66,11 +69,11 @@ public function testMake(): void { $this->addToAssertionCount(6); }, 'curl_exec' => null - ]); + ], "ErnestMarcinko\\WaifuVault"); $this->expectException(Exception::class); $handler->make(RequestMethods::PATCH, 'fake4', null, [1, '1', 'hey']); // @phpstan-ignore-line - $this->assertSame(16, $this->numberOfAssertionsPerformed()); + $this->assertSame(19, $this->numberOfAssertionsPerformed()); } public function testGetWaifu(): void { @@ -78,79 +81,65 @@ public function testGetWaifu(): void { 'curl_exec' => json_encode((array)$this->waifuResponse), 'curl_getinfo' => 200, 'json_validate' => true, - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler = new WaifuRequestHandler(); $response = $handler->make(RequestMethods::GET, 'fake') ->getWaifu(); $this->assertEqualsCanonicalizing($this->waifuResponse, $response); - try { - $this->setGlobalMocks([ - 'curl_exec' => 'result', - 'curl_getinfo' => 200, - 'json_validate' => false, - ]); - $handler = new WaifuRequestHandler(); - $handler->make(RequestMethods::GET, 'fake') - ->getWaifu(); - } catch (Exception) { - $this->addToAssertionCount(1); - } - - try { - $this->setGlobalMocks([ - 'curl_exec' => 'result', - 'curl_getinfo' => 200, - 'json_validate' => true, - 'json_decode' => null, - ]); - $handler = new WaifuRequestHandler(); - $handler->make(RequestMethods::GET, 'fake') - ->getWaifu(); - } catch (Exception) { - $this->addToAssertionCount(1); - } + $this->setGlobalMocks([ + 'curl_exec' => 'result', + 'curl_getinfo' => 200, + 'json_validate' => false, + ], "ErnestMarcinko\\WaifuVault"); + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->make(RequestMethods::GET, 'fake')->getWaifu(), + Exception::class + ); + + $this->setGlobalMocks([ + 'curl_exec' => 'result', + 'curl_getinfo' => 200, + 'json_validate' => true, + 'json_decode' => null, + ], "ErnestMarcinko\\WaifuVault"); + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->make(RequestMethods::GET, 'fake')->getWaifu(), + Exception::class + ); // trigger responseErrorCheck #1 - try { - $this->setGlobalMocks([ - 'curl_exec' => 'result', - 'curl_getinfo' => 300, - ]); - $handler = new WaifuRequestHandler(); - $handler->make(RequestMethods::GET, 'fake') - ->getWaifu(); - } catch (Exception $e) { - $this->assertSame(ErrorException::class, get_class($e)); - $this->addToAssertionCount(1); - } + $this->setGlobalMocks([ + 'curl_exec' => 'result', + 'curl_getinfo' => 300, + ], "ErnestMarcinko\\WaifuVault"); + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->make(RequestMethods::GET, 'fake')->getWaifu(), + ErrorException::class + ); // trigger responseErrorCheck #1 - try { - $this->setGlobalMocks([ - 'curl_exec' => 'result', - 'curl_getinfo' => 300, - 'json_validate' => true, - 'json_decode' => array() - ]); - $handler = new WaifuRequestHandler(); - $handler->make(RequestMethods::GET, 'fake') - ->getWaifu(); - } catch (Exception $e) { - $this->assertSame(WaifuException::class, get_class($e)); - $this->addToAssertionCount(1); - } - - // trigger responseErrorCheck #3 - call without ->make() - try { - $handler = new WaifuRequestHandler(); - $handler->getWaifu(); - } catch (Exception $e) { - $this->assertSame(ErrorException::class, get_class($e)); - $this->addToAssertionCount(1); - } - - $this->assertSame(5, $this->numberOfAssertionsPerformed()); + $this->setGlobalMocks([ + 'curl_exec' => 'result', + 'curl_getinfo' => 300, + 'json_validate' => true, + 'json_decode' => array() + ], "ErnestMarcinko\\WaifuVault"); + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->make(RequestMethods::GET, 'fake')->getWaifu(), + WaifuException::class + ); + + // trigger responseErrorCheck #3 + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->getWaifu(), + ErrorException::class + ); } public function testGetTrue(): void { @@ -158,21 +147,18 @@ public function testGetTrue(): void { 'curl_exec' => 'true', 'curl_getinfo' => 200, 'json_validate' => true, - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler = new WaifuRequestHandler(); $response = $handler->make(RequestMethods::GET, 'fake') ->getTrue(); $this->assertSame(true, $response); // trigger responseErrorCheck call without ->make() - try { - $handler = new WaifuRequestHandler(); - $handler->getTrue(); - } catch (Exception $e) { - $this->assertSame(ErrorException::class, get_class($e)); - $this->addToAssertionCount(1); - } - $this->assertSame(1, $this->numberOfAssertionsPerformed()); + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->getTrue(), + ErrorException::class + ); } public function testGetRaw(): void { @@ -180,35 +166,31 @@ public function testGetRaw(): void { 'curl_exec' => 'content', 'curl_getinfo' => 200, 'json_validate' => true, - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler = new WaifuRequestHandler(); $response = $handler->make(RequestMethods::GET, 'fake') ->getRaw(); $this->assertSame('content', $response); // trigger responseErrorCheck call without ->make() - try { - $handler = new WaifuRequestHandler(); - $handler->getRaw(); - } catch (Exception $e) { - $this->assertSame(ErrorException::class, get_class($e)); - $this->addToAssertionCount(1); - } - $this->assertSame(1, $this->numberOfAssertionsPerformed()); + $handler = new WaifuRequestHandler(); + $this->expectCatchException( + fn()=>$handler->getRaw(), + ErrorException::class + ); $this->setGlobalMocks([ 'curl_exec' => 'content', 'curl_getinfo' => 403, 'json_validate' => true, - ]); + ], "ErnestMarcinko\\WaifuVault"); $handler = new WaifuRequestHandler(); $this->expectException(Exception::class); - $handler->make(RequestMethods::GET, 'fake') - ->getRaw(); + $handler->make(RequestMethods::GET, 'fake')->getRaw(); } public function setUp(): void { - GlobalMock::disable(); + $this->unsetGlobalMocks(); $args = [ "token" => "13b2485a-1010-4e3e-8f75-20f2a0c50b56", "url" => "https://waifuvault.moe/f/1711098733870/image.jpg", @@ -217,20 +199,4 @@ public function setUp(): void { ]; $this->waifuResponse = new WaifuResponse(...$args); } - - /** - * Defines the global mocks via an array of function_name=>reponse - * - * @param array $global_mocks key as function name, value as response - * @return void - */ - private function setGlobalMocks(array $global_mocks): void { - try { - foreach ($global_mocks as $function_name => $return) { - GlobalMock::mock($function_name, $return); - } - } catch (Exception $e) { - $this->fail('Mocking failed: ' . $e->getMessage()); - } - } }