PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/phpunit/Helper/MinkTestCase.php

https://gitlab.com/x33n/ImpressPages
PHP | 203 lines | 122 code | 38 blank | 43 comment | 10 complexity | 54ebbb2a4e8884d34ad4774384c8f2a6 MD5 | raw file
  1. <?php
  2. namespace PhpUnit\Helper;
  3. class MinkTestCase extends \PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @var \Behat\Mink\Session
  7. */
  8. private $session;
  9. protected $timeout = 40;
  10. protected function setUp()
  11. {
  12. TestEnvironment::setup();
  13. }
  14. /**
  15. * @return \Behat\Mink\Session
  16. */
  17. public function session()
  18. {
  19. if (!$this->session) {
  20. $this->session = static::startSession(get_class($this) . '::' . $this->getName());
  21. }
  22. return $this->session;
  23. }
  24. /**
  25. * @return \Behat\Mink\Element\DocumentElement
  26. */
  27. public function page()
  28. {
  29. return $this->session()->getPage();
  30. }
  31. public function tearDown()
  32. {
  33. static::stopSession($this->session, $this->getStatus() == \PHPUnit_Runner_BaseTestRunner::STATUS_PASSED);
  34. $this->session = null;
  35. // TestEnvironment::cleanupFiles();
  36. }
  37. /**
  38. * @param string $cssSelector
  39. * @return \Behat\Mink\Element\NodeElement|null
  40. */
  41. protected function find($cssSelector)
  42. {
  43. return $this->waitForElementPresent($cssSelector);
  44. }
  45. /**
  46. * @return \Behat\Mink\Session
  47. */
  48. public static function startSession($testName)
  49. {
  50. // init Mink:
  51. if (getenv('TRAVIS')) {
  52. // $url = sprintf('http://%s:%s@localhost:4445/wd/hub', getenv('SAUCE_USERNAME'), getenv('SAUCE_ACCESS_KEY'));
  53. $url = sprintf(
  54. 'http://%s:%s@ondemand.saucelabs.com/wd/hub',
  55. getenv('SAUCE_USERNAME'),
  56. getenv('SAUCE_ACCESS_KEY')
  57. );
  58. $desiredCapabilities = array(
  59. 'name' => $testName,
  60. 'tunnel-identifier' => getenv('TRAVIS_JOB_NUMBER'),
  61. 'build' => getenv('TRAVIS_BUILD_NUMBER'),
  62. 'tags' => array(getenv('TRAVIS_PHP_VERSION'), 'CI')
  63. );
  64. $driver = new \Behat\Mink\Driver\Selenium2Driver(
  65. 'firefox',
  66. $desiredCapabilities,
  67. $url
  68. );
  69. } else {
  70. $driver = new \Behat\Mink\Driver\Selenium2Driver(
  71. 'firefox'
  72. );
  73. }
  74. $session = new \Behat\Mink\Session($driver);
  75. $session->start();
  76. // $session->wait();
  77. return $session;
  78. }
  79. /**
  80. * @param \Behat\Mink\Session $session
  81. * @param bool $hasPassed
  82. */
  83. public static function stopSession($session, $hasPassed)
  84. {
  85. if (!$session) {
  86. return;
  87. }
  88. if (getenv('TRAVIS')) {
  89. $sessionUrl = $session->getDriver()->getWebDriverSession()->getURL();
  90. $sauceSessionId = substr($sessionUrl, strrpos($sessionUrl, '/') + 1);
  91. $sauceReport = array(
  92. 'passed' => $hasPassed,
  93. );
  94. $json = json_encode($sauceReport);
  95. $template = 'curl -H "Content-Type:text/json" -s -X PUT -d \'%1$s\' http://%2$s:%3$s@saucelabs.com/rest/v1/%2$s/jobs/%4$s';
  96. $command = sprintf($template, $json, getenv('SAUCE_USERNAME'), getenv('SAUCE_ACCESS_KEY'), $sauceSessionId);
  97. // echo "\n---\n";
  98. // printf($template . "\n", $json, getenv('SAUCE_USERNAME'), 'SAUCE_ACCESS_KEY', $sauceSessionId);
  99. exec($command);
  100. // echo "\n---\n";
  101. }
  102. try {
  103. $session->stop();
  104. } catch (\Exception $e) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. /**
  110. * @param $lambda
  111. * @param int|null $timeout
  112. * @return mixed
  113. */
  114. public function spin($lambda, $timeout = null)
  115. {
  116. if (!$timeout) {
  117. $timeout = $this->timeout;
  118. }
  119. $start = microtime(true);
  120. do {
  121. try {
  122. $result = $lambda($this);
  123. } catch (Exception $e) {
  124. // do nothing
  125. }
  126. } while ($result === null && microtime(true) - $start < $timeout);
  127. return $result;
  128. }
  129. /**
  130. * @param string $cssSelector
  131. * @param int|null $timeout
  132. * @return \Behat\Mink\Element\NodeElement|null
  133. */
  134. public function waitForElementPresent($cssSelector, $timeout = null)
  135. {
  136. $context = $this;
  137. $result = $this->spin(
  138. function () use ($context, $cssSelector) {
  139. return $context->page()->find('css', $cssSelector);
  140. },
  141. $timeout
  142. );
  143. $this->assertNotEmpty($result, "Element $cssSelector not found");
  144. return $result;
  145. }
  146. /**
  147. * @param string $cssSelector
  148. * @param int|null $timeout
  149. * @return bool
  150. */
  151. public function waitForElementNotPresent($cssSelector, $timeout = null)
  152. {
  153. $context = $this;
  154. $result = $this->spin(
  155. function () use ($context, $cssSelector) {
  156. $element = $context->page()->find('css', $cssSelector);
  157. if (!$element) {
  158. return true;
  159. }
  160. }
  161. );
  162. $this->assertTrue($result, "Element $cssSelector is present");
  163. return $result ? true : false;
  164. }
  165. }