<commit_before><?php

/*
 * This file is part of the Active Collab Utils project.
 *
 * (c) A51 doo <<EMAIL>>. All rights reserved.
 */

declare(strict_types=1);

namespace ActiveCollab\Utils\Test\HttpClient;

use ActiveCollab\HttpClient\HttpClient;
use ActiveCollab\HttpClient\RequestMiddleware\RequestMiddlewareInterface;
use ActiveCollab\Utils\Test\Base\TestCase;
use Laminas\Diactoros\RequestFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;

class HttpClientTest extends TestCase
{
    /**
     * @dataProvider provideDataForMiddlewareInvokation
     * @param string $method
     * @param string $url
     */
    public function testWillInvokeMiddlewares(string $method, string $url): void
    {
        $psrHttpClient = $this->createMock(ClientInterface::class);
        $psrHttpClient
            ->expects($this->once())
            ->method('sendRequest');

        $requestMock = $this->createMock(RequestInterface::class);

        $requestFactoryMock = $this->createMock(RequestFactoryInterface::class);
        $requestFactoryMock
            ->expects($this->once())
            ->method('createRequest')
            ->willReturn($requestMock);

        $middleware1 = $this->createMock(RequestMiddlewareInterface::class);
        $middleware1
            ->expects($this->once())
            ->method('alter')
            ->willReturn($requestMock);

        $middleware2 = $this->createMock(RequestMiddlewareInterface::class);
        $middleware2
            ->expects($this->once())
            ->method('alter')
            ->willReturn($requestMock);

        $httpClient = new HttpClient($psrHttpClient, $requestFactoryMock);
        call_user_func([$httpClient, $method], $url, $middleware1, $middleware2);
    }

    public function provideDataForMiddlewareInvokation(): array
    {
        return [
            ['get', 'https://activecollab.com'],
            ['post', 'https://activecollab.com'],
            ['put', 'https://activecollab.com'],
            ['patch', 'https://activecollab.com'],
            ['delete', 'https://activecollab.com'],
        ];
    }
}<commit_msg>Test if GET request is properly forwarded to HTTP client<commit_after><?php

/*
 * This file is part of the Active Collab Utils project.
 *
 * (c) A51 doo <<EMAIL>>. All rights reserved.
 */

declare(strict_types=1);

namespace ActiveCollab\Utils\Test\HttpClient;

use ActiveCollab\HttpClient\HttpClient;
use ActiveCollab\HttpClient\RequestMiddleware\RequestMiddlewareInterface;
use ActiveCollab\Utils\Test\Base\TestCase;
use Laminas\Diactoros\RequestFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class HttpClientTest extends TestCase
{
    /**
     * @dataProvider provideDataForMiddlewareInvokation
     * @param string $method
     * @param string $url
     */
    public function testWillInvokeMiddlewares(string $method, string $url): void
    {
        $psrHttpClient = $this->createMock(ClientInterface::class);
        $psrHttpClient
            ->expects($this->once())
            ->method('sendRequest');

        $requestMock = $this->createMock(RequestInterface::class);

        $requestFactoryMock = $this->createMock(RequestFactoryInterface::class);
        $requestFactoryMock
            ->expects($this->once())
            ->method('createRequest')
            ->willReturn($requestMock);

        $middleware1 = $this->createMock(RequestMiddlewareInterface::class);
        $middleware1
            ->expects($this->once())
            ->method('alter')
            ->willReturn($requestMock);

        $middleware2 = $this->createMock(RequestMiddlewareInterface::class);
        $middleware2
            ->expects($this->once())
            ->method('alter')
            ->willReturn($requestMock);

        $httpClient = new HttpClient($psrHttpClient, $requestFactoryMock);
        call_user_func([$httpClient, $method], $url, $middleware1, $middleware2);
    }

    public function provideDataForMiddlewareInvokation(): array
    {
        return [
            ['get', 'https://activecollab.com'],
            ['post', 'https://activecollab.com'],
            ['put', 'https://activecollab.com'],
            ['patch', 'https://activecollab.com'],
            ['delete', 'https://activecollab.com'],
        ];
    }

    public function testGetWillSendRequest(): void
    {
        $psrHttpClient = $this->getTestHttpClient();

        (new HttpClient($psrHttpClient, new RequestFactory()))->get('https://activecollab.com?test=1');

        $capturedRequest = $psrHttpClient->getCapturedRequest();

        $this->assertInstanceOf(RequestInterface::class, $capturedRequest);
        $this->assertSame('GET', $capturedRequest->getMethod());
        $this->assertSame('https://activecollab.com?test=1', (string) $capturedRequest->getUri());
    }

    private function getTestHttpClient(ResponseInterface $response = null): ClientInterface
    {
        if (empty($response)) {
            $response = $this->createMock(ResponseInterface::class);
        }

        return new class ($response) implements ClientInterface
        {
            private ResponseInterface $response;
            private ?RequestInterface $capturedRequest = null;

            public function __construct(ResponseInterface $response)
            {
                $this->response = $response;
            }

            public function sendRequest(RequestInterface $request): ResponseInterface
            {
                $this->capturedRequest = $request;

                return $this->response;
            }

            public function getCapturedRequest(): ?RequestInterface
            {
                return $this->capturedRequest;
            }
        };
    }
}
{'fraction_non_alphanumeric': 0.13056027164685907, 'fraction_numerical': 0.003395585738539898, 'mean_word_length': 2.8055555555555554, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 60, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2124182', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1568, 'n_words': 280}