Skip to content

Commit f1eab07

Browse files
committed
maybe nullable defaults
1 parent 997316a commit f1eab07

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

src/Plugin/MaybeTest.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Mvc5\Plugin\Maybe;
1010
use Mvc5\Plugin\Nothing;
1111
use Mvc5\Plugin\Nullable;
12+
use Mvc5\Plugin\Plug;
1213
use Mvc5\Plugin\Plugin;
1314
use Mvc5\Test\Test\TestCase;
1415

@@ -24,46 +25,50 @@ function test()
2425

2526
$this->assertEquals([$maybe, '__invoke'], $maybe->config());
2627
$this->assertEquals(['foo'], $maybe->args());
28+
$this->assertInstanceOf(Nothing::class, (new Maybe)());
2729
}
2830

2931
/**
3032
*
3133
*/
32-
function test_plugin_returns_false()
34+
function test_custom_default()
3335
{
34-
$app = new App(['services' => [
35-
'foo' => new Maybe(false)
36-
]]);
36+
$this->assertEquals('bar', (new App)(new Maybe(null, 'bar')));
37+
}
3738

38-
$this->assertFalse($app['foo']);
39-
$this->assertFalse($app->plugin('foo'));
39+
/**
40+
*
41+
*/
42+
function test_default()
43+
{
44+
$this->assertInstanceOf(Nothing::class, (new Maybe)());
4045
}
4146

4247
/**
4348
*
4449
*/
45-
function test_plugin_returns_null()
50+
function test_null()
4651
{
4752
$app = new App(['services' => [
4853
'foo' => new Maybe(null),
4954
'foobar' => new Nullable(new Plugin('foo'))
5055
]]);
5156

52-
$this->assertNull($app['foobar']);
5357
$this->assertInstanceOf(Nothing::class, $app['foo']);
54-
$this->assertInstanceOf(Nothing::class, $app->plugin('foo'));
58+
$this->assertNull($app['foobar']);
5559
}
5660

5761
/**
5862
*
5963
*/
60-
function test_plugin_returns_something()
64+
function test_not_null()
6165
{
6266
$app = new App(['services' => [
63-
'foo' => new Maybe('foobar')
67+
'foo' => new Maybe('foobar'),
68+
'foobar' => new Nullable(new Plug('foo'))
6469
]]);
6570

6671
$this->assertEquals('foobar', $app['foo']);
67-
$this->assertEquals('foobar', $app->plugin('foo'));
72+
$this->assertEquals('foobar', $app['foobar']);
6873
}
6974
}

src/Plugin/NullableTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,29 @@ function test()
2424

2525
$this->assertEquals([$maybe, '__invoke'], $maybe->config());
2626
$this->assertEquals(['foo'], $maybe->args());
27+
$this->assertNull((new Nullable)());
2728
}
2829

2930
/**
3031
*
3132
*/
32-
function test_shared_null_value()
33+
function test_custom_default()
34+
{
35+
$this->assertEquals('bar', (new App)(new Nullable(new Maybe, 'bar')));
36+
}
37+
38+
/**
39+
*
40+
*/
41+
function test_default()
42+
{
43+
$this->assertEquals(null, (new App)(new Nullable));
44+
}
45+
46+
/**
47+
*
48+
*/
49+
function test_null_value()
3350
{
3451
$count = 0;
3552

@@ -47,4 +64,26 @@ function test_shared_null_value()
4764
$this->assertNull($app['foobar']);
4865
$this->assertEquals(1, $count);
4966
}
67+
68+
/**
69+
*
70+
*/
71+
function test_value_not_null()
72+
{
73+
$count = 0;
74+
75+
$app = new App(['services' => [
76+
'foo' => new Maybe(new Call(function() use (&$count) {
77+
++$count;
78+
return 'bar';
79+
})),
80+
'foobar' => new Nullable(new Shared('foo'))
81+
]]);
82+
83+
$this->assertEquals('bar', $app->plugin('foobar'));
84+
$this->assertEquals('bar', $app->shared('foobar'));
85+
$this->assertEquals('bar', $app->get('foobar'));
86+
$this->assertEquals('bar', $app['foobar']);
87+
$this->assertEquals(1, $count);
88+
}
5089
}

0 commit comments

Comments
 (0)