Skip to content

Commit 165a44b

Browse files
committed
Add testing helpers for component testing
1 parent 99d39e9 commit 165a44b

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/core package.
4+
*
5+
* (c) Marc Aschmann <maschmann@gmail.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+
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 <maschmann@gmail.com>
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+
$this->outPortCallbacks = [];
53+
foreach ($component->outPorts() as $port) {
54+
$socket = $this->stub(
55+
InternalSocket::class,
56+
[
57+
'isConnected' => true,
58+
]
59+
);
60+
$socket->expects($this->any())
61+
->method('send')
62+
->willReturnCallback(
63+
\Closure::bind(
64+
function ($data) {
65+
$this->data = $data;
66+
},
67+
$socket
68+
)
69+
);
70+
$this->outPortSockets[$port->getName()] = $socket;
71+
$socket->from = [];
72+
$socket->to = [];
73+
$port->attach($socket);
74+
}
75+
76+
return $component;
77+
}
78+
79+
/**
80+
* @param string $port
81+
* @return array|mixed
82+
*/
83+
protected function getOutPortData($port = '')
84+
{
85+
if ('' !== $port) {
86+
if (array_key_exists($port, $this->outPortSockets)) {
87+
return $this->outPortSockets[$port]->data;
88+
}
89+
}
90+
91+
return $this->outPortSockets;
92+
}
93+
94+
/**
95+
* @param string $port
96+
* @return bool
97+
*/
98+
protected function wasCalled($port)
99+
{
100+
return !empty($this->outPortSockets[$port]);
101+
}
102+
}

lib/PhpFlo/Test/StubTrait.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/core package.
4+
*
5+
* (c) Marc Aschmann <maschmann@gmail.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+
namespace PhpFlo\Test;
11+
12+
/**
13+
* Class StubTrait
14+
*
15+
* @package PhpFlo\PhpFloBundle\Test
16+
* @author Marc Aschmann <maschmann@gmail.com>
17+
*/
18+
trait StubTrait
19+
{
20+
/**
21+
* Will create a stub with several methods and defined return values.
22+
* definition:
23+
* [
24+
* 'myMethod' => 'somevalue',
25+
* 'myOtherMethod' => $callback,
26+
* 'anotherMethod' => function ($x) use ($y) {},
27+
* ]
28+
*
29+
* @param string $class
30+
* @param array $methods
31+
* @param string $className classname for mock object
32+
* @param bool $forceMethods
33+
* @return \PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected function stub($class, array $methods = [], $className = '', $forceMethods = false)
36+
{
37+
$builder = $this->getMockBuilder($class)
38+
->disableOriginalConstructor();
39+
40+
if (!empty($methods) && $forceMethods) {
41+
$builder->setMethods(array_keys($methods));
42+
}
43+
44+
if ('' !== $className) {
45+
$builder->setMockClassName($className);
46+
}
47+
48+
$stub = $builder->getMock();
49+
foreach ($methods as $method => $value) {
50+
if (is_callable($value)) {
51+
$stub->expects($this->any())->method($method)->willReturnCallback($value);
52+
} else {
53+
$stub->expects($this->any())->method($method)->willReturn($value);
54+
}
55+
}
56+
57+
return $stub;
58+
}
59+
}

0 commit comments

Comments
 (0)