Completed
Push — master ( fa5ac7...eb2a5f )
by Jan
06:08
created

ClientTest::testSendSuccess()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 7
Ratio 16.67 %

Importance

Changes 0
Metric Value
dl 7
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET;
4
5
use SlevomatEET\Cryptography\CryptographyService;
6
use SlevomatEET\Driver\DriverRequestFailedException;
7
use SlevomatEET\Driver\SoapClientDriver;
8
9
class ClientTest extends \PHPUnit\Framework\TestCase
10
{
11
12
	/** @var \SlevomatEET\Cryptography\CryptographyService|\PHPUnit_Framework_MockObject_MockObject */
13
	private $cryptographyService;
14
15
	/** @var \SlevomatEET\Configuration|\PHPUnit_Framework_MockObject_MockObject */
16
	private $configuration;
17
18
	/** @var \SlevomatEET\Driver\SoapClientDriver|\PHPUnit_Framework_MockObject_MockObject */
19
	private $soapClientDriver;
20
21
	public function setUp()
22
	{
23
		$this->cryptographyService = $this->createMock(CryptographyService::class);
24
		$this->configuration = $configuration = new Configuration('CZ00000019', '273', '/5546/RO24', new EvidenceEnvironment(EvidenceEnvironment::PLAYGROUND), false);
0 ignored issues
show
Unused Code introduced by
$configuration is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25
		$this->soapClientDriver = $this->createMock(SoapClientDriver::class);
26
	}
27
28
	public function testSendSuccess()
29
	{
30
		$client = new Client(
31
			$this->cryptographyService,
32
			$this->configuration,
33
			$this->soapClientDriver
34
		);
35
36
		$soapClient = $this->createMock(SoapClient::class);
37
		(function () use ($soapClient) {
38
			$this->soapClient = $soapClient;
0 ignored issues
show
Bug introduced by
The property soapClient does not seem to exist. Did you mean soapClientDriver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
39
		})->call($client);
40
41
		$receipt = $this->getTestReceipt();
42
43
		$responseData = (object) [
44
			'Hlavicka' => (object) [
45
				'uuid_zpravy' => '12345',
46
				'dat_prij' => '2016-11-21T08:00:00Z',
47
			],
48
			'Potvrzeni' => (object) [
49
				'fik' => '888-88-88-8888-ff',
50
			],
51
		];
52
53
		$soapClient->expects($this->once())
54
			->method('OdeslaniTrzby')
55 View Code Duplication
			->with($this->callback(function (array $request) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
				$this->assertArrayHasKey('Hlavicka', $request);
57
				$this->assertArrayHasKey('Data', $request);
58
				$this->assertArrayHasKey('KontrolniKody', $request);
59
60
				return true;
61
			}))
62
			->willReturn($responseData);
63
64
		$response = $client->send($receipt);
65
66
		$this->assertTrue($response->isValid());
67
		$this->assertFalse($response->isTest());
68
		$this->assertSame($response->getFik(), '888-88-88-8888-ff');
69
	}
70
71
	public function testSendInvalidResponse()
72
	{
73
		$client = new Client(
74
			$this->cryptographyService,
75
			$this->configuration,
76
			$this->soapClientDriver
77
		);
78
79
		$soapClient = $this->createMock(SoapClient::class);
80
		(function () use ($soapClient) {
81
			$this->soapClient = $soapClient;
0 ignored issues
show
Bug introduced by
The property soapClient does not seem to exist. Did you mean soapClientDriver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
82
		})->call($client);
83
84
		$receipt = $this->getTestReceipt();
85
86
		$responseData = (object) [
87
			'Hlavicka' => (object) [
88
				'uuid_zpravy' => '12345',
89
				'dat_odmit' => '2016-11-21T08:00:00Z',
90
			],
91
			'Chyba' => (object) [],
92
		];
93
94
		$soapClient->expects($this->once())
95
			->method('OdeslaniTrzby')
96 View Code Duplication
			->with($this->callback(function (array $request) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
				$this->assertArrayHasKey('Hlavicka', $request);
98
				$this->assertArrayHasKey('Data', $request);
99
				$this->assertArrayHasKey('KontrolniKody', $request);
100
101
				return true;
102
			}))
103
			->willReturn($responseData);
104
105
		try {
106
			$client->send($receipt);
107
			$this->fail('Expected exception was not thrown');
108
		} catch (InvalidResponseReceivedException $e) {
109
			$response = $e->getResponse();
110
			$this->assertFalse($response->isValid());
111
			$this->assertFalse($response->isTest());
112
			$this->expectException(InvalidResponseWithoutFikException::class);
113
			$response->getFik();
114
		}
115
	}
116
117
	public function testSendFailedRequest()
118
	{
119
		$this->soapClientDriver->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in SlevomatEET\Driver\SoapClientDriver.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
120
			->method('send')
121
			->with(
122
				$this->stringStartsWith('<?xml'),
123
				$this->identicalTo('https://pg.eet.cz:443/eet/services/EETServiceSOAP/v3'),
124
				$this->identicalTo('http://fs.mfcr.cz/eet/OdeslaniTrzby')
125
			)
126
			->willThrowException(new DriverRequestFailedException(new \Exception('Fail')));
127
		$this->cryptographyService->method('addWSESignature')
128
			->willReturnArgument(0);
129
130
		$client = new Client(
131
			$this->cryptographyService,
132
			$this->configuration,
133
			$this->soapClientDriver
134
		);
135
136
		$receipt = $this->getTestReceipt();
137
138
		try {
139
			$client->send($receipt);
140
			$this->fail('Expected exception was not thrown');
141
		} catch (FailedRequestException $e) {
142
			$request = $e->getRequestData();
143
			$this->assertSame('', $request->getBkpCode());
144
		}
145
	}
146
147
	private function getTestReceipt(): Receipt
148
	{
149
		$receipt = new Receipt(
150
			true,
151
			'CZ683555118',
152
			'0/6460/ZQ42',
153
			new \DateTimeImmutable('2016-11-01 00:30:12'),
154
			3411300
155
		);
156
157
		return $receipt;
158
	}
159
160
}
161