Skip to content

Commit 0755b38

Browse files
committed
Merge pull request #6 from php-http/conditions
Conditions
2 parents 19fcb6d + 0c90a02 commit 0755b38

12 files changed

+261
-139
lines changed

CONTRIBUTING.md

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,58 @@
33
If you're here, you would like to contribute to this repository and you're really welcome!
44

55

6-
## Coding standard
6+
## Bug reports
77

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

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

12-
## Feature request
1315

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

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

20-
## Bug report
2120

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

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

2728

28-
## Bug fix
29+
## Sending a Pull Request
2930

30-
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
31-
create a new branch with the following commands:
31+
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:
3232

3333
``` bash
3434
$ git clone [email protected]:your-name/repo-name.git
35-
$ git checkout -b bug-fix-description
35+
$ git checkout -b feature-or-bug-fix-description
3636
```
3737

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

4040
``` bash
4141
$ composer install
4242
```
4343

44-
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
45-
it immediately. Before, you will need to add tests and update the doc. For the tests, everything is tested with
46-
[PHPUnit](http://phpunit.de/) and the doc is in the markdown format under the `doc` directory.
44+
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/))
4745

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

5048
``` bash
51-
$ vendor/bin/phpunit
49+
$ git commit -m "Feature or bug fix description"
50+
$ git push origin feature-or-bug-fix-description
5251
```
5352

54-
When you have fixed the bug, tested it and documented it, you can commit and push it with the following commands:
53+
**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.
5554

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

61-
If you have reworked you patch, please squash all your commits in a single one with the following commands (here, we
62-
will assume you would like to squash 3 commits in a single one):
57+
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):
6358

6459
``` bash
6560
$ git rebase -i HEAD~3
@@ -70,5 +65,16 @@ If your branch conflicts with the master branch, you will need to rebase and rep
7065
``` bash
7166
$ git remote add upstream [email protected]:php-http/repo-name.git
7267
$ git pull --rebase upstream master
73-
$ git push origin bug-fix-description -f
68+
$ git push -f origin feature-or-bug-fix-description
7469
```
70+
71+
72+
## Coding standard
73+
74+
This repository follows the [PSR-2 standard](http://www.php-fig.org/psr/psr-2/) and so, if you want to contribute,
75+
you must follow these rules.
76+
77+
78+
## Semver
79+
80+
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.

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[![Build Status](https://img.shields.io/travis/php-http/discovery.svg?style=flat-square)](https://travis-ci.org/php-http/discovery)
66
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/php-http/discovery.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/discovery)
77
[![Quality Score](https://img.shields.io/scrutinizer/g/php-http/discovery.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/discovery)
8-
[![HHVM Status](https://img.shields.io/hhvm/php-http/discovery.svg?style=flat-square)](http://hhvm.h4cc.de/package/php-http/discovery)
98
[![Total Downloads](https://img.shields.io/packagist/dt/php-http/discovery.svg?style=flat-square)](https://packagist.org/packages/php-http/discovery)
109

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

7271

72+
### Custom conditions
73+
74+
You can use various types of conditions to check for a class:
75+
76+
- string (checked for class existence)
77+
- callable
78+
- boolean
79+
80+
Any further types are treated as invalid and evaulated to false boolean value.
81+
82+
7383
## Testing
7484

7585
``` bash

phpspec.yml.ci

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ suites:
22
discovery_suite:
33
namespace: Http\Discovery
44
psr4_prefix: Http\Discovery
5+
bootstrap: spec/bootstrap.php
6+
formatter.name: pretty
57
extensions:
68
- PhpSpec\Extension\CodeCoverageExtension
7-
formatter.name: pretty
89
code_coverage:
910
format: clover
1011
output: build/coverage.xml

phpspec.yml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ suites:
22
discovery_suite:
33
namespace: Http\Discovery
44
psr4_prefix: Http\Discovery
5+
bootstrap: spec/bootstrap.php
56
formatter.name: pretty

spec/ClassDiscoverySpec.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery;
4+
5+
use Http\Discovery\ClassDiscovery;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class ClassDiscoverySpec extends ObjectBehavior
9+
{
10+
function let()
11+
{
12+
$this->beAnInstanceOf('spec\Http\Discovery\DiscoveryStub');
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
18+
}
19+
20+
function it_registers_a_class()
21+
{
22+
$this->reset();
23+
24+
$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');
25+
26+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
27+
}
28+
29+
function it_registers_a_class_with_a_condition()
30+
{
31+
$this->reset();
32+
33+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
34+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', 'spec\Http\Discovery\TestClass');
35+
36+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
37+
}
38+
39+
function it_registers_a_class_with_a_callable_condition()
40+
{
41+
$this->reset();
42+
43+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
44+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', function() { return true; });
45+
46+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
47+
}
48+
49+
function it_registers_a_class_with_a_boolean_condition()
50+
{
51+
$this->reset();
52+
53+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
54+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);
55+
56+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
57+
}
58+
59+
function it_registers_a_class_with_an_invalid_condition()
60+
{
61+
$this->reset();
62+
63+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', new \stdClass);
64+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);
65+
66+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
67+
}
68+
69+
function it_resets_cache_when_a_class_is_registered()
70+
{
71+
$this->reset();
72+
73+
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
74+
75+
$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');
76+
77+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
78+
}
79+
80+
function it_caches_a_found_class()
81+
{
82+
$this->reset();
83+
84+
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
85+
86+
$this->registerWithoutCacheReset('class1', 'spec\Http\Discovery\AnotherClassToFind');
87+
88+
$this->find()->shouldhaveType('spec\Http\Discovery\ClassToFind');
89+
}
90+
91+
function it_throws_an_exception_when_no_class_is_found()
92+
{
93+
$this->reset();
94+
95+
$this->register('class1', 'invalid');
96+
97+
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
98+
}
99+
}
100+
101+
class DiscoveryStub extends ClassDiscovery
102+
{
103+
protected static $cache;
104+
105+
/**
106+
* @var array
107+
*/
108+
protected static $classes;
109+
110+
/**
111+
* Reset classes
112+
*/
113+
public function reset()
114+
{
115+
static::$cache = null;
116+
117+
static::$classes = [
118+
'class1' => [
119+
'class' => 'spec\Http\Discovery\ClassToFind',
120+
'condition' => 'spec\Http\Discovery\ClassToFind'
121+
],
122+
];
123+
}
124+
125+
public function registerWithoutCacheReset($name, $class, $condition = null)
126+
{
127+
static::$classes[$name] = [
128+
'class' => $class,
129+
'condition' => $condition ?: $class,
130+
];
131+
}
132+
}
133+
134+
class ClassToFind {}
135+
class AnotherClassToFind {}
136+
class TestClass {}

spec/HttpAdapterDiscoverySpec.php

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace spec\Http\Discovery;
44

5-
use Http\Discovery\MessageFactoryDiscovery;
6-
use Http\Discovery\UriFactoryDiscovery;
75
use PhpSpec\ObjectBehavior;
86

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

16-
function it_registers_a_factory()
14+
function it_is_a_class_discovery()
1715
{
18-
$this->reset();
19-
20-
$this->register('guzzle6', 'spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
21-
22-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
16+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
2317
}
2418

25-
function it_resets_cache_when_a_factory_is_registered()
19+
function it_finds_an_http_adapter()
2620
{
27-
$this->reset();
28-
29-
$firstMatch = $this->find();
30-
31-
$this->register('guzzle6', 'spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
32-
33-
$this->find()->shouldNotBe($firstMatch);
34-
}
35-
36-
function it_caches_a_found_adapter()
37-
{
38-
$this->reset();
39-
40-
$firstMatch = $this->find()->shouldHaveType('spec\Http\Discovery\Guzzle6HttpAdapter');
41-
42-
$this->find()->shouldReturn($firstMatch);
43-
}
44-
45-
function it_finds_guzzle6_then_guzzle5_by_default()
46-
{
47-
$this->reset();
48-
49-
$this->find()->shouldHaveType('spec\Http\Discovery\Guzzle6HttpAdapter');
50-
51-
$this->register('guzzle6', 'invalid', '');
52-
53-
$this->find()->shouldHaveType('spec\Http\Discovery\Guzzle5HttpAdapter');
54-
}
55-
56-
function it_throws_an_exception_when_no_adapter_is_found()
57-
{
58-
$this->reset();
59-
60-
$this->register('guzzle6', 'invalid', '');
61-
$this->register('guzzle5', 'invalid', '');
62-
63-
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
64-
}
65-
66-
function reset()
67-
{
68-
$this->register('guzzle5', 'spec\Http\Discovery\Guzzle5HttpAdapter');
69-
$this->register('guzzle6', 'spec\Http\Discovery\Guzzle6HttpAdapter');
21+
$this->find()->shouldHaveType('Http\Adapter\HttpAdapter');
7022
}
7123
}
72-
73-
class Guzzle5HttpAdapter {}
74-
class Guzzle6HttpAdapter {}
75-
class AnotherGuzzle6HttpAdapter {}

spec/MessageFactory/DiactorosFactorySpec.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ function it_creates_a_request()
3030
$this->createRequest('GET', '/')->shouldHaveType('Psr\Http\Message\RequestInterface');
3131
}
3232

33+
function it_creates_a_request_with_string_body()
34+
{
35+
$this->createRequest('POST', '/', '1.1', [], 'body')->shouldHaveType('Psr\Http\Message\RequestInterface');
36+
}
37+
38+
function it_creates_a_request_with_empty_body()
39+
{
40+
$this->createRequest('POST', '/', '1.1', [], null)->shouldHaveType('Psr\Http\Message\RequestInterface');
41+
}
42+
43+
function it_creates_a_request_with_stream_body(StreamInterface $stream)
44+
{
45+
$stream->rewind()->shouldBeCalled();
46+
47+
$this->createRequest('POST', '/', '1.1', [], $stream)->shouldHaveType('Psr\Http\Message\RequestInterface');
48+
}
49+
50+
function it_creates_a_request_with_resource_body()
51+
{
52+
$resource = tmpfile();
53+
54+
$this->createRequest('POST', '/', '1.1', [], $resource)->shouldHaveType('Psr\Http\Message\RequestInterface');
55+
}
56+
3357
function it_creates_a_response()
3458
{
3559
$this->createResponse()->shouldHaveType('Psr\Http\Message\ResponseInterface');

0 commit comments

Comments
 (0)