ClientTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Test;
  9. use JonnyW\PhantomJs\Client;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class ClientTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Setup tests
  19. *
  20. * @return void
  21. */
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $this->client = Client::getInstance();
  26. }
  27. /**
  28. * Test invalid URL's throw exception
  29. *
  30. * @dataProvider provideInvalidHosts
  31. *
  32. * @param string $host
  33. * @return void
  34. */
  35. public function testInvalidUrl($host)
  36. {
  37. $this->setExpectedException('JonnyW\\PhantomJs\\Exception\\InvalidUrlException');
  38. $this->client->open($host);
  39. }
  40. /**
  41. * Invalid host data providers
  42. *
  43. * @return array
  44. */
  45. public function provideInvalidHosts()
  46. {
  47. return array(
  48. array('invalid_url'),
  49. array('invalid_url.test')
  50. );
  51. }
  52. /**
  53. * Test exception is thrown when
  54. * PhantomJS executable cannot be run
  55. *
  56. * @return void
  57. */
  58. public function testBinNotExecutable()
  59. {
  60. $this->setExpectedException('JonnyW\\PhantomJs\\Exception\\NoPhantomJsException');
  61. $this->client->setPhantomJs('/path/does/not/exist/phantomjs');
  62. }
  63. /**
  64. * Test exception is thrown when capture
  65. * path is not writeable
  66. *
  67. * @return void
  68. */
  69. public function testPathNotWriteable()
  70. {
  71. $this->setExpectedException('JonnyW\\PhantomJs\\Exception\\NotWriteableException');
  72. $this->client->capture('http://google.com', 'path/does/not/exist/phantoms.png');
  73. }
  74. /**
  75. * Test open page
  76. *
  77. * @return void
  78. */
  79. public function testOpenPage()
  80. {
  81. $client = $this->getMock('JonnyW\PhantomJs\Client', array('request'));
  82. $response = $this->getMock('JonnyW\PhantomJs\Response', null, array(array('content' => 'test')));
  83. $client->expects($this->once())
  84. ->method('request')
  85. ->will($this->returnValue($response));
  86. $actual = $client->open('http://jonnyw.me');
  87. $this->assertSame($response, $actual);
  88. }
  89. /**
  90. * Test screen capture page
  91. *
  92. * @return void
  93. */
  94. public function testCapturePage()
  95. {
  96. $client = $this->getMock('JonnyW\PhantomJs\Client', array('request'));
  97. $response = $this->getMock('JonnyW\PhantomJs\Response', null, array(array('content' => 'test')));
  98. $client->expects($this->once())
  99. ->method('request')
  100. ->will($this->returnValue($response));
  101. $actual = $client->capture('http://jonnyw.me', '/tmp/testing.png');
  102. $this->assertSame($response, $actual);
  103. }
  104. /**
  105. * Test page is redirect
  106. *
  107. * @return void
  108. */
  109. public function testRedirectPage()
  110. {
  111. $client = $this->getMock('JonnyW\PhantomJs\Client', array('request'));
  112. $response = $this->getMock('JonnyW\PhantomJs\Response', null, array(array('content' => 'test', 'status' => 301, 'redirectUrl' => 'http://google.com')));
  113. $client->expects($this->once())
  114. ->method('request')
  115. ->will($this->returnValue($response));
  116. $actual = $client->open('http://jonnyw.me');
  117. $this->assertTrue($response->isRedirect());
  118. }
  119. }