AgentTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testNewRequest() 0 21 1
B testExecute() 0 27 3
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'];
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
        $this->assertInstanceOf('CurlX\RequestInterface', $r);
40
41
        // Check that they were transferred properly to the newly created Request
42
        $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...
43
        $this->assertEquals($this->agent->post_data, $r->post_data);
0 ignored issues
show
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...
44
        $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...
45
        $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...
46
        $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...
47
    }
48
49
    public function testExecute()
50
    {
51
        $called = 0;
52
53
        $this->agent->addListener(function (RequestInterface $req) use (&$called) {
54
            $this->assertInstanceOf('CurlX\RequestInterface', $req);
55
            $called++;
56
        });
57
58
        $r = [];
59
        $this->agent->url = $this->localTestUrl;
60
61
        for ($i = 0; $i < 20; $i++) {
62
            $r[] = $this->agent->newRequest();
63
        }
64
        $this->assertEquals($this->agent->url, $r[0]->url);
65
66
        $this->agent->execute();
67
68
        $this->assertEquals(20, $called);
69
70
        foreach ($r as $key => $req) {
71
            $this->assertNotNull($req->response);
72
            $this->assertJson($req->response);
73
            $this->assertArrayHasKey('server', json_decode($req->response, true));
74
        }
75
    }
76
}
77