annotate vendor/behat/mink/src/Exception/ExpectationException.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Mink package.
Chris@0 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 6 *
Chris@0 7 * For the full copyright and license information, please view the LICENSE
Chris@0 8 * file that was distributed with this source code.
Chris@0 9 */
Chris@0 10
Chris@0 11 namespace Behat\Mink\Exception;
Chris@0 12
Chris@0 13 use Behat\Mink\Driver\DriverInterface;
Chris@0 14 use Behat\Mink\Session;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Exception thrown for failed expectations.
Chris@0 18 *
Chris@0 19 * Some specialized child classes are available to customize the error rendering.
Chris@0 20 *
Chris@0 21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 22 */
Chris@0 23 class ExpectationException extends Exception
Chris@0 24 {
Chris@0 25 private $session;
Chris@0 26 private $driver;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Initializes exception.
Chris@0 30 *
Chris@0 31 * @param string $message optional message
Chris@0 32 * @param DriverInterface|Session $driver driver instance (or session for BC)
Chris@0 33 * @param \Exception|null $exception expectation exception
Chris@0 34 */
Chris@0 35 public function __construct($message, $driver, \Exception $exception = null)
Chris@0 36 {
Chris@0 37 if ($driver instanceof Session) {
Chris@0 38 @trigger_error('Passing a Session object to the ExpectationException constructor is deprecated as of Mink 1.7. Pass the driver instead.', E_USER_DEPRECATED);
Chris@0 39
Chris@0 40 $this->session = $driver;
Chris@0 41 $this->driver = $driver->getDriver();
Chris@0 42 } elseif (!$driver instanceof DriverInterface) {
Chris@0 43 // Trigger an exception as we cannot typehint a disjunction
Chris@0 44 throw new \InvalidArgumentException('The ExpectationException constructor expects a DriverInterface or a Session.');
Chris@0 45 } else {
Chris@0 46 $this->driver = $driver;
Chris@0 47 }
Chris@0 48
Chris@0 49 if (!$message && null !== $exception) {
Chris@0 50 $message = $exception->getMessage();
Chris@0 51 }
Chris@0 52
Chris@0 53 parent::__construct($message, 0, $exception);
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Returns exception message with additional context info.
Chris@0 58 *
Chris@0 59 * @return string
Chris@0 60 */
Chris@0 61 public function __toString()
Chris@0 62 {
Chris@0 63 try {
Chris@0 64 $pageText = $this->pipeString($this->trimString($this->getContext())."\n");
Chris@0 65 $string = sprintf("%s\n\n%s%s", $this->getMessage(), $this->getResponseInfo(), $pageText);
Chris@0 66 } catch (\Exception $e) {
Chris@0 67 return $this->getMessage();
Chris@0 68 }
Chris@0 69
Chris@0 70 return $string;
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Gets the context rendered for this exception.
Chris@0 75 *
Chris@0 76 * @return string
Chris@0 77 */
Chris@0 78 protected function getContext()
Chris@0 79 {
Chris@0 80 return $this->trimBody($this->driver->getContent());
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Returns driver.
Chris@0 85 *
Chris@0 86 * @return DriverInterface
Chris@0 87 */
Chris@0 88 protected function getDriver()
Chris@0 89 {
Chris@0 90 return $this->driver;
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Returns exception session.
Chris@0 95 *
Chris@0 96 * @return Session
Chris@0 97 *
Chris@0 98 * @deprecated since 1.7, to be removed in 2.0. Use getDriver and the driver API instead.
Chris@0 99 */
Chris@0 100 protected function getSession()
Chris@0 101 {
Chris@0 102 if (null === $this->session) {
Chris@0 103 throw new \LogicException(sprintf('The deprecated method %s cannot be used when passing a driver in the constructor', __METHOD__));
Chris@0 104 }
Chris@0 105
Chris@0 106 @trigger_error(sprintf('The method %s is deprecated as of Mink 1.7 and will be removed in 2.0. Use getDriver and the driver API instead.', __METHOD__), E_USER_DEPRECATED);
Chris@0 107
Chris@0 108 return $this->session;
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Prepends every line in a string with pipe (|).
Chris@0 113 *
Chris@0 114 * @param string $string
Chris@0 115 *
Chris@0 116 * @return string
Chris@0 117 */
Chris@0 118 protected function pipeString($string)
Chris@0 119 {
Chris@0 120 return '| '.strtr($string, array("\n" => "\n| "));
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Removes response header/footer, letting only <body /> content.
Chris@0 125 *
Chris@0 126 * @param string $string response content
Chris@0 127 *
Chris@0 128 * @return string
Chris@0 129 */
Chris@0 130 protected function trimBody($string)
Chris@0 131 {
Chris@0 132 $string = preg_replace(array('/^.*<body>/s', '/<\/body>.*$/s'), array('<body>', '</body>'), $string);
Chris@0 133
Chris@0 134 return $string;
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Trims string to specified number of chars.
Chris@0 139 *
Chris@0 140 * @param string $string response content
Chris@0 141 * @param int $count trim count
Chris@0 142 *
Chris@0 143 * @return string
Chris@0 144 */
Chris@0 145 protected function trimString($string, $count = 1000)
Chris@0 146 {
Chris@0 147 $string = trim($string);
Chris@0 148
Chris@0 149 if ($count < mb_strlen($string)) {
Chris@0 150 return mb_substr($string, 0, $count - 3).'...';
Chris@0 151 }
Chris@0 152
Chris@0 153 return $string;
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Returns response information string.
Chris@0 158 *
Chris@0 159 * @return string
Chris@0 160 */
Chris@0 161 protected function getResponseInfo()
Chris@0 162 {
Chris@0 163 $driver = basename(str_replace('\\', '/', get_class($this->driver)));
Chris@0 164
Chris@0 165 $info = '+--[ ';
Chris@0 166 try {
Chris@0 167 $info .= 'HTTP/1.1 '.$this->driver->getStatusCode().' | ';
Chris@0 168 } catch (UnsupportedDriverActionException $e) {
Chris@0 169 // Ignore the status code when not supported
Chris@0 170 }
Chris@0 171 $info .= $this->driver->getCurrentUrl().' | '.$driver." ]\n|\n";
Chris@0 172
Chris@0 173 return $info;
Chris@0 174 }
Chris@0 175 }