<commit_before><?php

declare(strict_types=1);

namespace spec\Packagist\Api;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use Packagist\Api\Client;
use Packagist\Api\Result\Factory;
use PhpSpec\ObjectBehavior;

class ClientSpec extends ObjectBehavior
{
    public function let(HttpClient $client, Factory $factory)
    {
        $this->beConstructedWith($client, $factory);
    }

    public function it_is_initializable(): void
    {
        $this->shouldHaveType(Client::class);
    }

    public function it_search_for_packages(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/search.json?q=sylius')
			->shouldBeCalled()
			->willReturn($response);
        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn([]);

        $this->search('sylius');
    }

    public function it_search_for_packages_with_limit(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/search.json?q=sylius')->shouldBeCalled()->willReturn($response);
        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn(array());

        $this->search('sylius', [], 2);
    }

    public function it_searches_for_packages_with_filters(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/search.json?tag=storage&q=sylius')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn([]);

        $this->search('sylius', ['tag' => 'storage']);
    }

    public function it_gets_popular_packages(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/popular.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/explore/popular.json?page=1')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))
			->shouldBeCalled()
			->willReturn(array_pad([], 5, null));

        $this->popular(2)->shouldHaveCount(2);
    }

    public function it_gets_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/get.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/packages/sylius/sylius.json')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled();

        $this->get('sylius/sylius');
    }

    public function it_gets_composer_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/get_composer.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/p/sylius/sylius.json')
            ->shouldBeCalled()
            ->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled();

        $this->getComposer('sylius/sylius');
    }

    public function it_gets_composer_lite_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/get_composer_lite.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/p/sylius/sylius.json')
            ->shouldBeCalled()
            ->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled();

        $this->getComposerLite('sylius/sylius');
    }

    public function it_lists_all_package_names(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/all.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/packages/list.json')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled();

        $this->all();
    }

    public function it_filters_package_names_by_type(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/all.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/packages/list.json?type=library')<commit_msg>Fix the specs to pass<commit_after><?php

declare(strict_types=1);

namespace spec\Packagist\Api;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use Packagist\Api\Client;
use Packagist\Api\Result\Factory;
use PhpSpec\ObjectBehavior;

class ClientSpec extends ObjectBehavior
{
    public function let(HttpClient $client, Factory $factory)
    {
        $this->beConstructedWith($client, $factory);
    }

    public function it_is_initializable(): void
    {
        $this->shouldHaveType(Client::class);
    }

    public function it_search_for_packages(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/search.json?q=sylius')
			->shouldBeCalled()
			->willReturn($response);
        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn([]);

        $this->search('sylius');
    }

    public function it_search_for_packages_with_limit(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/search.json?q=sylius')->shouldBeCalled()->willReturn($response);
        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn(array());

        $this->search('sylius', [], 2);
    }

    public function it_searches_for_packages_with_filters(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/search.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/search.json?tag=storage&q=sylius')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn([]);

        $this->search('sylius', ['tag' => 'storage']);
    }

    public function it_gets_popular_packages(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/popular.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/explore/popular.json?page=1')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))
			->shouldBeCalled()
			->willReturn(array_pad([], 5, null));

        $this->popular(2)->shouldHaveCount(2);
    }

    public function it_gets_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/get.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/packages/sylius/sylius.json')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled();

        $this->get('sylius/sylius');
    }

    public function it_gets_composer_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/get_composer.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/p2/sylius/sylius~dev.json')
            ->shouldBeCalled()
            ->willReturn($response);

        $packages = [
            '1.0.0' => ['name' => 'sylius/sylius', 'version' => '1.0.0']
        ];

        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn($packages);

        $this->getComposer('sylius/sylius')->shouldBe($packages);
    }

    public function it_gets_composer_lite_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/get_composer_lite.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/p2/sylius/sylius.json')
            ->shouldBeCalled()
            ->willReturn($response);

        $packages = [
            '1.0.0' => ['name' => 'sylius/sylius', 'version' => '1.0.0']
        ];

        $factory->create(json_decode($data, true))->shouldBeCalled()->willReturn($packages);

        $this->getComposerLite('sylius/sylius')->shouldBe($packages);
    }

    public function it_lists_all_package_names(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/all.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/packages/list.json')
			->shouldBeCalled()
			->willReturn($response);

        $factory->create(json_decode($data, true))->shouldBeCalled();

        $this->all();
    }

    public function it_filters_package_names_by_type(HttpClient $client, Factory $factory, Response $response, Stream $body): void
    {
        $data = file_get_contents('spec/Packagist/Api/Fixture/all.json');
        $response->getBody()->shouldBeCalled()->willReturn($body);
        $body->getContents()->shouldBeCalled()->willReturn($data);

        $client->request('GET', 'https://packagist.org/packages/list.json?type=library')
{'fraction_non_alphanumeric': 0.17310549777117384, 'fraction_numerical': 0.0024764735017335313, 'mean_word_length': 3.9590667212443718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 235, 'https://': 18, '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': '648998', 'n_tokens_mistral': 3746, 'n_tokens_neox': 3676, 'n_words': 481}