Completed
Push — master ( dd8eb0...35c3d1 )
by Benjamin
01:59
created

AgentTest::testNewRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace CurlX\Tests;
4
5
use CurlX\Agent;
6
use CurlX\RequestInterface;
7
use PHPUnit_Framework_TestCase;
8
9
/**
10
 * Class AgentTest
11
 * @package CurlX\Tests
12
 *
13
 * @property Agent $agent
14
 * @property string $localTestUrl
15
 */
16
class AgentTest extends PHPUnit_Framework_TestCase
17
{
18
    protected $agent;
19
    protected $localTestUrl;
20
21
    public function setUp()
22
    {
23
        $this->agent = new Agent(50);
24
        $this->localTestUrl = 'http://localhost:8000/echo.php';
25
    }
26
27
    public function tearDown()
28
    {
29
30
    }
31
32
    public function testNewRequest()
33
    {
34
        // We set the default parameters
35
        $this->agent->url = $this->localTestUrl;
36
        $this->agent->post_data = ['a' => 'a'];
0 ignored issues
show
Documentation introduced by
The property post_data does not exist on object<CurlX\Agent>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
37
        $this->agent->headers = ['a' => 'a'];
38
        $this->agent->timeout = 5;
39
        $this->agent->options = [CURLOPT_BINARYTRANSFER => true];
40
        $this->agent->addListener(function(RequestInterface $r) {});
0 ignored issues
show
Unused Code introduced by
The parameter $r is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
42
        $r = $this->agent->newRequest();
43
44
        // Check that they were transferred properly to the newly created Request
45
        $this->assertEquals($this->agent->url, $r->url);
0 ignored issues
show
Bug introduced by
Accessing url on the interface CurlX\RequestInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
46
        $this->assertEquals($this->agent->post_data, $r->post_data);
0 ignored issues
show
Documentation introduced by
The property post_data does not exist on object<CurlX\Agent>. 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...
Bug introduced by
Accessing post_data on the interface CurlX\RequestInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
47
        $this->assertEquals($this->agent->headers, $r->headers);
0 ignored issues
show
Bug introduced by
Accessing headers on the interface CurlX\RequestInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
48
        $this->assertEquals($this->agent->timeout, $r->timeout);
0 ignored issues
show
Bug introduced by
Accessing timeout on the interface CurlX\RequestInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
        $this->assertEquals($this->agent->options, $r->options);
0 ignored issues
show
Bug introduced by
Accessing options on the interface CurlX\RequestInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
    }
51
52
    public function testSomething()
53
    {
54
        $called = 0;
55
56
        $this->agent->addListener(function(RequestInterface $req) use (&$called) {
57
            $this->assertInstanceOf('CurlX\RequestInterface', $req);
58
            $called++;
59
        });
60
61
        $r = [];
62
        $this->agent->url = $this->localTestUrl;
63
64
        for($i = 0; $i<20; $i++) {
65
            $r[] = $this->agent->newRequest();
66
        }
67
        $this->assertEquals($this->agent->url, $r[0]->url);
68
69
        $this->agent->execute();
70
71
        $this->assertEquals(20, $called);
72
73
        foreach($r as $key => $req) {
74
            $this->assertNotNull($req->response);
75
            $this->assertJson($req->response);
76
            $this->assertArrayHasKey('server', json_decode($req->response, true));
77
        }
78
    }
79
}