1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ip2c\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Ip2c\Http\Http; |
6
|
|
|
use Prophecy\Argument; |
7
|
|
|
|
8
|
|
|
class HttpTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** @var Http */ |
12
|
|
|
private $sut; |
13
|
|
|
|
14
|
|
|
public function testShouldBeInstantiated() |
15
|
|
|
{ |
16
|
|
|
$this->sut = new Http($this->libCurlReturnData()); |
17
|
|
|
$this->assertInstanceOf('Ip2c\Http\Http', $this->sut); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function testShouldReturnData() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$this->sut = new Http($this->libCurlReturnData()); |
23
|
|
|
$returnedData = $this->sut->request('http://fake.request.mock'); |
24
|
|
|
|
25
|
|
|
$this->assertTrue($returnedData == 'someData'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @expectedException \Exception |
30
|
|
|
* @expectedExceptionCode 1 |
31
|
|
|
* @expectedExceptionMessage error |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function testShouldThrowException() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->sut = new Http($this->libCurlReturnsNothing()); |
36
|
|
|
$returnedData = $this->sut->request('http://fake.request.mock'); |
37
|
|
|
|
38
|
|
|
$this->assertTrue($returnedData == 'someData'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
private function libCurlReturnData() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$libCurl = $this->prophesize('Ip2c\Curl\LibCurl'); |
44
|
|
|
|
45
|
|
|
$libCurl->init(Argument::any())->willReturn(null); |
46
|
|
|
$libCurl->setOpt(Argument::any(), Argument::any(), Argument::any())->willReturn(null); |
47
|
|
|
$libCurl->exec(Argument::any())->willReturn('someData'); |
48
|
|
|
$libCurl->errno(Argument::any())->willReturn(0); |
49
|
|
|
$libCurl->close(Argument::any())->willReturn(null); |
50
|
|
|
|
51
|
|
|
return $libCurl->reveal(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
private function libCurlReturnsNothing() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$libCurl = $this->prophesize('Ip2c\Curl\LibCurl'); |
57
|
|
|
|
58
|
|
|
$libCurl->init(Argument::any())->willReturn(null); |
59
|
|
|
$libCurl->setOpt(Argument::any(), Argument::any(), Argument::any())->willReturn(null); |
60
|
|
|
$libCurl->exec(Argument::any())->willReturn(''); |
61
|
|
|
$libCurl->errno(Argument::any())->willReturn(1); |
62
|
|
|
$libCurl->error(Argument::any())->willReturn('error'); |
63
|
|
|
$libCurl->close(Argument::any())->willReturn(null); |
64
|
|
|
|
65
|
|
|
return $libCurl->reveal(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.