2017 © Pedro PelĆ”ez
 

library phpspec-prepare-extension

image

coduo/phpspec-prepare-extension

  • Monday, October 26, 2015
  • by norzechowicz
  • Repository
  • 3 Watchers
  • 14 Stars
  • 7,403 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PhpSpec prepare extension

Build Status, (*1)

Prepare examples before phpspec execute them., (*2)

Installation

require: {
   "coduo/phpspec-prepare-extension": "^1.0"
}

Usage

Enable exntesion in phpspec.yml file, (*3)

extensions:
  - Coduo\PhpSpec\PrepareExtension

Write a spec:, (*4)

<?php

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'
        )));
    }
}

Write class for spec:, (*5)

<?php

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;
    }
}

Run php spec, (*6)

$ console bin/phpspec run -f pretty

It should pass!, (*7)

The Versions

26/10 2015

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

by Norbert Orzechowicz

extension phpspec coduo example prepare

24/10 2015

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Norbert Orzechowicz

extension phpspec coduo example prepare