Completed
Push — refactoring ( f58f49 )
by Josef
45:19
created

RequestFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 43.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 4
dl 24
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testCreateRequest() 0 24 1
A testMissingSignatureHeader() 11 11 2
A testMissingSignatureAlgorithmHeader() 13 13 2

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
2
3
namespace HelePartnerSyncApi\Request;
4
5
use HelePartnerSyncApi\Client;
6
use PHPUnit_Framework_TestCase;
7
8
class RequestFactoryTest extends PHPUnit_Framework_TestCase
9
{
10
11
	public function testCreateRequest()
12
	{
13
		$algo = 'sha1';
14
		$secret = 'secret';
15
		$arguments = array('value');
16
		$method = 'fooMethod';
17
		$data = array(
18
			'data' => $arguments,
19
			'expectedVersion' => Client::VERSION,
20
			'method' => $method,
21
		);
22
23
		$requestData = json_encode($data);
24
		$requestFactory = new RequestFactory($secret);
25
		$request = $requestFactory->createRequest($requestData, array(
26
			Client::HEADER_SIGNATURE => hash_hmac($algo, $requestData, $secret),
27
			Client::HEADER_SIGNATURE_ALGORITHM => 'sha1',
28
		));
29
30
		$this->assertInstanceOf('HelePartnerSyncApi\Request\Request', $request);
31
		$this->assertSame($arguments, $request->getData());
32
		$this->assertSame(Client::VERSION, $request->getExpectedVersion());
33
		$this->assertSame($method, $request->getMethod());
34
	}
35
36 View Code Duplication
	public function testMissingSignatureHeader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
	{
38
		$secret = 'secret';
39
		$requestFactory = new RequestFactory($secret);
40
41
		try {
42
			$requestFactory->createRequest(json_encode(array()), array());
43
		} catch (RequestException $e) {
44
			$this->assertSame('Missing X-Hele-Signature header in HTTP request', $e->getMessage());
45
		}
46
	}
47
48 View Code Duplication
	public function testMissingSignatureAlgorithmHeader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
49
	{
50
		$secret = 'secret';
51
		$requestFactory = new RequestFactory($secret);
52
53
		try {
54
			$requestFactory->createRequest(json_encode(array()), array(
55
				Client::HEADER_SIGNATURE => 'fooSignature',
56
			));
57
		} catch (RequestException $e) {
58
			$this->assertSame('Missing X-Hele-Signature-Algorithm header in HTTP request', $e->getMessage());
59
		}
60
	}
61
62
}
63