|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the phpflo/core package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace PhpFlo\Test; |
|
11
|
|
|
|
|
12
|
|
|
use PhpFlo\Common\ComponentInterface; |
|
13
|
|
|
use PhpFlo\Interaction\InternalSocket; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ComponentTestHelperTrait |
|
17
|
|
|
* |
|
18
|
|
|
* @package PhpFlo\PhpFloBundle\Test |
|
19
|
|
|
* @author Marc Aschmann <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ComponentTestHelperTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $outPortSockets; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Fake-connect socket to port. |
|
30
|
|
|
* |
|
31
|
|
|
* @param ComponentInterface $component |
|
32
|
|
|
* @return ComponentInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function connectInPorts(ComponentInterface $component) |
|
35
|
|
|
{ |
|
36
|
|
|
foreach ($component->inPorts() as $alias => $inPort) { |
|
37
|
|
|
$inPort->attach($this->stub(InternalSocket::class)); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $component; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Fake-connect socket to port and add a storage for later value checks. |
|
45
|
|
|
* |
|
46
|
|
|
* @param ComponentInterface $component |
|
47
|
|
|
* @return ComponentInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function connectOutPorts(ComponentInterface $component) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->outPortSockets = []; |
|
52
|
|
|
foreach ($component->outPorts() as $port) { |
|
53
|
|
|
$socket = $this->stub( |
|
|
|
|
|
|
54
|
|
|
InternalSocket::class, |
|
55
|
|
|
[ |
|
56
|
|
|
'isConnected' => true, |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
$socket->expects($this->any()) |
|
|
|
|
|
|
60
|
|
|
->method('send') |
|
61
|
|
|
->willReturnCallback( |
|
62
|
|
|
\Closure::bind( |
|
63
|
|
|
function ($data) { |
|
64
|
|
|
$this->data = $data; |
|
|
|
|
|
|
65
|
|
|
}, |
|
66
|
|
|
$socket |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
$this->outPortSockets[$port->getName()] = $socket; |
|
70
|
|
|
$socket->from = []; |
|
71
|
|
|
$socket->to = []; |
|
72
|
|
|
$port->attach($socket); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $component; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $port |
|
80
|
|
|
* @return array|mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getOutPortData($port = '') |
|
83
|
|
|
{ |
|
84
|
|
|
if ('' !== $port) { |
|
85
|
|
|
if (array_key_exists($port, $this->outPortSockets)) { |
|
86
|
|
|
return $this->outPortSockets[$port]->data; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->outPortSockets; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $port |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function wasCalled($port) |
|
98
|
|
|
{ |
|
99
|
|
|
return !empty($this->outPortSockets[$port]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.