ClientTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 144
Duplicated Lines 9.72 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 13
dl 14
loc 144
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testSendSuccess() 7 42 1
A testSendInvalidResponse() 7 45 2
A testSendFailedRequest() 0 31 2
A getTestReceipt() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET;
4
5
use DateTimeImmutable;
6
use Exception;
7
use PHPUnit\Framework\TestCase;
8
use SlevomatEET\Cryptography\CryptographyService;
9
use SlevomatEET\Driver\DriverRequestFailedException;
10
use SlevomatEET\Driver\SoapClientDriver;
11
12
class ClientTest extends TestCase
13
{
14
15
	/** @var Configuration */
16
	private $configuration;
17
18
	public function setUp(): void
19
	{
20
		$this->configuration = new Configuration('CZ00000019', '273', '/5546/RO24', EvidenceEnvironment::get(EvidenceEnvironment::PLAYGROUND), false);
21
	}
22
23
	public function testSendSuccess(): void
24
	{
25
		$client = new Client(
26
			$this->createMock(CryptographyService::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Slevo...tographyService::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SlevomatEET\Crypt...hy\CryptographyService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
			$this->configuration,
28
			$this->createMock(SoapClientDriver::class)
0 ignored issues
show
Documentation introduced by
$this->createMock(\Slevo...oapClientDriver::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SlevomatEET\Driver\SoapClientDriver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
		);
30
31
		$soapClient = $this->createMock(SoapClient::class);
32
		(function () use ($soapClient): void {
33
			$this->soapClient = $soapClient;
0 ignored issues
show
Bug introduced by
The property soapClient does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
		})->call($client);
35
36
		$receipt = $this->getTestReceipt();
37
38
		$responseData = (object) [
39
			'Hlavicka' => (object) [
40
				'uuid_zpravy' => '12345',
41
				'dat_prij' => '2016-11-21T08:00:00Z',
42
			],
43
			'Potvrzeni' => (object) [
44
				'fik' => '888-88-88-8888-ff',
45
			],
46
		];
47
48
		$soapClient->expects($this->once())
49
			->method('OdeslaniTrzby')
50 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...
51
				$this->assertArrayHasKey('Hlavicka', $request);
52
				$this->assertArrayHasKey('Data', $request);
53
				$this->assertArrayHasKey('KontrolniKody', $request);
54
55
				return true;
56
			}))
57
			->willReturn($responseData);
58
59
		$response = $client->send($receipt);
60
61
		$this->assertTrue($response->isValid());
62
		$this->assertFalse($response->isTest());
63
		$this->assertSame($response->getFik(), '888-88-88-8888-ff');
64
	}
65
66
	public function testSendInvalidResponse(): void
67
	{
68
		$client = new Client(
69
			$this->createMock(CryptographyService::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Slevo...tographyService::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SlevomatEET\Crypt...hy\CryptographyService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
			$this->configuration,
71
			$this->createMock(SoapClientDriver::class)
0 ignored issues
show
Documentation introduced by
$this->createMock(\Slevo...oapClientDriver::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SlevomatEET\Driver\SoapClientDriver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
		);
73
74
		$soapClient = $this->createMock(SoapClient::class);
75
		(function () use ($soapClient): void {
76
			$this->soapClient = $soapClient;
77
		})->call($client);
78
79
		$receipt = $this->getTestReceipt();
80
81
		$responseData = (object) [
82
			'Hlavicka' => (object) [
83
				'uuid_zpravy' => '12345',
84
				'dat_odmit' => '2016-11-21T08:00:00Z',
85
			],
86
			'Chyba' => (object) [],
87
		];
88
89
		$soapClient->expects($this->once())
90
			->method('OdeslaniTrzby')
91 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...
92
				$this->assertArrayHasKey('Hlavicka', $request);
93
				$this->assertArrayHasKey('Data', $request);
94
				$this->assertArrayHasKey('KontrolniKody', $request);
95
96
				return true;
97
			}))
98
			->willReturn($responseData);
99
100
		try {
101
			$client->send($receipt);
102
			$this->fail('Expected exception was not thrown');
103
		} catch (InvalidResponseReceivedException $e) {
104
			$response = $e->getResponse();
105
			$this->assertFalse($response->isValid());
106
			$this->assertFalse($response->isTest());
107
			$this->expectException(InvalidResponseWithoutFikException::class);
108
			$response->getFik();
109
		}
110
	}
111
112
	public function testSendFailedRequest(): void
113
	{
114
		$soapClientDriver = $this->createMock(SoapClientDriver::class);
115
		$soapClientDriver->expects($this->once())
116
			->method('send')
117
			->with(
118
				$this->stringStartsWith('<?xml'),
119
				$this->identicalTo('https://pg.eet.cz:443/eet/services/EETServiceSOAP/v3'),
120
				$this->identicalTo('http://fs.mfcr.cz/eet/OdeslaniTrzby')
121
			)
122
			->willThrowException(new DriverRequestFailedException(new Exception('Fail')));
123
		$cryptographyService = $this->createMock(CryptographyService::class);
124
		$cryptographyService->method('addWSESignature')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
			->willReturnArgument(0);
126
127
		$client = new Client(
128
			$cryptographyService,
0 ignored issues
show
Documentation introduced by
$cryptographyService is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SlevomatEET\Crypt...hy\CryptographyService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
			$this->configuration,
130
			$soapClientDriver
0 ignored issues
show
Documentation introduced by
$soapClientDriver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SlevomatEET\Driver\SoapClientDriver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
		);
132
133
		$receipt = $this->getTestReceipt();
134
135
		try {
136
			$client->send($receipt);
137
			$this->fail('Expected exception was not thrown');
138
		} catch (FailedRequestException $e) {
139
			$request = $e->getRequest();
140
			$this->assertSame('', $request->getBkpCode());
141
		}
142
	}
143
144
	private function getTestReceipt(): Receipt
145
	{
146
		return new Receipt(
147
			true,
148
			'CZ683555118',
149
			'0/6460/ZQ42',
150
			new DateTimeImmutable('2016-11-01 00:30:12'),
151
			3411300
152
		);
153
	}
154
155
}
156