Completed
Push — master ( bc3bf5...3f770a )
by smiley
02:50
created

RequestTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 32.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 8
c 12
b 0
f 0
lcom 1
cbo 5
dl 33
loc 101
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 15 15 1
A testInstanceWithoutArgsCoverage() 0 3 1
A fetchDataProvider() 0 7 1
A testFetchWithCA() 9 9 1
A testFetchNoCA() 9 9 1
A testFetchUrlSchemeException() 0 3 1
A testResponseNoCurlException() 0 3 1
A testResponseGetMagicFieldException() 0 3 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
2
/**
3
 *
4
 * @filesource   RequestTest.php
5
 * @created      13.02.2016
6
 * @package      chillerlan\TinyCurlTest
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2016 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\TinyCurlTest;
13
14
use chillerlan\TinyCurl\Request;
15
use chillerlan\TinyCurl\RequestOptions;
16
use chillerlan\TinyCurl\Response\Response;
17
use chillerlan\TinyCurl\URL;
18
use stdClass;
19
20
class RequestTest extends \PHPUnit_Framework_TestCase{
21
22
	const GW2_APIKEY = '39519066-20B0-5545-9AE2-71109410A2CAF66348DA-50F7-4ACE-9363-B38FD8EE1881';
23
	const GW2_ACC_ID = 'A9EAD53E-4157-E111-BBF3-78E7D1936222';
24
	const GW2_GUILD  = '75FD83CF-0C45-4834-BC4C-097F93A487AF';
25
	const GW2_CHARS  = 'Skin Receiver';
26
27
	/**
28
	 * @var \chillerlan\TinyCurl\Request
29
	 */
30
	protected $requestWithCA;
31
32
	/**
33
	 * @var \chillerlan\TinyCurl\Request
34
	 */
35
	protected $requestNoCA;
36
37
	/**
38
	 * @var \chillerlan\TinyCurl\Response\ResponseInterface
39
	 */
40
	protected $response;
41
42 View Code Duplication
	protected function setUp(){
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...
43
44
		$co = [
45
			CURLOPT_HTTPHEADER => ['Authorization: Bearer '.self::GW2_APIKEY]
46
		];
47
48
		$o1 = new RequestOptions;
49
		$o1->curl_options = $co;
50
		$o1->ca_info = __DIR__.'/test-cacert.pem';
51
		$this->requestWithCA = new Request($o1);
52
53
		$o2 = new RequestOptions;
54
		$o2->curl_options = $co;
55
		$this->requestNoCA = new Request($o2);
56
	}
57
58
	public function testInstanceWithoutArgsCoverage(){
59
		$this->assertInstanceOf(Request::class, new Request); // HA HA.
60
	}
61
62
	public function fetchDataProvider(){
63
		return [
64
			['https://api.guildwars2.com/v2/account', []],
65
			['https://api.guildwars2.com/v2/account?lang=de', []],
66
			['https://api.guildwars2.com/v2/account?lang=de', ['lang' => 'fr']],
67
		];
68
	}
69
70
	/**
71
	 * @dataProvider fetchDataProvider
72
	 */
73 View Code Duplication
	public function testFetchWithCA($url, array $params){
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...
74
		$this->response = $this->requestWithCA->fetch(new URL($url, $params));
75
76
		$this->assertEquals(0, $this->response->error->code);
0 ignored issues
show
Documentation introduced by
The property error does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
		$this->assertEquals(200, $this->response->info->http_code);
0 ignored issues
show
Documentation introduced by
The property info does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
		$this->assertEquals('*', $this->response->headers->{'access-control-allow-origin'});
0 ignored issues
show
Bug introduced by
The property headers does not seem to exist. Did you mean response_headers?

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...
79
		$this->assertEquals(self::GW2_ACC_ID, $this->response->json->id);
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
		$this->assertEquals('application/json; charset=utf-8', $this->response->body->content_type);
0 ignored issues
show
Documentation introduced by
The property body does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
	}
82
83
	/**
84
	 * @dataProvider fetchDataProvider
85
	 */
86 View Code Duplication
	public function testFetchNoCA($url, array $params){
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...
87
		$this->response = $this->requestNoCA->fetch(new URL($url, $params));
88
89
		$this->assertEquals(0, $this->response->error->code);
0 ignored issues
show
Documentation introduced by
The property error does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
		$this->assertEquals(200, $this->response->info->http_code);
0 ignored issues
show
Documentation introduced by
The property info does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
		$this->assertEquals('*', $this->response->headers->{'access-control-allow-origin'});
0 ignored issues
show
Bug introduced by
The property headers does not seem to exist. Did you mean response_headers?

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...
92
		$this->assertEquals(self::GW2_ACC_ID, $this->response->json->id);
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
		$this->assertEquals('application/json; charset=utf-8', $this->response->body->content_type);
0 ignored issues
show
Documentation introduced by
The property body does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
	}
95
96
	/**
97
	 * @expectedException \chillerlan\TinyCurl\RequestException
98
	 * @expectedExceptionMessage $url
99
	 */
100
	public function testFetchUrlSchemeException(){
101
		$this->requestWithCA->fetch(new URL('htps://whatever.wat'));
102
	}
103
104
	/**
105
	 * @expectedException \chillerlan\TinyCurl\Response\ResponseException
106
	 * @expectedExceptionMessage $curl
107
	 */
108
	public function testResponseNoCurlException(){
109
		$this->response = new Response(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a resource.

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...
110
	}
111
112
	/**
113
	 * @expectedException \chillerlan\TinyCurl\Response\ResponseException
114
	 * @expectedExceptionMessage !$property: foobar
115
	 */
116
	public function testResponseGetMagicFieldException(){
117
		var_dump($this->requestWithCA->fetch(new URL('https://api.guildwars2.com/v2/build'))->foobar);
0 ignored issues
show
Documentation introduced by
The property foobar does not exist on object<chillerlan\TinyCurl\Response\Response>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Security Debugging Code introduced by
var_dump($this->requestW...m/v2/build'))->foobar); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
118
	}
119
120
}
121