Skip to content

Commit 5c35c32

Browse files
authored
Merge pull request #41 from mlopes/feature/Skip-3.2-matchers-on-older-versions-of-phpspec
Skip phpspec 3.2 matchers if they don't exist
2 parents 2648862 + cfc69f8 commit 5c35c32

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ phpspec2-expect
66
Installation
77
============
88

9-
Add `"bossa/phpspec2-expect": "*"` to `composer.json`
9+
Install it using the `composer require` command:
10+
11+
```bash
12+
composer require --dev bossa/phpspec2-expect
13+
```
14+
15+
Alternativelly you can add it to the `composer.json` file
1016

1117
```json
1218
{
1319
"require-dev": {
14-
"phpspec/phpspec": "dev-master",
15-
"bossa/phpspec2-expect": "*"
20+
"bossa/phpspec2-expect": "^2.2"
1621
},
1722
"config": {
1823
"bin-dir": "bin"

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
},
1818
"require": {
19-
"phpspec/phpspec": "~3.2 <3.3.0"
19+
"phpspec/phpspec": "~3.2 <3.4.0"
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "^5.4"

expect.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ function expect($sus)
7373
$matchers->add(new StringEndMatcher($presenter));
7474
$matchers->add(new StringRegexMatcher($presenter));
7575
$matchers->add(new StringContainMatcher($presenter));
76-
$matchers->add(new TriggerMatcher($unwrapper));
77-
$matchers->add(new IterateAsMatcher($presenter));
78-
$matchers->add(new ApproximatelyMatcher($presenter));
76+
if (class_exists('TriggerMatcher')) {
77+
$matchers->add(new TriggerMatcher($unwrapper));
78+
}
79+
if (class_exists('IterateAsMatcher')) {
80+
$matchers->add(new IterateAsMatcher($presenter));
81+
}
82+
if (class_exists('ApproximatelyMatcher')) {
83+
$matchers->add(new ApproximatelyMatcher($presenter));
84+
}
7985

8086
$trace = debug_backtrace();
8187
if (isset($trace[1]['object'])) {

tests/ExpectTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ function correctExpectations()
4444
[ function () { expect('foo bar')->toEndWith('bar'); } ],
4545
[ function () { expect('foo bar')->toMatch('/bar/'); } ],
4646
[ function () { expect((new Foo()))->toThrow('InvalidArgumentException')->duringThrowException(); } ],
47-
[ function () { expect((new Foo()))->toTrigger(E_USER_DEPRECATED)->duringTriggerError(); } ],
48-
[ function () { expect(1.444447777)->toBeApproximately(1.444447777, 1.0e-9); } ],
49-
[ function () { expect((new Foo())->getIterator())->toIterateAs(new \ArrayIterator(['Foo', 'Bar'])); } ],
47+
[ function () { method_exists(expect(''), 'toTrigger') && expect((new Foo()))->toTrigger(E_USER_DEPRECATED)->duringTriggerError(); } ],
48+
[ function () { method_exists(expect(''), 'toBeApproximately') && expect(1.444447777)->toBeApproximately(1.444447777, 1.0e-9); } ],
49+
[ function () { method_exists(expect(''), 'toIterateAs') && expect((new Foo())->getIterator())->toIterateAs(new \ArrayIterator(['Foo', 'Bar'])); } ],
5050
];
5151
}
5252

@@ -55,7 +55,7 @@ function correctExpectations()
5555
*/
5656
function incorrectExpectations()
5757
{
58-
return [
58+
$incorrectExpectations = [
5959
[ function () { expect(6)->toBe(5); } ],
6060
[ function () { expect(6)->toBeLike('5'); } ],
6161
[ function () { expect((new Foo()))->toHaveType('Bar'); } ],
@@ -70,9 +70,16 @@ function incorrectExpectations()
7070
[ function () { expect('foo bar')->toStartWith('baz'); } ],
7171
[ function () { expect('foo bar')->toEndWith('baz'); } ],
7272
[ function () { expect('foo bar')->toMatch('/baz/'); } ],
73-
[ function () { expect((new Foo()))->toThrow('AnotherException')->duringThrowException(); } ],
74-
[ function () { expect(1.444447777)->toBeApproximately(1.444447778, 1.0e-9); } ],
75-
[ function () { expect((new Foo())->getIterator())->toIterateAs(new \ArrayIterator(['Bar', 'Foo'])); } ]
73+
[ function () { expect((new Foo()))->toThrow('AnotherException')->duringThrowException(); } ]
7674
];
75+
76+
if (method_exists(expect(''), 'toBeApproximately')) {
77+
$incorrectExpectations[] = [function () { expect(1.444447777)->toBeApproximately(1.444447778, 1.0e-9); }];
78+
}
79+
if (method_exists(expect(''), 'toIterateAs')) {
80+
$incorrectExpectations = [ function () { expect((new Foo())->getIterator())->toIterateAs(new \ArrayIterator(['Bar', 'Foo'])); } ];
81+
}
82+
83+
return $incorrectExpectations;
7784
}
7885
}

0 commit comments

Comments
 (0)