Failed Conditions
Pull Request — master (#790)
by Guilherme
10:36 queued 05:09
created

SoapClientFactoryTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 0
cts 86
cp 0
rs 10
c 0
b 0
f 0
wmc 12
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PROCERGS\LoginCidadao\NfgBundle\Test\Service;
12
13
use PHPUnit\Framework\AssertionFailedError;
14
use PHPUnit\Framework\TestCase;
15
use PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException;
16
use PROCERGS\LoginCidadao\NfgBundle\Service\SoapClientFactory;
17
use Psr\Log\LoggerInterface;
18
19
class SoapClientFactoryTest extends TestCase
20
{
21
    private function getBreaker($exception = null)
22
    {
23
        $breaker = $this->getMockBuilder('Eljam\CircuitBreaker\Breaker')
24
            ->setConstructorArgs(['some_name'])
25
            ->setMethods(['protect'])
26
            ->getMock();
27
28
        if ($exception instanceof \Exception) {
29
            $breaker->expects($this->once())->method('protect')
30
                ->willThrowException($exception);
31
        } else {
32
            $breaker->expects($this->once())->method('protect')
33
                ->willReturnCallback(function (\Closure $closure) {
34
                    return $closure();
35
                });
36
        }
37
38
        return $breaker;
39
    }
40
41
    public function testInvalidWsdl()
42
    {
43
        $factory = new SoapClientFactory();
44
45
        try {
46
            $client = $factory->createClient('invalid', true);
47
            $this->fail('Exception not thrown');
48
        } catch (AssertionFailedError $e) {
49
            var_dump($client);
50
            throw $e;
51
        } catch (\Exception $e) {
52
            $this->assertInstanceOf(NfgServiceUnavailableException::class, $e);
53
            $this->assertInstanceOf('SoapFault', $e->getPrevious());
54
        }
55
    }
56
57
    public function testOpenClosedCircuitBreaker()
58
    {
59
        $factory = new SoapClientFactory();
60
        $factory->setCircuitBreaker($this->getBreaker());
61
62
        try {
63
            $factory->createClient('invalid', true);
64
        } catch (\Exception $e) {
65
            $this->assertInstanceOf('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException', $e);
66
            $this->assertInstanceOf('SoapFault', $e->getPrevious());
67
        }
68
    }
69
70
    public function testSuccess()
71
    {
72
        /** @var SoapClientFactory|\PHPUnit_Framework_MockObject_MockObject $factory */
73
        $factory = $this->getMockBuilder('PROCERGS\LoginCidadao\NfgBundle\Service\SoapClientFactory')
74
            ->setMethods(['instantiateSoapClient'])
75
            ->getMock();
76
        $factory->expects($this->atLeastOnce())
77
            ->method('instantiateSoapClient')
78
            ->willReturn($this->getMockBuilder('\SoapClient')->disableOriginalConstructor()->getMock());
79
        $factory->setCircuitBreaker($this->getBreaker());
80
81
        $client = $factory->createClient('invalid', true);
82
        $this->assertInstanceOf('\SoapClient', $client);
83
    }
84
85
    public function testCircuitBreakerOpen()
86
    {
87
        $factory = new SoapClientFactory();
88
        $factory->setCircuitBreaker($this->getBreaker(new NfgServiceUnavailableException()));
89
90
        try {
91
            $factory->createClient('invalid', true);
92
        } catch (\Exception $e) {
93
            $this->assertInstanceOf('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException', $e);
94
            $this->assertNull($e->getPrevious());
95
        }
96
    }
97
98
    public function testLogging()
99
    {
100
        $successFactory = $this->getValidFactory();
101
102
        /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */
103
        $logger = $this->createMock('Psr\Log\LoggerInterface');
104
        $logger->expects($this->atLeastOnce())->method('info');
105
106
        $successFactory->setLogger($logger);
107
        $successFactory->createClient('valid', true);
108
109
        try {
110
            $failureFactory = new SoapClientFactory();
111
            $failureFactory->setLogger($logger);
112
            $failureFactory->createClient('invalid', true);
113
        } catch (\Exception $e) {
114
            // success
115
        }
116
    }
117
118
    /**
119
     * @return SoapClientFactory|\PHPUnit_Framework_MockObject_MockObject
120
     */
121
    private function getValidFactory()
122
    {
123
        /** @var SoapClientFactory|\PHPUnit_Framework_MockObject_MockObject $factory */
124
        $factory = $this->getMockBuilder('PROCERGS\LoginCidadao\NfgBundle\Service\SoapClientFactory')
125
            ->setMethods(['instantiateSoapClient'])
126
            ->getMock();
127
        $factory->expects($this->atLeastOnce())
128
            ->method('instantiateSoapClient')
129
            ->willReturn($this->getMockBuilder('\SoapClient')->disableOriginalConstructor()->getMock());
130
        $factory->setCircuitBreaker($this->getBreaker());
131
132
        return $factory;
133
    }
134
}
135