22
33namespace PhpOption \Tests ;
44
5+ use stdClass ;
6+ use ArrayIterator ;
7+ use PhpOption \Some ;
8+ use PhpOption \None ;
59use PhpOption \LazyOption ;
610
711class 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}
0 commit comments