Completed
Pull Request — master (#217)
by Thomas
02:42
created

FetcherTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 37
loc 111
rs 10

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 Owncloud\Updater\Tests\Utils;
4
5
use Owncloud\Updater\Utils\Fetcher;
6
7
class FetcherTest extends \PHPUnit_Framework_TestCase {
8
9
	protected $httpClient;
10
	protected $locator;
11
	protected $configReader;
12
13
	public function setUp(){
14
		$this->httpClient = $this->getMockBuilder('GuzzleHttp\Client')
15
				->disableOriginalConstructor()
16
				->getMock()
17
		;
18
		$this->locator = $this->getMockBuilder('Owncloud\Updater\Utils\Locator')
19
				->disableOriginalConstructor()
20
				->getMock()
21
		;
22
		$this->configReader = $this->getMockBuilder('Owncloud\Updater\Utils\ConfigReader')
23
				->disableOriginalConstructor()
24
				->getMock()
25
		;
26
		
27
		$map = [
28
			['apps.core.installedat', '100500'],
29
			['apps.core.lastupdatedat', '500100'],
30
			['apps.core.OC_Channel', 'stable'],
31
			['system.version', '8.2.0.3'],
32
		];
33
34
		$this->configReader
35
				->method('getByPath')
36
				->will($this->returnValueMap($map))
37
		;
38
		$this->configReader
39
				->method('getEdition')
40
				->willReturn('')
41
		;
42
		$this->locator
43
				->method('getBuild')
44
				->willReturn('2015-03-09T13:29:12+00:00 8db687a1cddd13c2a6fb6b16038d20275bd31e17')
45
		;
46
	}
47
48
	public function testGetValidFeed(){
49
		$responseMock = $this->getResponseMock('<?xml version="1.0"?><owncloud>  <version>8.1.3.0</version><versionstring>ownCloud 8.1.3</versionstring>
50
  <url>https://download.owncloud.org/community/owncloud-8.1.3.zip</url>
51
  <web>https://doc.owncloud.org/server/8.1/admin_manual/maintenance/upgrade.html</web>
52
</owncloud>');
53
		$this->httpClient
54
				->method('get')
55
				->willReturn($responseMock)
56
		;
57
		$fetcher = new Fetcher($this->httpClient, $this->locator, $this->configReader);
58
		$feed = $fetcher->getFeed();
59
		$this->assertInstanceOf('Owncloud\Updater\Utils\Feed', $feed);
60
		$this->assertTrue($feed->isValid());
61
		$this->assertEquals('8.1.3.0', $feed->getVersion());
62
	}
63
64
65
	public function testGetEmptyFeed(){
66
		$responseMock = $this->getResponseMock('');
67
		$this->httpClient
68
				->method('get')
69
				->willReturn($responseMock)
70
		;
71
		$fetcher = new Fetcher($this->httpClient, $this->locator, $this->configReader);
72
		$feed = $fetcher->getFeed();
73
		$this->assertInstanceOf('Owncloud\Updater\Utils\Feed', $feed);
74
		$this->assertFalse($feed->isValid());
75
	}
76
77
	public function testGetGarbageFeed(){
78
		$responseMock = $this->getResponseMock('<!DOCTYPE html><html lang="en"> <head><meta charset="utf-8">');
79
		$this->httpClient
80
				->method('get')
81
				->willReturn($responseMock)
82
		;
83
		$fetcher = new Fetcher($this->httpClient, $this->locator, $this->configReader);
84
		$feed = $fetcher->getFeed();
85
		$this->assertInstanceOf('Owncloud\Updater\Utils\Feed', $feed);
86
		$this->assertFalse($feed->isValid());
87
	}
88
89
	private function getResponseMock($body){
90
		$bodyMock = $this->getMockBuilder('Owncloud\Updater\Tests\StreamInterface')
91
				->disableOriginalConstructor()
92
				->getMock()
93
		;
94
		$bodyMock
95
				->expects($this->any())
96
				->method('getContents')
97
				->willReturn($body)
98
		;
99
100
		$responseMock = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
101
				->disableOriginalConstructor()
102
				->getMock()
103
		;
104
		$responseMock
105
				->expects($this->any())
106
				->method('getStatusCode')
107
				->willReturn(200)
108
		;
109
		$responseMock
110
				->expects($this->any())
111
				->method('getBody')
112
				->willReturn($bodyMock)
113
		;
114
		return $responseMock;
115
	}
116
117
}
118