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) { |
|
|
|
|
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); |
|
|
|
|
42
|
|
|
$this->assertEquals($this->agent->post_data, $r->post_data); |
|
|
|
|
43
|
|
|
$this->assertEquals($this->agent->headers, $r->headers); |
|
|
|
|
44
|
|
|
$this->assertEquals($this->agent->timeout, $r->timeout); |
|
|
|
|
45
|
|
|
$this->assertEquals($this->agent->options, $r->options); |
|
|
|
|
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
|
|
|
|
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.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.