Skip to content

Conditions #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,58 @@
If you're here, you would like to contribute to this repository and you're really welcome!


## Coding standard
## Bug reports

This repository follows the [PSR-2 standard](http://www.php-fig.org/psr/psr-2/) and so, if you want to contribute,
you must follow these rules.
If you find a bug or a documentation issue, please report it or even better: fix it :). If you report it,
please be as precise as possible. Here is a little list of required information:

- Precise description of the bug
- Details of your environment (for example: OS, PHP version, installed extensions)
- Backtrace which might help identifing the bug

## Feature request

If you think a feature is missing, please report it or even better implement it :). If you report it, describe the more
precisely what you would like to see implemented and we will discuss what is the best approach for it. If you can do
some search before submitting it and link the resources to your description, you're awesome! It will allow me to more
easily understood/implement it.
## Security issues

If you discover any security related issues, please contact us at [[email protected]](mailto:[email protected]) instead of submitting an issue on Github.

## Bug report

If you think you have detected a bug or a doc issue, please report it or even better fix it :). If you report it,
please be the more precise possible. Here a little list of required informations:
## Feature requests

* Precise description of the bug.
If you think a feature is missing, please report it or even better: implement it :). If you report it, describe the more
precisely what you would like to see implemented and we will discuss what is the best approach for it. If you can do
some search before submitting it and link the resources to your description, you're awesome! It will allow us to more
easily understood/implement it.


## Bug fix
## Sending a Pull Request

If you're here, you are going to fix a bug and you're the best! To do it, first fork the repository, clone it and
create a new branch with the following commands:
If you're here, you are going to fix a bug or implement a feature and you're the best! To do it, first fork the repository, clone it and create a new branch with the following commands:

``` bash
$ git clone [email protected]:your-name/repo-name.git
$ git checkout -b bug-fix-description
$ git checkout -b feature-or-bug-fix-description
```

Then, install the dependencies through [Composer](https://getcomposer.org/):
Then install the dependencies through [Composer](https://getcomposer.org/):

``` bash
$ composer install
```

When you're on the new branch with the dependencies, code as much as you want and when the fix is ready, don't commit
it immediately. Before, you will need to add tests and update the doc. For the tests, everything is tested with
[PHPUnit](http://phpunit.de/) and the doc is in the markdown format under the `doc` directory.
Write code and tests. When you are ready, find the testing command in the [README](README.md) and execute it. (This is usually [PHPUnit](http://phpunit.de/) or [PHPSpec](http://phpspec.net/))

To run the tests, use the following command:
When you are ready with the code, tested it and documented it, you can commit and push it with the following commands:

``` bash
$ vendor/bin/phpunit
$ git commit -m "Feature or bug fix description"
$ git push origin feature-or-bug-fix-description
```

When you have fixed the bug, tested it and documented it, you can commit and push it with the following commands:
**Note:** Please write your commit messages in the imperative and follow the [guidelines](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) for clear and concise messages.

``` bash
$ git commit -m "Bug fix description"
$ git push origin bug-fix-description
```
Then [create a pull request](https://help.github.com/articles/creating-a-pull-request/) on GitHub.

If you have reworked you patch, please squash all your commits in a single one with the following commands (here, we
will assume you would like to squash 3 commits in a single one):
Please make sure that each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting with the following commands (here, we assume you would like to squash 3 commits in a single one):

``` bash
$ git rebase -i HEAD~3
Expand All @@ -70,5 +65,16 @@ If your branch conflicts with the master branch, you will need to rebase and rep
``` bash
$ git remote add upstream [email protected]:php-http/repo-name.git
$ git pull --rebase upstream master
$ git push origin bug-fix-description -f
$ git push -f origin feature-or-bug-fix-description
```


## Coding standard

This repository follows the [PSR-2 standard](http://www.php-fig.org/psr/psr-2/) and so, if you want to contribute,
you must follow these rules.


## Semver

We are trying to follow [semver](http://semver.org/). When you are making BC breaking changes, please let us know why you think it is important. In this case, your patch can only be included in the next major version.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[![Build Status](https://img.shields.io/travis/php-http/discovery.svg?style=flat-square)](https://travis-ci.org/php-http/discovery)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/php-http/discovery.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/discovery)
[![Quality Score](https://img.shields.io/scrutinizer/g/php-http/discovery.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/discovery)
[![HHVM Status](https://img.shields.io/hhvm/php-http/discovery.svg?style=flat-square)](http://hhvm.h4cc.de/package/php-http/discovery)
[![Total Downloads](https://img.shields.io/packagist/dt/php-http/discovery.svg?style=flat-square)](https://packagist.org/packages/php-http/discovery)

**Finds installed adapters and message factories.**
Expand Down Expand Up @@ -70,6 +69,17 @@ $factory = UriFactoryDiscovery::find();
```


### Custom conditions

You can use various types of conditions to check for a class:

- string (checked for class existence)
- callable
- boolean

Any further types are treated as invalid and evaulated to false boolean value.


## Testing

``` bash
Expand Down
3 changes: 2 additions & 1 deletion phpspec.yml.ci
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ suites:
discovery_suite:
namespace: Http\Discovery
psr4_prefix: Http\Discovery
bootstrap: spec/bootstrap.php
formatter.name: pretty
extensions:
- PhpSpec\Extension\CodeCoverageExtension
formatter.name: pretty
code_coverage:
format: clover
output: build/coverage.xml
1 change: 1 addition & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ suites:
discovery_suite:
namespace: Http\Discovery
psr4_prefix: Http\Discovery
bootstrap: spec/bootstrap.php
formatter.name: pretty
136 changes: 136 additions & 0 deletions spec/ClassDiscoverySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace spec\Http\Discovery;

use Http\Discovery\ClassDiscovery;
use PhpSpec\ObjectBehavior;

class ClassDiscoverySpec extends ObjectBehavior
{
function let()
{
$this->beAnInstanceOf('spec\Http\Discovery\DiscoveryStub');
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
}

function it_registers_a_class()
{
$this->reset();

$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
}

function it_registers_a_class_with_a_condition()
{
$this->reset();

$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', 'spec\Http\Discovery\TestClass');

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
}

function it_registers_a_class_with_a_callable_condition()
{
$this->reset();

$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', function() { return true; });

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
}

function it_registers_a_class_with_a_boolean_condition()
{
$this->reset();

$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
}

function it_registers_a_class_with_an_invalid_condition()
{
$this->reset();

$this->register('class1', 'spec\Http\Discovery\ClassToFind', new \stdClass);
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
}

function it_resets_cache_when_a_class_is_registered()
{
$this->reset();

$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');

$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
}

function it_caches_a_found_class()
{
$this->reset();

$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');

$this->registerWithoutCacheReset('class1', 'spec\Http\Discovery\AnotherClassToFind');

$this->find()->shouldhaveType('spec\Http\Discovery\ClassToFind');
}

function it_throws_an_exception_when_no_class_is_found()
{
$this->reset();

$this->register('class1', 'invalid');

$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
}
}

class DiscoveryStub extends ClassDiscovery
{
protected static $cache;

/**
* @var array
*/
protected static $classes;

/**
* Reset classes
*/
public function reset()
{
static::$cache = null;

static::$classes = [
'class1' => [
'class' => 'spec\Http\Discovery\ClassToFind',
'condition' => 'spec\Http\Discovery\ClassToFind'
],
];
}

public function registerWithoutCacheReset($name, $class, $condition = null)
{
static::$classes[$name] = [
'class' => $class,
'condition' => $condition ?: $class,
];
}
}

class ClassToFind {}
class AnotherClassToFind {}
class TestClass {}
60 changes: 4 additions & 56 deletions spec/HttpAdapterDiscoverySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace spec\Http\Discovery;

use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use PhpSpec\ObjectBehavior;

class HttpAdapterDiscoverySpec extends ObjectBehavior
Expand All @@ -13,63 +11,13 @@ function it_is_initializable()
$this->shouldHaveType('Http\Discovery\HttpAdapterDiscovery');
}

function it_registers_a_factory()
function it_is_a_class_discovery()
{
$this->reset();

$this->register('guzzle6', 'spec\Http\Discovery\AnotherGuzzle6HttpAdapter');

$this->find()->shouldHaveType('spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
}

function it_resets_cache_when_a_factory_is_registered()
function it_finds_an_http_adapter()
{
$this->reset();

$firstMatch = $this->find();

$this->register('guzzle6', 'spec\Http\Discovery\AnotherGuzzle6HttpAdapter');

$this->find()->shouldNotBe($firstMatch);
}

function it_caches_a_found_adapter()
{
$this->reset();

$firstMatch = $this->find()->shouldHaveType('spec\Http\Discovery\Guzzle6HttpAdapter');

$this->find()->shouldReturn($firstMatch);
}

function it_finds_guzzle6_then_guzzle5_by_default()
{
$this->reset();

$this->find()->shouldHaveType('spec\Http\Discovery\Guzzle6HttpAdapter');

$this->register('guzzle6', 'invalid', '');

$this->find()->shouldHaveType('spec\Http\Discovery\Guzzle5HttpAdapter');
}

function it_throws_an_exception_when_no_adapter_is_found()
{
$this->reset();

$this->register('guzzle6', 'invalid', '');
$this->register('guzzle5', 'invalid', '');

$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
}

function reset()
{
$this->register('guzzle5', 'spec\Http\Discovery\Guzzle5HttpAdapter');
$this->register('guzzle6', 'spec\Http\Discovery\Guzzle6HttpAdapter');
$this->find()->shouldHaveType('Http\Adapter\HttpAdapter');
}
}

class Guzzle5HttpAdapter {}
class Guzzle6HttpAdapter {}
class AnotherGuzzle6HttpAdapter {}
24 changes: 24 additions & 0 deletions spec/MessageFactory/DiactorosFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ function it_creates_a_request()
$this->createRequest('GET', '/')->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_request_with_string_body()
{
$this->createRequest('POST', '/', '1.1', [], 'body')->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_request_with_empty_body()
{
$this->createRequest('POST', '/', '1.1', [], null)->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_request_with_stream_body(StreamInterface $stream)
{
$stream->rewind()->shouldBeCalled();

$this->createRequest('POST', '/', '1.1', [], $stream)->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_request_with_resource_body()
{
$resource = tmpfile();

$this->createRequest('POST', '/', '1.1', [], $resource)->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_response()
{
$this->createResponse()->shouldHaveType('Psr\Http\Message\ResponseInterface');
Expand Down
Loading