Completed
Push — master ( 35c3d1...7a49df )
by Benjamin
02:02
created

AgentTest::testExecute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 16
nc 4
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(20);
24
        $this->localTestUrl = 'http://localhost:8000/echo.php';
25
    }
26
27
    public function testNewRequest()
28
    {
29
        // We set the default parameters
30
        $this->agent->url = $this->localTestUrl;
31
        $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...
32
        $this->agent->headers = ['a' => 'a'];
33
        $this->agent->timeout = 5;
34
        $this->agent->options = [CURLOPT_BINARYTRANSFER => true];
35
        $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...
36
        });
37
38
        $r = $this->agent->newRequest();
39
40
        // Check that they were transferred properly to the newly created Request
41
        $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...
42
        $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...
43
        $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...
44
        $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...
45
        $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...
46
    }
47
48
    public function testExecute()
49
    {
50
        $called = 0;
51
52
        $this->agent->addListener(function (RequestInterface $req) use (&$called) {
53
            $this->assertInstanceOf('CurlX\RequestInterface', $req);
54
            $called++;
55
        });
56
57
        $r = [];
58
        $this->agent->url = $this->localTestUrl;
59
60
        for ($i = 0; $i < 20; $i++) {
61
            $r[] = $this->agent->newRequest();
62
        }
63
        $this->assertEquals($this->agent->url, $r[0]->url);
64
65
        $this->agent->execute();
66
67
        $this->assertEquals(20, $called);
68
69
        foreach ($r as $key => $req) {
70
            $this->assertNotNull($req->response);
71
            $this->assertJson($req->response);
72
            $this->assertArrayHasKey('server', json_decode($req->response, true));
73
        }
74
    }
75
}
76