|
| 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 | +} |
0 commit comments