| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by PhpStorm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * User: lejla | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Date: 2016.02.10. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Time: 17:51 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace CurlX\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use CurlX\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use CurlX\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class RequestTest extends PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function testUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $request = new Request('http://url.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->assertEquals('http://url.com', $request->url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $request->url = 'http://url2.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->assertEquals('http://url2.com', $request->url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $request->url = 'badurl'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertEquals('http://url2.com', $request->url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testPostFieldsBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // No post values yet, so it should default to GET | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertArrayNotHasKey(CURLOPT_POST, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertEmpty($request->post_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // With post values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $post = ['username' => 'mike', 'password' => 'pass']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $request->post_data = $post; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertArrayHasKey(CURLOPT_POST, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->assertEquals($post, $request->post_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // Add more post fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $post2 = ['otherdata' => 'newvalue', 'username' => 'stacey']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $request->post_data = $post2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertArrayHasKey(CURLOPT_POST, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->assertEquals($post2 + $post, $request->post_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function testNotify() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $called1 = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $called2 = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $r1 = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $r2 = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $request->addListener(function(RequestInterface $var) use (&$called1, &$r1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $called1 = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $r1 = $var; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $request->addListener(function(RequestInterface $var) use (&$called2, &$r2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $called2 = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $r2 = $var; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $request->callBack([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertTrue($called1, 'Callback 1 was not notified on request completion!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->assertInstanceOf('CurlX\RequestInterface', $r1, 'Callback 1 did not receive the request object'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertTrue($called2, 'Callback 2 was not notified on request completion!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->assertInstanceOf('CurlX\RequestInterface', $r2, 'Callback 2 did not receive the request object'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function testHeaders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $header = ['a' => 'aaa', 'b' => 'bbb']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $normalHeader = ['a: aaa', 'b: bbb']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $request->headers = $header; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->assertEquals($header, $request->headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertArrayHasKey(CURLOPT_HTTPHEADER, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->assertEquals($normalHeader, $request->options[CURLOPT_HTTPHEADER]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $header2 = ['b' => 'BBB', 'c' => 'CCC']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $normalOfBothHeaders = ['b: BBB', 'c: CCC', 'a: aaa']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $request->headers = $header2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertEquals($header2 + $header, $request->headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->assertArrayHasKey(CURLOPT_HTTPHEADER, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->assertArraySubset($normalOfBothHeaders, $request->options[CURLOPT_HTTPHEADER]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function testOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $opt = [CURLOPT_CRLF => 'test', CURLOPT_AUTOREFERER => 'test']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $request->options = $opt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->assertArrayHasKey(CURLOPT_CRLF, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->assertArrayHasKey(CURLOPT_AUTOREFERER, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertArraySubset($opt, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $opt2 = [CURLOPT_AUTOREFERER => 'no-test', CURLOPT_BINARYTRANSFER => 'no-test']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $request->options = $opt2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertArrayHasKey(CURLOPT_CRLF, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertArrayHasKey(CURLOPT_AUTOREFERER, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->assertArrayHasKey(CURLOPT_BINARYTRANSFER, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->assertArraySubset($opt2 + $opt, $request->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     public function testHandle() | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         $request = new Request('http://example.com'); | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |         $ch = $request->handle; | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |         $this->assertNotNull($ch); | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         $info = curl_getinfo($ch); | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |         $this->assertEquals($request->url, $info['url']); | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | } |