PHP code example of coduo / phpspec-prepare-extension

1. Go to this page and download the library: Download coduo/phpspec-prepare-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

coduo / phpspec-prepare-extension example snippets




namespace spec\Coduo\Packagist;

use PhpSpec\ObjectBehavior;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\Message\Response;
use Prophecy\Argument;

class ClientSpec extends ObjectBehavior
{
    function let(ClientInterface $client)
    {
        $this->beConstructedWith($client);
    }

    /**
     *  @before prepareClientForSearch
     */
    function it_return_list_of_packages(Response $response)
    {
        $this->search('coduo')->shouldReturn(array(
            'coduo/php-to-string',
            'coduo/php-matcher'
        ));
    }

    /**
     *  @before prepareClientForSearch
     */
    function it_return_list_of_packages_when_filter_is_not_a_string(Response $response)
    {
        $this->search('coduo', new \DateTime())->shouldReturn(array(
            'coduo/php-to-string',
            'coduo/php-matcher'
        ));
    }

    /**
     *  @before prepareClientForSearch
     */
    function it_return_list_of_filtered_packages(Response $response)
    {
        $this->search('coduo', 'string')->shouldReturn(array(
            'coduo/php-to-string',
        ));
    }

    function prepareClientForSearch(ClientInterface $client, Response $response)
    {
        $client->get(
            'https://api.com/search.json',
            Argument::allOf(
                Argument::type('array'),
                Argument::withKey('q')
            )
        )->willReturn($response);

        $response->getBody(true)->willReturn(json_encode(array(
            'coduo/php-to-string',
            'coduo/php-matcher'
        )));
    }
}



namespace Coduo\Packagist;

use Guzzle\Http\ClientInterface;

class Client
{
    private $client;

    public function __construct(ClientInterface $client)
    {
        $this->client = $client;
    }

    public function search($package, $filter = null)
    {
        $response = $this->client->get(
            'https://api.com/search.json',
            array('q' => $package)
        );

        $packages = json_decode($response->getBody(true), true);

        if (isset($filter) && is_string($filter)) {
            foreach ($packages as $index => $package) {
                if (false === strpos($package, $filter)) {
                    unset($packages[$index]);
                }
            }
        }

        return $packages;
    }
}