|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * gomoob/php-pushwoosh |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2017, GOMOOB SARL (http://gomoob.com) |
| 7 | + * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE.md file) |
| 8 | + */ |
| 9 | +namespace Gomoob\Pushwoosh\Curl; |
| 10 | + |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test case used to test the `CurlRequest` class. |
| 15 | + * |
| 16 | + * @author Baptiste GAILLARD (baptiste.gaillard@gomoob.com) |
| 17 | + * @group CurlRequestTest |
| 18 | + */ |
| 19 | +class CurlRequestTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Test method for the `__construct($url = null)` function. |
| 23 | + * |
| 24 | + * @group CurlRequestTest.testConstruct |
| 25 | + */ |
| 26 | + public function testConstruct() |
| 27 | + { |
| 28 | + // Test with no URL provided |
| 29 | + $curlRequest = new CurlRequest(); |
| 30 | + $this->assertNotNull($curlRequest); |
| 31 | + |
| 32 | + // Test with a valid URL provided |
| 33 | + $curlRequest = new CurlRequest('https://www.gomoob.com'); |
| 34 | + $this->assertNotNull($curlRequest); |
| 35 | + |
| 36 | + // Test with a bad URL provided |
| 37 | + try { |
| 38 | + new CurlRequest('bad'); |
| 39 | + $this->fail('Must have thrown an Exception !'); |
| 40 | + } catch (\Exception $ex) { |
| 41 | + $this->assertSame('Invalid URL provided \'bad\' !', $ex->getMessage()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test method for the `close()` function. |
| 47 | + * |
| 48 | + * @group CurlRequestTest.testClose |
| 49 | + */ |
| 50 | + public function testClose() |
| 51 | + { |
| 52 | + // Test with a CURL request without handle |
| 53 | + $curlRequest = new CurlRequest(); |
| 54 | + |
| 55 | + try { |
| 56 | + $curlRequest->close(); |
| 57 | + $this->fail('Must have thrown an Exception !'); |
| 58 | + } catch (\Exception $ex) { |
| 59 | + $this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage()); |
| 60 | + } |
| 61 | + |
| 62 | + // Test with a CURL request having a handle |
| 63 | + $curlRequest = new CurlRequest('https://www.gomoob.com'); |
| 64 | + $curlRequest->close(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test method for the `error()` function. |
| 69 | + * |
| 70 | + * @group CurlRequestTest.testError |
| 71 | + */ |
| 72 | + public function testError() |
| 73 | + { |
| 74 | + // Test with a CURL request without handle |
| 75 | + $curlRequest = new CurlRequest(); |
| 76 | + |
| 77 | + try { |
| 78 | + $curlRequest->error(); |
| 79 | + $this->fail('Must have thrown an Exception !'); |
| 80 | + } catch (\Exception $ex) { |
| 81 | + $this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage()); |
| 82 | + } |
| 83 | + |
| 84 | + // Test with a CURL request having a handle |
| 85 | + $curlRequest = new CurlRequest('https://www.gomoob.com'); |
| 86 | + $error = $curlRequest->error(); |
| 87 | + $this->assertSame('', $error); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test method for the `exec()` function. |
| 92 | + * |
| 93 | + * @group CurlRequestTest.testExec |
| 94 | + */ |
| 95 | + public function testExec() |
| 96 | + { |
| 97 | + // Test with a CURL request without handle |
| 98 | + $curlRequest = new CurlRequest(); |
| 99 | + |
| 100 | + try { |
| 101 | + $curlRequest->exec(); |
| 102 | + $this->fail('Must have thrown an Exception !'); |
| 103 | + } catch (\Exception $ex) { |
| 104 | + $this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage()); |
| 105 | + } |
| 106 | + |
| 107 | + // Test with a CURL request having a handle |
| 108 | + $curlRequest = new CurlRequest('https://www.gomoob.com'); |
| 109 | + |
| 110 | + // see: http://curl.haxx.se/docs/sslcerts.html |
| 111 | + $curlRequest->setOpt(CURLOPT_RETURNTRANSFER, true); |
| 112 | + $curlRequest->setOpt(CURLOPT_CAINFO, __DIR__ . '/../../../../../main/resources/cacert.pem'); |
| 113 | + $curlRequest->setOpt(CURLOPT_SSL_VERIFYHOST, 2); |
| 114 | + $curlRequest->setOpt(CURLOPT_SSL_VERIFYPEER, true); |
| 115 | + |
| 116 | + $result = $curlRequest->exec(); |
| 117 | + $this->assertRegexp('/GoMoob/', $result); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Test method for the `init($url = null)` function. |
| 122 | + * |
| 123 | + * @group CurlRequestTest.testInit |
| 124 | + */ |
| 125 | + public function testInit() |
| 126 | + { |
| 127 | + // Test with no URL provided and no CURL handle initilized after construct |
| 128 | + $curlRequest = new CurlRequest(); |
| 129 | + $curlRequest->init(); |
| 130 | + |
| 131 | + // Test with no URL provided and a CURL handle initilized after construct |
| 132 | + $curlRequest = new CurlRequest('https://www.gomoob.com'); |
| 133 | + $curlRequest->init(); |
| 134 | + |
| 135 | + // Test with a bad URL provided |
| 136 | + try { |
| 137 | + $curlRequest->init('bad'); |
| 138 | + $this->fail('Must have thrown an Exception !'); |
| 139 | + } catch (\Exception $ex) { |
| 140 | + $this->assertSame('Invalid URL provided \'bad\' !', $ex->getMessage()); |
| 141 | + } |
| 142 | + |
| 143 | + // Test with a good URL provided |
| 144 | + $curlRequest->init('https://www.gomoob.com'); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test method for the `getInfo()` function. |
| 149 | + * |
| 150 | + * @group CurlRequestTest.testGetInfo |
| 151 | + */ |
| 152 | + public function testGetInfo() |
| 153 | + { |
| 154 | + // Test with a CURL request without handle |
| 155 | + $curlRequest = new CurlRequest(); |
| 156 | + |
| 157 | + try { |
| 158 | + $curlRequest->getInfo(); |
| 159 | + $this->fail('Must have thrown an Exception !'); |
| 160 | + } catch (\Exception $ex) { |
| 161 | + $this->assertSame('No CURL handle found, did you call init ?', $ex->getMessage()); |
| 162 | + } |
| 163 | + |
| 164 | + // Test with no parameter provided |
| 165 | + $curlRequest = new CurlRequest('https://www.gomoob.com'); |
| 166 | + |
| 167 | + // see: http://curl.haxx.se/docs/sslcerts.html |
| 168 | + $curlRequest->setOpt(CURLOPT_RETURNTRANSFER, true); |
| 169 | + $curlRequest->setOpt(CURLOPT_CAINFO, __DIR__ . '/../../../../../main/resources/cacert.pem'); |
| 170 | + $curlRequest->setOpt(CURLOPT_SSL_VERIFYHOST, 2); |
| 171 | + $curlRequest->setOpt(CURLOPT_SSL_VERIFYPEER, true); |
| 172 | + |
| 173 | + $result = $curlRequest->getInfo(); |
| 174 | + $this->assertSame('https://www.gomoob.com', $result); |
| 175 | + |
| 176 | + // Test with a parameter |
| 177 | + $result = $curlRequest->getInfo(CURLINFO_LOCAL_PORT); |
| 178 | + $this->assertSame(0, $result); |
| 179 | + } |
| 180 | +} |
0 commit comments