| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- <?php
- /*
- * This file is part of the php-phantomjs.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace JonnyW\PhantomJs\Tests\Unit\Message;
- use JonnyW\PhantomJs\Message\CaptureRequest;
- use JonnyW\PhantomJs\Message\RequestInterface;
- /**
- * PHP PhantomJs
- *
- * @author Jon Wenmoth <contact@jonnyw.me>
- */
- class CaptureRequestTest extends \PHPUnit_Framework_TestCase
- {
- /** +++++++++++++++++++++++++++++++++++ **/
- /** ++++++++++++++ TESTS ++++++++++++++ **/
- /** +++++++++++++++++++++++++++++++++++ **/
- /**
- * Test get type returns capture request type.
- *
- * @access public
- * @return void
- */
- public function testGetTypeReturnsCaptureRequestType()
- {
- $captureRequest = $this->getCaptureRequest();
- $this->assertSame(RequestInterface::REQUEST_TYPE_CAPTURE, $captureRequest->getType());
- }
- /**
- * Test URL can be set via constructor.
- *
- * @access public
- * @return void
- */
- public function testUrlCanBeSetViaConstructor()
- {
- $url = 'http://test.com';
- $captureRequest = $this->getCaptureRequest($url);
- $this->assertSame($url, $captureRequest->getUrl());
- }
- /**
- * Test method can be set via constructor.
- *
- * @access public
- * @return void
- */
- public function testMethodCanBeSetViaConstructor()
- {
- $method = 'GET';
- $captureRequest = $this->getCaptureRequest(null, $method);
- $this->assertSame($method, $captureRequest->getMethod());
- }
- /**
- * Test timeout can be set via constructor.
- *
- * @access public
- * @return void
- */
- public function testTimeoutCanBeSetViaConstructor()
- {
- $timeout = 100000;
- $captureRequest = $this->getCaptureRequest('http://test.com', 'GET', $timeout);
- $this->assertSame($timeout, $captureRequest->getTimeout());
- }
- /**
- * Test set method throws invalid method
- * exception if an invalid method is set
- *
- * @access public
- * @return void
- */
- public function testSetMethodThrowsInvalidMethodExceptionIfAnInvalidMethodIsSet()
- {
- $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidMethodException');
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('INVALID_METHOD');
- }
- /**
- * Test set method throws invalid method
- * exception if an invalid method is set
- * via constructor
- *
- * @access public
- * @return void
- */
- public function testSetMethodThrowsInvalidMethodExceptionIfAnInvalidMethodIsSetViaConstructor()
- {
- $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidMethodException');
- $this->getCaptureRequest('http://test.com', 'INVALID_METHOD');
- }
- /**
- * Test set capture dimensions sets
- * rect width.
- *
- * @access public
- * @return void
- */
- public function testSetCaptureDimensionsSetsRectWidth()
- {
- $width = 100;
- $height = 200;
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setCaptureDimensions($width, $height);
- $this->assertSame($width, $captureRequest->getRectWidth());
- }
- /**
- * Test set capture dimensions sets
- * rect height.
- *
- * @access public
- * @return void
- */
- public function testSetCaptureDimensionsSetsRectHeight()
- {
- $width = 100;
- $height = 200;
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setCaptureDimensions($width, $height);
- $this->assertSame($height, $captureRequest->getRectHeight());
- }
- /**
- * Test set capture dimensions sets
- * rect top.
- *
- * @access public
- * @return void
- */
- public function testSetCaptureDimensionsSetsRectTop()
- {
- $width = 100;
- $height = 200;
- $top = 50;
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setCaptureDimensions($width, $height, $top);
- $this->assertSame($top, $captureRequest->getRectTop());
- }
- /**
- * Test set capture dimensions sets
- * rect left.
- *
- * @access public
- * @return void
- */
- public function testSetCaptureDimensionsSetsRectLeft()
- {
- $width = 100;
- $height = 200;
- $left = 50;
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setCaptureDimensions($width, $height, 0, $left);
- $this->assertSame($left, $captureRequest->getRectLeft());
- }
- /**
- * Test set URL throws invalid
- * URL exception if an invalid
- * URL is set.
- *
- * @access public
- * @return void
- */
- public function testSetUrlThrowsInvalidUrlExceptionIfAnInvalidUrlIsSet()
- {
- $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setUrl('\\AnInvalidUrl');
- }
- /**
- * Test get URL returns URL without query
- * paramters if method is not HEAD or GET.
- *
- * @access public
- * @return void
- */
- public function testGetUrlReturnsUrlWithoutQueryParametersIfMethodIsNotHeadOrGet()
- {
- $url = 'http://test.com';
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('POST');
- $captureRequest->setUrl($url);
- $captureRequest->setRequestData($data);
- $this->assertSame($url, $captureRequest->getUrl());
- }
- /**
- * Test get URL returns URL with query
- * parameters if method is GET.
- *
- * @access public
- * @return void
- */
- public function testGetUrlReturnsUrlWithQueryParametersIfMethodIsGet()
- {
- $url = 'http://test.com';
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('GET');
- $captureRequest->setUrl($url);
- $captureRequest->setRequestData($data);
- $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
- $this->assertSame($expectedUrl, $captureRequest->getUrl());
- }
- /**
- * Test get URL returns URL with query
- * parameters if method is HEAD.
- *
- * @access public
- * @return void
- */
- public function testGetUrlReturnsUrlWithQueryParametersIfMethodIsHead()
- {
- $url = 'http://test.com';
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('HEAD');
- $captureRequest->setUrl($url);
- $captureRequest->setRequestData($data);
- $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
- $this->assertSame($expectedUrl, $captureRequest->getUrl());
- }
- /**
- * Test get URL returns URL with query
- * parameters if method is HEAD.
- *
- * @access public
- * @return void
- */
- public function testGetUrlAppendsQueryParametersIfUrlHasExistingQueryParameters()
- {
- $url = 'http://test.com?existing_param=Existing';
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('GET');
- $captureRequest->setUrl($url);
- $captureRequest->setRequestData($data);
- $expectedUrl = $url . '&test_param1=Testing1&test_param2=Testing2';
- $this->assertSame($expectedUrl, $captureRequest->getUrl());
- }
- /**
- * Test get body returns an empty
- * string if method is GET.
- *
- * @access public
- * @return void
- */
- public function testGetBodyReturnsAnEmptyStringIfMethodIsGet()
- {
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('GET');
- $captureRequest->setRequestData($data);
- $this->assertSame('', $captureRequest->getBody());
- }
- /**
- * Test get body returns and empty
- * string if method is HEAD.
- *
- * @access public
- * @return void
- */
- public function testGetBodyReturnsAnEmptyStringIfMethodIsHead()
- {
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('HEAD');
- $captureRequest->setRequestData($data);
- $this->assertSame('', $captureRequest->getBody());
- }
- /**
- * Test get body returns query string if
- * method is not HEAD or GET.
- *
- * @access public
- * @return void
- */
- public function testGetBodyReturnsQueryStringIfMethodIsNotHeadOrGet()
- {
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => 'Testing2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setMethod('POST');
- $captureRequest->setRequestData($data);
- $body = 'test_param1=Testing1&test_param2=Testing2';
- $this->assertSame($body, $captureRequest->getBody());
- }
- /**
- * Test get request data returns flattened
- * request data if flatten is set to true.
- *
- * @access public
- * @return void
- */
- public function testGetRequestDataReturnsFlattenedRequestDataIfFlattenIsSetToTrue()
- {
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => array(
- 'Testing2',
- 'Testing3'
- )
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setRequestData($data);
- $flatData = array(
- 'test_param1' => 'Testing1',
- 'test_param2[0]' => 'Testing2',
- 'test_param2[1]' => 'Testing3'
- );
- $this->assertSame($flatData, $captureRequest->getRequestData(true));
- }
- /**
- * Test get request data returns unflattened
- * request data if flatten is set to false
- *
- * @access public
- * @return void
- */
- public function testGetRequestDataReturnsUnflattenedRequestDataIfFlattenIsSetToFalse()
- {
- $data = array(
- 'test_param1' => 'Testing1',
- 'test_param2' => array(
- 'Testing2',
- 'Testing3'
- )
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setRequestData($data);
- $this->assertSame($data, $captureRequest->getRequestData(false));
- }
- /**
- * Test add headers merge headers with
- * existing headers.
- *
- * @access public
- * @return void
- */
- public function testAddHeadersMergesHeadersWithExistingHeaders()
- {
- $existingHeaders = array(
- 'Header1' => 'Header 1'
- );
- $newHeaders = array(
- 'Header2' => 'Header 2',
- 'Header3' => 'Header 3'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setHeaders($existingHeaders);
- $captureRequest->addHeaders($newHeaders);
- $expectedHeaders = array_merge($existingHeaders, $newHeaders);
- $this->assertSame($expectedHeaders, $captureRequest->getHeaders());
- }
- /**
- * Test get headers returns JSON encoded
- * headers if format is set to JSON.
- *
- * @access public
- * @return void
- */
- public function testGetHeadersReturnsJsonEncodedHeadersIfFormatIsSetToJson()
- {
- $headers = array(
- 'Header1' => 'Header 1',
- 'Header2' => 'Header 2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setHeaders($headers);
- $expectedHeaders = json_encode($headers);
- $this->assertSame($expectedHeaders, $captureRequest->getHeaders('json'));
- }
- /**
- * Test get headers returns headers as
- * array if format is not set to json
- *
- * @access public
- * @return void
- */
- public function testGetHeadersReturnsHeadersAsArrayIfFormatIsNotSetToJson()
- {
- $headers = array(
- 'Header1' => 'Header 1',
- 'Header2' => 'Header 2'
- );
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setHeaders($headers);
- $this->assertSame($headers, $captureRequest->getHeaders('default'));
- }
- /**
- * Test set capture file throws not
- * writable exception if file path
- * is not writable.
- *
- * @access public
- * @return void
- */
- public function testSetCaptureFileThrowsNotWriteableExceptionIfFilePathIsNotWriteable()
- {
- $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
- $invalidPath = '/invalid/path';
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setCaptureFile($invalidPath);
- }
- /**
- * Test get capture file returns capture
- * file if capture file is set.
- *
- * @access public
- * @return void
- */
- public function testGetCaptureFileReturnsCaptureFileIfCaptureFileIsSet()
- {
- $captureFile = sprintf('%s/test.jpg', sys_get_temp_dir());
- $captureRequest = $this->getCaptureRequest();
- $captureRequest->setCaptureFile($captureFile);
- $this->assertSame($captureFile, $captureRequest->getCaptureFile());
- }
- /** +++++++++++++++++++++++++++++++++++ **/
- /** ++++++++++ TEST ENTITIES ++++++++++ **/
- /** +++++++++++++++++++++++++++++++++++ **/
- /**
- * Get capture request instance.
- *
- * @access protected
- * @param string $url (default: null)
- * @param string $method (default: RequestInterface::METHOD_GET)
- * @param int $timeout (default: 5000)
- * @return \JonnyW\PhantomJs\Message\CaptureRequest
- */
- protected function getCaptureRequest($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
- {
- $captureRequest = new CaptureRequest($url, $method, $timeout);
- return $captureRequest;
- }
- }
|