Skip to content

Commit eeb6027

Browse files
author
dstevenson
authored
Merge pull request #2 from mister-spock/master
bugfix() Fix how "Some::filterIsOneOf()" traverses through received parameters array
2 parents 23f637c + 90c379e commit eeb6027

8 files changed

Lines changed: 108 additions & 69 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea/
12
vendor/
23
phpunit.xml
34
composer.lock

‎.travis.yml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
language: php
22
php:
3-
- 5.3
4-
- 5.4
5-
- 5.5
6-
- hhvm
3+
- 5.6
4+
- 7.0
75

86
before_script:
97
- wget http://getcomposer.org/composer.phar

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PHP Option Type [![Build Status](https://secure.travis-ci.org/schmittjoh/php-option.png)](http://travis-ci.org/schmittjoh/php-option) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/schmittjoh/php-option/badges/quality-score.png?s=7feb0fcc5150b36216f7683292ed8480aab1628b)](https://scrutinizer-ci.com/g/schmittjoh/php-option/)
1+
PHP Option Type [![Build Status](https://secure.travis-ci.org/mister-spock/php-option.png)](http://travis-ci.org/mister-spock/php-option)
22
===============
33
This adds an Option type for PHP.
44

‎src/PhpOption/Some.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
namespace PhpOption;
2020

2121
use ArrayIterator;
22+
use RecursiveArrayIterator;
23+
use RecursiveIteratorIterator;
2224

2325
final class Some extends Option
2426
{
@@ -132,9 +134,9 @@ public function filterIsOneOf(...$classes)
132134
return None::create();
133135
}
134136

135-
if (is_array($classes[0]) || $classes[0] instanceof \Traversable) {
136-
$classes = $classes[0];
137-
}
137+
$classes = new RecursiveIteratorIterator(
138+
new RecursiveArrayIterator($classes)
139+
);
138140

139141
foreach($classes as $class) {
140142
if (is_a($this->value, $class)) {

‎tests/PhpOption/Tests/LazyOptionTest.php‎

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace PhpOption\Tests;
44

5+
use stdClass;
6+
use ArrayIterator;
7+
use PhpOption\Some;
8+
use PhpOption\None;
59
use PhpOption\LazyOption;
610

711
class LazyOptionTest extends \PHPUnit_Framework_TestCase
@@ -18,13 +22,13 @@ public function setUp()
1822

1923
public function testGetWithArgumentsAndConstructor()
2024
{
21-
$some = \PhpOption\LazyOption::create(array($this->subject, 'execute'), array('foo'));
25+
$some = LazyOption::create(array($this->subject, 'execute'), array('foo'));
2226

2327
$this->subject
2428
->expects($this->once())
2529
->method('execute')
2630
->with('foo')
27-
->will($this->returnValue(\PhpOption\Some::create('foo')));
31+
->will($this->returnValue(Some::create('foo')));
2832

2933
$this->assertEquals('foo', $some->get());
3034
$this->assertEquals('foo', $some->getOrElse(null));
@@ -35,13 +39,13 @@ public function testGetWithArgumentsAndConstructor()
3539

3640
public function testGetWithArgumentsAndCreate()
3741
{
38-
$some = new \PhpOption\LazyOption(array($this->subject, 'execute'), array('foo'));
42+
$some = new LazyOption(array($this->subject, 'execute'), array('foo'));
3943

4044
$this->subject
4145
->expects($this->once())
4246
->method('execute')
4347
->with('foo')
44-
->will($this->returnValue(\PhpOption\Some::create('foo')));
48+
->will($this->returnValue(Some::create('foo')));
4549

4650
$this->assertEquals('foo', $some->get());
4751
$this->assertEquals('foo', $some->getOrElse(null));
@@ -52,12 +56,12 @@ public function testGetWithArgumentsAndCreate()
5256

5357
public function testGetWithoutArgumentsAndConstructor()
5458
{
55-
$some = new \PhpOption\LazyOption(array($this->subject, 'execute'));
59+
$some = new LazyOption(array($this->subject, 'execute'));
5660

5761
$this->subject
5862
->expects($this->once())
5963
->method('execute')
60-
->will($this->returnValue(\PhpOption\Some::create('foo')));
64+
->will($this->returnValue(Some::create('foo')));
6165

6266
$this->assertEquals('foo', $some->get());
6367
$this->assertEquals('foo', $some->getOrElse(null));
@@ -68,12 +72,12 @@ public function testGetWithoutArgumentsAndConstructor()
6872

6973
public function testGetWithoutArgumentsAndCreate()
7074
{
71-
$option = \PhpOption\LazyOption::create(array($this->subject, 'execute'));
75+
$option = LazyOption::create(array($this->subject, 'execute'));
7276

7377
$this->subject
7478
->expects($this->once())
7579
->method('execute')
76-
->will($this->returnValue(\PhpOption\Some::create('foo')));
80+
->will($this->returnValue(Some::create('foo')));
7781

7882
$this->assertTrue($option->isDefined());
7983
$this->assertFalse($option->isEmpty());
@@ -89,12 +93,12 @@ public function testGetWithoutArgumentsAndCreate()
8993
*/
9094
public function testCallbackReturnsNull()
9195
{
92-
$option = \PhpOption\LazyOption::create(array($this->subject, 'execute'));
96+
$option = LazyOption::create(array($this->subject, 'execute'));
9397

9498
$this->subject
9599
->expects($this->once())
96100
->method('execute')
97-
->will($this->returnValue(\PhpOption\None::create()));
101+
->will($this->returnValue(None::create()));
98102

99103
$this->assertFalse($option->isDefined());
100104
$this->assertTrue($option->isEmpty());
@@ -110,7 +114,7 @@ public function testCallbackReturnsNull()
110114
*/
111115
public function testExceptionIsThrownIfCallbackReturnsNonOption()
112116
{
113-
$option = \PhpOption\LazyOption::create(array($this->subject, 'execute'));
117+
$option = LazyOption::create(array($this->subject, 'execute'));
114118

115119
$this->subject
116120
->expects($this->once())
@@ -121,21 +125,21 @@ public function testExceptionIsThrownIfCallbackReturnsNonOption()
121125
}
122126

123127
/**
124-
* @expectedException InvalidArgumentException
128+
* @expectedException \InvalidArgumentException
125129
* @expectedExceptionMessage Invalid callback given
126130
*/
127131
public function testInvalidCallbackAndConstructor()
128132
{
129-
new \PhpOption\LazyOption('invalidCallback');
133+
new LazyOption('invalidCallback');
130134
}
131135

132136
/**
133-
* @expectedException InvalidArgumentException
137+
* @expectedException \InvalidArgumentException
134138
* @expectedExceptionMessage Invalid callback given
135139
*/
136140
public function testInvalidCallbackAndCreate()
137141
{
138-
\PhpOption\LazyOption::create('invalidCallback');
142+
LazyOption::create('invalidCallback');
139143
}
140144

141145
public function testifDefined()
@@ -162,10 +166,10 @@ public function testForAll()
162166

163167
public function testOrElse()
164168
{
165-
$some = \PhpOption\Some::create('foo');
166-
$lazy = \PhpOption\LazyOption::create(function() use ($some) {return $some;});
167-
$this->assertSame($some, $lazy->orElse(\PhpOption\None::create()));
168-
$this->assertSame($some, $lazy->orElse(\PhpOption\Some::create('bar')));
169+
$some = Some::create('foo');
170+
$lazy = LazyOption::create(function() use ($some) {return $some;});
171+
$this->assertSame($some, $lazy->orElse(None::create()));
172+
$this->assertSame($some, $lazy->orElse(Some::create('bar')));
169173
}
170174

171175
public function testFoldLeftRight()
@@ -187,4 +191,17 @@ public function testFoldLeftRight()
187191
$lazyOption = new LazyOption(function() use ($option) { return $option; });
188192
$this->assertSame(6, $lazyOption->foldRight(5, $callback));
189193
}
194+
195+
public function testFilterIsOneOf()
196+
{
197+
$some = new Some(new stdClass());
198+
$lazy_opt = LazyOption::create(function() use ($some) { return $some; });
199+
200+
$this->assertInstanceOf('PhpOption\None', $lazy_opt->filterIsOneOf('unknown', 'unknown2'));
201+
$this->assertInstanceOf('PhpOption\None', $lazy_opt->filterIsOneOf(['unknown', 'unknown2']));
202+
203+
$this->assertSame($some, $lazy_opt->filterIsOneOf(stdClass::class, 'unknown'));
204+
$this->assertSame($some, $lazy_opt->filterIsOneOf([stdClass::class, 'unknown']));
205+
$this->assertSame($some, $lazy_opt->filterIsOneOf(new ArrayIterator([stdClass::class, 'unknown'])));
206+
}
190207
}

‎tests/PhpOption/Tests/NoneTest.php‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace PhpOption\Tests;
44

5+
use stdClass;
56
use PhpOption\None;
7+
use PhpOption\Some;
68

79
class NoneTest extends \PHPUnit_Framework_TestCase
810
{
@@ -13,19 +15,19 @@ class NoneTest extends \PHPUnit_Framework_TestCase
1315
*/
1416
public function testGet()
1517
{
16-
$none = \PhpOption\None::create();
18+
$none = None::create();
1719
$none->get();
1820
}
1921

2022
public function testGetOrElse()
2123
{
22-
$none = \PhpOption\None::create();
24+
$none = None::create();
2325
$this->assertEquals('foo', $none->getOrElse('foo'));
2426
}
2527

2628
public function testGetOrCall()
2729
{
28-
$none = \PhpOption\None::create();
30+
$none = None::create();
2931
$this->assertEquals('foo', $none->getOrCall(function() { return 'foo'; }));
3032
}
3133

@@ -40,14 +42,14 @@ public function testGetOrThrow()
4042

4143
public function testIsEmpty()
4244
{
43-
$none = \PhpOption\None::create();
45+
$none = None::create();
4446
$this->assertTrue($none->isEmpty());
4547
}
4648

4749
public function testOrElse()
4850
{
49-
$option = \PhpOption\Some::create('foo');
50-
$this->assertSame($option, \PhpOption\None::create()->orElse($option));
51+
$option = Some::create('foo');
52+
$this->assertSame($option, None::create()->orElse($option));
5153
}
5254

5355
public function testifDefined()
@@ -115,7 +117,7 @@ public function testReject()
115117

116118
public function testForeach()
117119
{
118-
$none = \PhpOption\None::create();
120+
$none = None::create();
119121

120122
$called = 0;
121123
foreach ($none as $value) {

‎tests/PhpOption/Tests/OptionTest.php‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
namespace PhpOption\Tests;
44

55
use PhpOption\None;
6-
use PhpOption\Option;
76
use PhpOption\Some;
7+
use PhpOption\Option;
8+
use PhpOption\LazyOption;
89

910
class OptionTest extends \PHPUnit_Framework_TestCase
1011
{
1112
public function testfromValueWithDefaultNoneValue()
1213
{
13-
$this->assertInstanceOf('PhpOption\None', \PhpOption\Option::fromValue(null));
14-
$this->assertInstanceOf('PhpOption\Some', \PhpOption\Option::fromValue('value'));
14+
$this->assertInstanceOf('PhpOption\None', Option::fromValue(null));
15+
$this->assertInstanceOf('PhpOption\Some', Option::fromValue('value'));
1516
}
1617

1718
public function testFromValueWithFalseNoneValue()
1819
{
19-
$this->assertInstanceOf('PhpOption\None', \PhpOption\Option::fromValue(false, false));
20-
$this->assertInstanceOf('PhpOption\Some', \PhpOption\Option::fromValue('value', false));
21-
$this->assertInstanceOf('PhpOption\Some', \PhpOption\Option::fromValue(null, false));
20+
$this->assertInstanceOf('PhpOption\None', Option::fromValue(false, false));
21+
$this->assertInstanceOf('PhpOption\Some', Option::fromValue('value', false));
22+
$this->assertInstanceOf('PhpOption\Some', Option::fromValue(null, false));
2223
}
2324

2425
public function testFromArraysValue()
@@ -36,25 +37,25 @@ public function testFromReturn()
3637
$false = function() { return false; };
3738
$some = function() { return 'foo'; };
3839

39-
$this->assertTrue(\PhpOption\Option::fromReturn($null)->isEmpty());
40-
$this->assertFalse(\PhpOption\Option::fromReturn($false)->isEmpty());
41-
$this->assertTrue(\PhpOption\Option::fromReturn($false, array(), false)->isEmpty());
42-
$this->assertTrue(\PhpOption\Option::fromReturn($some)->isDefined());
43-
$this->assertFalse(\PhpOption\Option::fromReturn($some, array(), 'foo')->isDefined());
40+
$this->assertTrue(Option::fromReturn($null)->isEmpty());
41+
$this->assertFalse(Option::fromReturn($false)->isEmpty());
42+
$this->assertTrue(Option::fromReturn($false, array(), false)->isEmpty());
43+
$this->assertTrue(Option::fromReturn($some)->isDefined());
44+
$this->assertFalse(Option::fromReturn($some, array(), 'foo')->isDefined());
4445
}
4546

4647
public function testOrElse()
4748
{
48-
$a = new \PhpOption\Some('a');
49-
$b = new \PhpOption\Some('b');
49+
$a = new Some('a');
50+
$b = new Some('b');
5051

5152
$this->assertEquals('a', $a->orElse($b)->get());
5253
}
5354

5455
public function testOrElseWithNoneAsFirst()
5556
{
56-
$a = \PhpOption\None::create();
57-
$b = new \PhpOption\Some('b');
57+
$a = None::create();
58+
$b = new Some('b');
5859

5960
$this->assertEquals('b', $a->orElse($b)->get());
6061
}
@@ -63,18 +64,18 @@ public function testOrElseWithLazyOptions()
6364
{
6465
$throws = function() { throw new \LogicException('Should never be called.'); };
6566

66-
$a = new \PhpOption\Some('a');
67-
$b = new \PhpOption\LazyOption($throws);
67+
$a = new Some('a');
68+
$b = new LazyOption($throws);
6869

6970
$this->assertEquals('a', $a->orElse($b)->get());
7071
}
7172

7273
public function testOrElseWithMultipleAlternatives()
7374
{
74-
$throws = new \PhpOption\LazyOption(function() { throw new \LogicException('Should never be called.'); });
75-
$returns = new \PhpOption\LazyOption(function() { return new \PhpOption\Some('foo'); });
75+
$throws = new LazyOption(function() { throw new \LogicException('Should never be called.'); });
76+
$returns = new LazyOption(function() { return new Some('foo'); });
7677

77-
$a = \PhpOption\None::create();
78+
$a = None::create();
7879

7980
$this->assertEquals('foo', $a->orElse($returns)->orElse($throws)->get());
8081
}

0 commit comments

Comments
 (0)