ClientTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Client instance
  19. *
  20. * @var JonnyW\PhantomJs\Client
  21. */
  22. protected $client;
  23. /**
  24. * Setup tests
  25. *
  26. * @return void
  27. */
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->client = Client::getInstance();
  32. }
  33. /**
  34. * Test get factory instance
  35. *
  36. * @return void
  37. */
  38. public function testMessageFactoryInstance()
  39. {
  40. $factory = $this->client->getMessageFactory();
  41. $this->assertInstanceOf('JonnyW\PhantomJs\Message\FactoryInterface', $factory);
  42. }
  43. /**
  44. * Test exception is thrown when
  45. * PhantomJS executable cannot be run
  46. *
  47. * @return void
  48. */
  49. public function testBinNotExecutable()
  50. {
  51. $this->setExpectedException('JonnyW\PhantomJs\Exception\NoPhantomJsException');
  52. $this->client->setPhantomJs('/path/does/not/exist/phantomjs');
  53. }
  54. /**
  55. * Test exception is thrown when capture
  56. * path is not writeable
  57. *
  58. * @return void
  59. */
  60. public function testPathNotWriteable()
  61. {
  62. $this->setExpectedException('JonnyW\PhantomJs\Exception\NotWriteableException');
  63. $request = $this->getMock('JonnyW\PhantomJs\Message\Request', null, array('method' => 'GET', 'url' => 'http://jonnyw.me'));
  64. $response = $this->getMock('JonnyW\PhantomJs\Message\Response', null);
  65. $this->client->send($request, $response, 'path/does/not/exist/phantoms.png');
  66. }
  67. /**
  68. * Test open page
  69. *
  70. * @return void
  71. */
  72. public function testOpenPage()
  73. {
  74. $client = $this->getMock('JonnyW\PhantomJs\Client', array('request'));
  75. $request = $this->getMock('JonnyW\PhantomJs\Message\Request', null, array('method' => 'GET', 'url' => 'http://jonnyw.me'));
  76. $response = $this->getMock('JonnyW\PhantomJs\Message\Response', null);
  77. $client->expects($this->once())
  78. ->method('request')
  79. ->will($this->returnValue($response));
  80. $actual = $client->send($request, $response);
  81. $this->assertSame($response, $actual);
  82. }
  83. /**
  84. * Test screen capture page
  85. *
  86. * @return void
  87. */
  88. public function testCapturePage()
  89. {
  90. $client = $this->getMock('JonnyW\PhantomJs\Client', array('request'));
  91. $request = $this->getMock('JonnyW\PhantomJs\Message\Request', null, array('method' => 'GET', 'url' => 'http://jonnyw.me'));
  92. $response = $this->getMock('JonnyW\PhantomJs\Message\Response', null);
  93. $client->expects($this->once())
  94. ->method('request')
  95. ->will($this->returnValue($response));
  96. $actual = $client->send($request, $response, '/tmp/testing.png');
  97. $this->assertSame($response, $actual);
  98. }
  99. /**
  100. * Test page is redirect
  101. *
  102. * @return void
  103. */
  104. public function testRedirectPage()
  105. {
  106. $client = $this->getMock('JonnyW\PhantomJs\Client', array('request'));
  107. $request = $this->getMock('JonnyW\PhantomJs\Message\Request', null, array('method' => 'GET', 'url' => 'http://jonnyw.me'));
  108. $response = $this->getMock('JonnyW\PhantomJs\Message\Response', array('getStatus'));
  109. $client->expects($this->once())
  110. ->method('request')
  111. ->will($this->returnValue($response));
  112. $response->expects($this->once())
  113. ->method('getStatus')
  114. ->will($this->returnValue(301));
  115. $actual = $client->send($request, $response);
  116. $this->assertTrue($actual->isRedirect());
  117. }
  118. }