Completed
Push — master ( e32eeb...714cda )
by
unknown
89:50 queued 46:29
created

AbstractConnectorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 9.0856
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Oro\Bundle\IntegrationBundle\Tests\Unit\Provider;
4
5
use Akeneo\Bundle\BatchBundle\Item\ExecutionContext;
6
use Akeneo\Bundle\BatchBundle\Entity\StepExecution;
7
8
use Symfony\Component\HttpKernel\Log\NullLogger;
9
10
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
11
use Oro\Bundle\IntegrationBundle\Entity\Transport;
12
use Oro\Bundle\IntegrationBundle\Logger\LoggerStrategy;
13
use Oro\Bundle\ImportExportBundle\Context\ContextRegistry;
14
use Oro\Bundle\IntegrationBundle\Provider\TransportInterface;
15
use Oro\Bundle\ImportExportBundle\Context\Context;
16
use Oro\Bundle\IntegrationBundle\Provider\AbstractConnector;
17
use Oro\Bundle\IntegrationBundle\Provider\ConnectorInterface;
18
use Oro\Bundle\IntegrationBundle\Tests\Unit\Stub\TestConnector;
19
20
class AbstractConnectorTest extends \PHPUnit_Framework_TestCase
21
{
22
    /** @var \PHPUnit_Framework_MockObject_MockObject|StepExecution */
23
    protected $stepExecutionMock;
24
25
    /** @var \PHPUnit_Framework_MockObject_MockObject|Transport */
26
    protected $transportSettings;
27
28
    /** @var TransportInterface|\PHPUnit_Framework_MockObject_MockObject */
29
    protected $transportMock;
30
31
    protected function setUp()
32
    {
33
        $this->stepExecutionMock = $this->getMockBuilder('Akeneo\\Bundle\\BatchBundle\\Entity\\StepExecution')
34
            ->setMethods(['getExecutionContext', 'getJobExecution'])
35
            ->disableOriginalConstructor()->getMock();
36
37
        $jobExecution = $this->getMock('Akeneo\Bundle\BatchBundle\Entity\JobExecution');
38
        $jobInstance = $this->getMock('Akeneo\Bundle\BatchBundle\Entity\JobInstance');
39
        $jobExecution->expects($this->any())
40
            ->method('getJobInstance')
41
            ->will($this->returnValue($jobInstance));
42
43
        $this->stepExecutionMock->expects($this->any())
44
            ->method('getJobExecution')
45
            ->will($this->returnValue($jobExecution));
46
47
        $jobInstance->expects($this->any())
48
            ->method('getAlias')
49
            ->will($this->returnValue('alias'));
50
51
        $this->transportSettings = $this->getMockForAbstractClass('Oro\\Bundle\\IntegrationBundle\\Entity\\Transport');
52
        $this->transportMock     = $this->getMock('Oro\\Bundle\\IntegrationBundle\\Provider\\TransportInterface');
53
    }
54
55
    protected function tearDown()
56
    {
57
        unset($this->transportMock, $this->stepExecutionMock);
58
    }
59
60
    /**
61
     * @dataProvider initializationDataProvider
62
     *
63
     * @param \PHPUnit_Framework_MockObject_MockObject|mixed $transport
64
     * @param null                                           $source
65
     * @param bool|string                                    $expectedException
66
     */
67
    public function testInitialization($transport, $source = null, $expectedException = false)
68
    {
69
        $logger              = new LoggerStrategy(new NullLogger());
70
        $contextRegistry     = new ContextRegistry();
71
        $contextMediatorMock = $this
72
            ->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\ConnectorContextMediator')
73
            ->disableOriginalConstructor()->getMock();
74
        $integration             = new Integration();
75
        $integration->setTransport($this->transportSettings);
76
77
        $context = $contextRegistry->getByStepExecution($this->stepExecutionMock);
78
        $contextMediatorMock->expects($this->at(0))
79
            ->method('getTransport')->with($this->equalTo($context))
80
            ->will($this->returnValue($transport));
81
        $contextMediatorMock->expects($this->at(1))
82
            ->method('getChannel')->with($this->equalTo($context))
83
            ->will($this->returnValue($integration));
84
85
        $connector = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\AbstractConnector')
86
            ->setMethods(['getConnectorSource'])
87
            ->setConstructorArgs([$contextRegistry, $logger, $contextMediatorMock])
88
            ->getMockForAbstractClass();
89
90
        if (false !== $expectedException) {
91
            $this->setExpectedException($expectedException);
92
        } else {
93
            $transport->expects($this->once())->method('init')
94
                ->with($this->equalTo($this->transportSettings));
95
            $connector->expects($this->once())->method('getConnectorSource')
96
                ->will($this->returnValue($source));
97
        }
98
99
        $connector->setStepExecution($this->stepExecutionMock);
100
101
        $this->assertAttributeSame($transport, 'transport', $connector);
102
        $this->assertAttributeSame($integration, 'channel', $connector);
103
    }
104
105
    public function initializationDataProvider()
106
    {
107
        return [
108
            'bad transport given, exception expected'       => [
109
                false,
110
                null,
111
                '\LogicException'
112
            ],
113
            'with regular iterator, correct initialization' => [
114
                $this->getMock('Oro\\Bundle\\IntegrationBundle\\Provider\\TransportInterface'),
115
                $this->getMock('\Iterator')
116
            ],
117
            'logger aware iterator should receive logger'   => [
118
                $this->getMock('Oro\\Bundle\\IntegrationBundle\\Provider\\TransportInterface'),
119
                $this->getMock('Oro\\Bundle\\IntegrationBundle\\Tests\Unit\Stub\\LoggerAwareIteratorSource')
120
            ]
121
        ];
122
    }
123
124
    /**
125
     * @param mixed            $transport
126
     * @param mixed            $stepExecutionMock
127
     * @param null|Integration $channel
128
     *
129
     * @param null             $context
130
     *
131
     * @return AbstractConnector
132
     */
133
    protected function getConnector($transport, $stepExecutionMock, $channel = null, $context = null)
134
    {
135
        $contextRegistryMock = $this->getMock('Oro\Bundle\ImportExportBundle\Context\ContextRegistry');
136
        $contextMediatorMock = $this
137
            ->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\ConnectorContextMediator')
138
            ->disableOriginalConstructor()->getMock();
139
140
        $transportSettings = $this->getMockForAbstractClass('Oro\\Bundle\\IntegrationBundle\\Entity\\Transport');
141
        $channel           = $channel ? : new Integration();
142
        $channel->setTransport($transportSettings);
143
144
        $contextMock = $context ? : new Context([]);
145
146
        $executionContext = new ExecutionContext();
147
        $stepExecutionMock->expects($this->any())
148
            ->method('getExecutionContext')->will($this->returnValue($executionContext));
149
150
        $contextRegistryMock->expects($this->any())->method('getByStepExecution')
151
            ->will($this->returnValue($contextMock));
152
        $contextMediatorMock->expects($this->once())
153
            ->method('getTransport')->with($this->equalTo($contextMock))
154
            ->will($this->returnValue($transport));
155
        $contextMediatorMock->expects($this->once())
156
            ->method('getChannel')->with($this->equalTo($contextMock))
157
            ->will($this->returnValue($channel));
158
159
        $logger = new LoggerStrategy(new NullLogger());
160
161
        return new TestConnector($contextRegistryMock, $logger, $contextMediatorMock);
162
    }
163
164
    public function testGetStatusData()
165
    {
166
        $connector = $this->getConnector($this->transportMock, $this->stepExecutionMock);
167
        $connector->setStepExecution($this->stepExecutionMock);
168
169
        $reflection = new \ReflectionMethod(
170
            '\Oro\Bundle\IntegrationBundle\Tests\Unit\Stub\TestConnector',
171
            'addStatusData'
172
        );
173
        $reflection->setAccessible(true);
174
        $reflection->invoke($connector, 'key', 'value');
175
176
        $context = $this->stepExecutionMock->getExecutionContext();
177
        $date    = $context->get(ConnectorInterface::CONTEXT_CONNECTOR_DATA_KEY);
178
179
        $this->assertArrayHasKey('key', $date);
180
        $this->assertSame('value', $date['key']);
181
182
        $reflection1 = new \ReflectionMethod(
183
            '\Oro\Bundle\IntegrationBundle\Tests\Unit\Stub\TestConnector',
184
            'getStatusData'
185
        );
186
187
        $reflection1->setAccessible(true);
188
        $result = $reflection1->invoke($connector, 'key', 'value');
189
190
        $this->assertSame('value', $result);
191
    }
192
}
193