1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-flow |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-flow/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-flow |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFlow\Unit\Test\Flow; |
15
|
|
|
|
16
|
|
|
use Graze\DataFlow\Flow; |
17
|
|
|
use Graze\DataFlow\Flow\Callback; |
18
|
|
|
use Graze\DataFlow\FlowInterface; |
19
|
|
|
use Graze\DataFlow\Test\TestCase; |
20
|
|
|
use Graze\DataNode\NodeInterface; |
21
|
|
|
use Mockery as m; |
22
|
|
|
|
23
|
|
|
class CallbackTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testInstanceOf() |
26
|
|
|
{ |
27
|
|
|
static::assertInstanceOf(FlowInterface::class, new Callback(function () { |
28
|
|
|
})); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testCallingFlowWillCallTheCallback() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$called = null; |
34
|
|
|
$return = m::mock(NodeInterface::class); |
35
|
|
|
$callbackFlow = new Callback(function ($item) use (&$called, $return) { |
36
|
|
|
$called = $item; |
37
|
|
|
return $return; |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
$node = m::mock(NodeInterface::class); |
41
|
|
|
|
42
|
|
|
$output = $callbackFlow->flow($node); |
43
|
|
|
static::assertEquals($return, $output); |
44
|
|
|
static::assertEquals($called, $node); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testStaticCalling() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$called = null; |
50
|
|
|
$return = m::mock(NodeInterface::class); |
51
|
|
|
$callbackFlow = Flow::callback(function ($item) use (&$called, $return) { |
52
|
|
|
$called = $item; |
53
|
|
|
return $return; |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
$node = m::mock(NodeInterface::class); |
57
|
|
|
|
58
|
|
|
$output = $callbackFlow->flow($node); |
59
|
|
|
static::assertEquals($return, $output); |
60
|
|
|
static::assertEquals($called, $node); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testInvokeFlow() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$called = null; |
66
|
|
|
$return = m::mock(NodeInterface::class); |
67
|
|
|
$callbackFlow = Flow::callback(function ($item) use (&$called, $return) { |
68
|
|
|
$called = $item; |
69
|
|
|
return $return; |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
$node = m::mock(NodeInterface::class); |
73
|
|
|
|
74
|
|
|
$output = call_user_func($callbackFlow, $node); |
75
|
|
|
static::assertEquals($return, $output); |
76
|
|
|
static::assertEquals($called, $node); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.