TestCase.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Behat\Mink\Tests\Driver;
  3. use Behat\Mink\Exception\UnsupportedDriverActionException;
  4. use Behat\Mink\Mink;
  5. use Behat\Mink\Session;
  6. use Behat\Mink\WebAssert;
  7. abstract class TestCase extends SkippingUnsupportedTestCase
  8. {
  9. /**
  10. * Mink session manager.
  11. *
  12. * @var Mink
  13. */
  14. private static $mink;
  15. /**
  16. * @var AbstractConfig
  17. */
  18. private static $config;
  19. /**
  20. * Initializes the test case.
  21. */
  22. public static function setUpBeforeClass()
  23. {
  24. if (null === self::$mink) {
  25. $session = new Session(self::getConfig()->createDriver());
  26. self::$mink = new Mink(array('sess' => $session));
  27. }
  28. }
  29. /**
  30. * @return AbstractConfig
  31. *
  32. * @throws \UnexpectedValueException if the global driver_config_factory returns an invalid object
  33. */
  34. private static function getConfig()
  35. {
  36. if (null === self::$config) {
  37. self::$config = call_user_func($GLOBALS['driver_config_factory']);
  38. if (!self::$config instanceof AbstractConfig) {
  39. throw new \UnexpectedValueException('The "driver_config_factory" global variable must return a \Behat\Mink\Tests\Driver\AbstractConfig.');
  40. }
  41. }
  42. return self::$config;
  43. }
  44. protected function checkRequirements()
  45. {
  46. if (null !== $message = self::getConfig()->skipMessage(get_class($this), $this->getName(false))) {
  47. $this->markTestSkipped($message);
  48. }
  49. parent::checkRequirements();
  50. }
  51. protected function tearDown()
  52. {
  53. if (null !== self::$mink) {
  54. self::$mink->resetSessions();
  55. }
  56. }
  57. /**
  58. * Returns session.
  59. *
  60. * @return Session
  61. */
  62. protected function getSession()
  63. {
  64. return self::$mink->getSession('sess');
  65. }
  66. /**
  67. * Returns assert session.
  68. *
  69. * @return WebAssert
  70. */
  71. protected function getAssertSession()
  72. {
  73. return self::$mink->assertSession('sess');
  74. }
  75. /**
  76. * @param string $id
  77. *
  78. * @return \Behat\Mink\Element\NodeElement
  79. */
  80. protected function findById($id)
  81. {
  82. return $this->getAssertSession()->elementExists('named', array('id', $id));
  83. }
  84. /**
  85. * Creates a new driver instance.
  86. *
  87. * This driver is not associated to a session. It is meant to be used for tests on the driver
  88. * implementation itself rather than test using the Mink API.
  89. *
  90. * @return \Behat\Mink\Driver\DriverInterface
  91. */
  92. protected function createDriver()
  93. {
  94. return self::getConfig()->createDriver();
  95. }
  96. /**
  97. * Map remote file path.
  98. *
  99. * @param string $file File path.
  100. *
  101. * @return string
  102. */
  103. protected function mapRemoteFilePath($file)
  104. {
  105. $realPath = realpath($file);
  106. if (false !== $realPath) {
  107. $file = $realPath;
  108. }
  109. return self::getConfig()->mapRemoteFilePath($file);
  110. }
  111. /**
  112. * @param string $path
  113. *
  114. * @return string
  115. */
  116. protected function pathTo($path)
  117. {
  118. return rtrim(self::getConfig()->getWebFixturesUrl(), '/').'/'.ltrim($path, '/');
  119. }
  120. /**
  121. * Waits for a condition to be true, considering than it is successful for drivers not supporting wait().
  122. *
  123. * @param int $time
  124. * @param string $condition A JS condition to evaluate
  125. *
  126. * @return bool
  127. *
  128. * @see \Behat\Mink\Session::wait()
  129. */
  130. protected function safePageWait($time, $condition)
  131. {
  132. try {
  133. return $this->getSession()->wait($time, $condition);
  134. } catch (UnsupportedDriverActionException $e) {
  135. return true;
  136. }
  137. }
  138. }