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\Test\Unit\Flow\Collection; |
15
|
|
|
|
16
|
|
|
use Graze\DataFlow\Flow; |
17
|
|
|
use Graze\DataFlow\Flow\Collection\Last; |
18
|
|
|
use Graze\DataFlow\FlowInterface; |
19
|
|
|
use Graze\DataFlow\Test\TestCase; |
20
|
|
|
use Graze\DataNode\NodeCollectionInterface; |
21
|
|
|
use Graze\DataNode\NodeInterface; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use Mockery as m; |
24
|
|
|
|
25
|
|
View Code Duplication |
class LastTest extends TestCase |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
public function testInstanceOf() |
28
|
|
|
{ |
29
|
|
|
$flow = new Last(function () { |
30
|
|
|
return true; |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
static::assertInstanceOf(FlowInterface::class, $flow); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testInvalidInputThrowsAnException() |
37
|
|
|
{ |
38
|
|
|
$file = m::mock(NodeInterface::class); |
39
|
|
|
$flow = new Last(function () { |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$this->expectException(InvalidArgumentException::class); |
43
|
|
|
|
44
|
|
|
$flow->flow($file); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testFlow() |
48
|
|
|
{ |
49
|
|
|
$func = function () use (&$called) { |
50
|
|
|
$called = true; |
51
|
|
|
return true; |
52
|
|
|
}; |
53
|
|
|
$flow = new Last($func); |
54
|
|
|
|
55
|
|
|
$node = m::mock(NodeCollectionInterface::class); |
56
|
|
|
$node->shouldReceive('last') |
57
|
|
|
->with($func) |
58
|
|
|
->andReturn($node); |
59
|
|
|
|
60
|
|
|
$response = $flow->flow($node); |
61
|
|
|
|
62
|
|
|
static::assertSame($response, $node); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testStaticFlow() |
66
|
|
|
{ |
67
|
|
|
$func = function () use (&$called) { |
68
|
|
|
$called = true; |
69
|
|
|
return true; |
70
|
|
|
}; |
71
|
|
|
$flow = Flow::last($func); |
72
|
|
|
|
73
|
|
|
$node = m::mock(NodeCollectionInterface::class); |
74
|
|
|
$node->shouldReceive('last') |
75
|
|
|
->with($func) |
76
|
|
|
->andReturn($node); |
77
|
|
|
|
78
|
|
|
$response = $flow->flow($node); |
79
|
|
|
|
80
|
|
|
static::assertSame($response, $node); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testInvokeFlow() |
84
|
|
|
{ |
85
|
|
|
$func = function () use (&$called) { |
86
|
|
|
$called = true; |
87
|
|
|
return true; |
88
|
|
|
}; |
89
|
|
|
$flow = Flow::last($func); |
90
|
|
|
|
91
|
|
|
$node = m::mock(NodeCollectionInterface::class); |
92
|
|
|
$node->shouldReceive('last') |
93
|
|
|
->with($func) |
94
|
|
|
->andReturn($node); |
95
|
|
|
|
96
|
|
|
$response = call_user_func($flow, $node); |
97
|
|
|
|
98
|
|
|
static::assertSame($response, $node); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.