Skip to content

Commit

Permalink
arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
devosc committed Mar 7, 2020
1 parent 89b7e95 commit ceb4b77
Show file tree
Hide file tree
Showing 57 changed files with 167 additions and 373 deletions.
68 changes: 21 additions & 47 deletions src/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AppTest
*/
function test_array_access_with_provider()
{
$app = new App([], function($name) { return 'foo' == $name ? 'bar' : null; });
$app = new App([], fn($name) => 'foo' == $name ? 'bar' : null);

$this->assertEquals('bar', $app['foo']);
}
Expand All @@ -54,7 +54,7 @@ function test_config()
*/
function test_invoke_with_provider()
{
$app = new App([], function() { return 'bar'; });
$app = new App([], fn() => 'bar');

$this->assertEquals('bar', $app('foo'));
}
Expand All @@ -76,38 +76,28 @@ function test_provider_and_scope()
{
$app = new App([
Arg::SERVICES => [
'var3' => function() { return 'foobar'; },
'var3' => fn() => 'foobar',
'var2' => [Config::class, new Args(['var3' => new Plugin('var3')])],
'bat' => function($var2) {
return $var2['var3'];
},
'bat' => fn($var2) => $var2['var3'],
Config::class => Config::class,
'v3' => function() { return '6'; },
'v3' => fn() => '6',
'v2' => [Config::class, new Args(['v3' => new Plugin('v3')])],
'var4' => function($v2) { return $v2['v3']; },
'var4' => fn($v2) => $v2['v3'],
'code' => 1,
'foo' => new Plugins([
'home' => 9,
'var2' => new Plugin(Config::class, [new Args(['var3' => new Provide('var4')])]),
Config::class => function($argv) {
return new Config($argv);
},
Config::class => fn($argv) => new Config($argv),
'code' => 2,
'bar' => new Plugins([
'code' => 5,
Config::class => new Provide(Config::class), //Provide from parent
'test' => function($bat, $code, $home, $var2, Config $config) {
return function($param, $param2, Config $config) use($bat, $code, $home, $var2) {
return $bat . $code . $home . $param . $var2['var3'] . $param2;
};
},
'baz' => function() {
return function($param2) {
/** @var \Mvc5\Service\Plugin $this */

return $this->call('test', ['param' => '3', 'param2' => $param2]);
};
}
'test' => fn($bat, $code, $home, $var2, Config $config) =>
fn($param, $param2, Config $config) =>
$bat . $code . $home . $param . $var2['var3'] . $param2,
'baz' => fn() => fn($param2) => $this->call('test', ['param' => '3', 'param2' => $param2])


])
])
]
Expand All @@ -129,9 +119,7 @@ function test_scope()
{
$app = new App([
'services' => [
'bar' => function() {
return $this;
},
'bar' => fn() => $this,
'foo' => new Plugin('bar')
]
], null, true);
Expand All @@ -149,9 +137,7 @@ function test_custom_scope()

$app = new App([
'services' => [
'bar' => function() {
return $this;
},
'bar' => fn() => $this,
'foo' => new Plugin('bar')
]
], null, $config);
Expand All @@ -167,12 +153,8 @@ function test_callable_closure_scope()
{
$app = new App([
'services' => [
'bar' => new Callback(function() {
return $this;
}),
'foo' => new Invoke(function() {
return $this;
}),
'bar' => new Callback(fn() => $this),
'foo' => new Invoke(fn() => $this),
]
], null, true);

Expand All @@ -189,12 +171,8 @@ function test_callable_closure_custom_scope()

$app = new App([
'services' => [
'bar' => new Callback(function() {
return $this;
}),
'foo' => new Invoke(function() {
return $this;
}),
'bar' => new Callback(fn() => $this),
'foo' => new Invoke(fn() => $this),
]
], null, $config);

Expand All @@ -209,12 +187,8 @@ function test_callable_closure_without_scope()
{
$app = new App([
'services' => [
'bar' => new Callback(function() {
return $this;
}),
'foo' => new Invoke(function() {
return $this;
}),
'bar' => new Callback(fn() => $this),
'foo' => new Invoke(fn() => $this),
]
]);

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function test_call_controller()
{
$action = new Action(new App);

$this->assertEquals('foo', $action(function() { return 'foo'; }));
$this->assertEquals('foo', $action(fn() => 'foo'));
}

/**
Expand Down
12 changes: 3 additions & 9 deletions src/Event/EventModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ function test_named_args()
{
$event = new TestEvent;

$callable = function($foo){
return 'foo' . $foo;
};
$callable = fn($foo) => 'foo' . $foo;

$this->assertEquals('foo/bar', $event($callable, ['foo' => '/bar']));
}
Expand All @@ -32,9 +30,7 @@ function test_no_args()
{
$event = new TestEvent;

$callable = function(Event $event){
return $event->name();
};
$callable = fn(Event $event) => $event->name();

$this->assertEquals(TestEvent::class, $event($callable));
}
Expand All @@ -46,9 +42,7 @@ function test_numeric_args()
{
$event = new TestEvent;

$callable = function($foo, $bar){
return $foo. '/' . $bar;
};
$callable = fn($foo, $bar) => $foo. '/' . $bar;

$this->assertEquals('foo/bar', $event($callable, ['foo', 'bar']));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Event/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function test_named_args()
{
$event = new Event;

$this->assertEquals('bar', $event(function($bar, $foo) { return $foo; }, ['foo' => 'bar', 'bar' => 'baz']));
$this->assertEquals('bar', $event(fn($bar, $foo) => $foo, ['foo' => 'bar', 'bar' => 'baz']));
}

/**
Expand All @@ -38,7 +38,7 @@ function test_numeric_args()
{
$event = new Event;

$this->assertEquals('baz', $event(function($bar, $foo) { return $foo; }, ['bar', 'baz']));
$this->assertEquals('baz', $event(fn($bar, $foo) => $foo, ['bar', 'baz']));
}

/**
Expand All @@ -48,7 +48,7 @@ function test_stopped()
{
$event = new Event;

$event(function(Event $event) { $event->stop(); });
$event(fn(Event $event) => $event->stop());

$this->assertTrue($event->stopped());
}
Expand Down
6 changes: 2 additions & 4 deletions src/Event/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ function test_event_array()
{
$app = new App([
'services' => [
'test_event' => function() {
return [
'test_event' => fn() => [
'@Mvc5\Test\Event\GeneratorTest::foo',
'@Mvc5\Test\Event\GeneratorTest::bar',
'@Mvc5\Test\Event\GeneratorTest::baz',
];
}
]
]
]);

Expand Down
8 changes: 2 additions & 6 deletions src/Http/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ function test_reset()
$middleware = new Middleware(
new App,
[
function(HttpRequest $request, HttpResponse $response, callable $next) {
return $next($request, $response);
},
function(HttpRequest $request, HttpResponse $response, callable $next) {
return $next($request, $response);
}
fn(HttpRequest $request, HttpResponse $response, callable $next) => $next($request, $response),
fn(HttpRequest $request, HttpResponse $response, callable $next) => $next($request, $response)
]
);

Expand Down
8 changes: 4 additions & 4 deletions src/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function test_message()
{
$logger = new Logger;

$this->assertEquals('foo', $logger(function() { return 'foo'; }));
$this->assertEquals('foo', $logger(function($message) { return $message; }));
$this->assertEquals('foo', $logger(fn() => 'foo'));
$this->assertEquals('foo', $logger(fn($message) => $message));
}

/**
Expand All @@ -29,7 +29,7 @@ function test_throw_exception_false()
{
$logger = new Logger;

$this->assertFalse($logger(function($throw_exception = false) { return $throw_exception; }));
$this->assertFalse($logger(fn($throw_exception = false) => $throw_exception));
}

/**
Expand All @@ -39,6 +39,6 @@ function test_throw_exception_true()
{
$logger = new Logger(true);

$this->assertTrue($logger(function($throw_exception) { return $throw_exception; }));
$this->assertTrue($logger(fn($throw_exception) => $throw_exception));
}
}
10 changes: 5 additions & 5 deletions src/Plugin/CallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function test()
*/
function test_named()
{
$call = new Call(function($foo, $bar) { return $foo . $bar; });
$call = new Call(fn($foo, $bar) => $foo . $bar);

$this->assertEquals('foobar', (new App)->plugin($call, ['foo' => 'foo', 'bar' => 'bar']));
}
Expand All @@ -38,7 +38,7 @@ function test_named()
*/
function test_named_with_parent_args()
{
$call = new Call(function($foo, $bar) { return $foo . $bar; }, ['bar' => 'bar']);
$call = new Call(fn($foo, $bar) => $foo . $bar, ['bar' => 'bar']);

$this->assertEquals('foobar', (new App)->plugin($call, ['foo' => 'foo']));
}
Expand All @@ -48,7 +48,7 @@ function test_named_with_parent_args()
*/
function test_no_args()
{
$call = new Call(function() { return 'foobar'; });
$call = new Call(fn() => 'foobar');

$this->assertEquals('foobar', (new App)->plugin($call));
}
Expand All @@ -58,7 +58,7 @@ function test_no_args()
*/
function test_not_named()
{
$call = new Call(function($foo, $bar) { return $foo . $bar; });
$call = new Call(fn($foo, $bar) => $foo . $bar);

$this->assertEquals('foobar', (new App)->plugin($call, ['foo', 'bar']));
}
Expand All @@ -68,7 +68,7 @@ function test_not_named()
*/
function test_not_named_with_parent_args()
{
$call = new Call(function($foo, $bar) { return $foo . $bar; }, ['bar']);
$call = new Call(fn($foo, $bar) => $foo . $bar, ['bar']);

$this->assertEquals('foobar', (new App)->plugin($call, ['foo']));
}
Expand Down
10 changes: 3 additions & 7 deletions src/Plugin/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CallbackTest
*/
function test()
{
$scoped = new Callback(function(){});
$scoped = new Callback(fn() => null);

$this->assertInstanceOf(\Closure::class, $scoped->closure());
$this->assertFalse($scoped->scoped());
Expand All @@ -30,9 +30,7 @@ function test_app_scope()
{
$app = new App(null, null, true);

$result = $app->call(new Callback(function() {
return $this;
}));
$result = $app->call(new Callback(fn() => $this));

$this->assertTrue($result === $app);
}
Expand All @@ -44,9 +42,7 @@ function test_current_scope()
{
$app = new App;

$result = $app->call(new Callback(function() {
return $this;
}));
$result = $app->call(new Callback(fn() => $this));

$this->assertTrue($result === $this);
}
Expand Down
12 changes: 5 additions & 7 deletions src/Plugin/ExpectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ function test_exception()
{
(new App)(new Expect(
new Call('@Mvc5\Exception::runtime', ['test']),
new Call(function(\Throwable $e) {
$this->assertEquals('test', $e->getMessage());
})
new Call(fn(\Throwable $e) => $this->assertEquals('test', $e->getMessage()))
));
}

Expand All @@ -57,7 +55,7 @@ function test_exception_as_named_arg()
{
$result = (new App)(new Expect(
new Call(function() { throw new \Exception('foo'); }),
new Call(function(\Throwable $exception) { return $exception->getMessage(); }),
new Call(fn(\Throwable $exception) => $exception->getMessage()),
true
));

Expand All @@ -71,7 +69,7 @@ function test_exception_with_named_args()
{
$result = (new App)(new Expect(
new Call(function() { throw new \Exception('foo'); }),
new Call(function(\Throwable $exception, $argv) { return $exception->getMessage() . $argv['bar']; }),
new Call(fn(\Throwable $exception, $argv) => $exception->getMessage() . $argv['bar']),
true,
true
), ['bar' => 'bar']);
Expand All @@ -85,7 +83,7 @@ function test_exception_with_named_args()
function test_named_args()
{
$result = (new App)(new Expect(
new Call(function($b, $a) { return $a . $b; }, ['b' => new Value('bar')])
new Call(fn($b, $a) => $a . $b, ['b' => new Value('bar')])
), ['a' => 'foo']);

$this->assertEquals('foobar', $result);
Expand All @@ -96,7 +94,7 @@ function test_named_args()
*/
function test_not_named_args()
{
$result = (new App)(new Expect(new Call(function($a, $b) { return $a . $b; }, ['bar'])), ['foo']);
$result = (new App)(new Expect(new Call(fn($a, $b) => $a . $b, ['bar'])), ['foo']);

$this->assertEquals('foobar', $result);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Plugin/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ function test_plugin()
{
$app = new App([
'services' => [
'bar' => function() {
return function() { return 'baz'; };
},
'bar' => fn() => fn() => 'baz',
'factory' => new Plugin('bar')
]
]);
Expand Down
Loading

0 comments on commit ceb4b77

Please sign in to comment.