2015-08-17 17:00:26 -07:00
< ? php
namespace Behat\Mink\Tests\Exception ;
use Behat\Mink\Exception\ExpectationException ;
class ExpectationExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testEmptyMessageAndPreviousException ()
{
2015-10-08 11:40:12 -07:00
$exception = new ExpectationException ( '' , $this -> getMock ( 'Behat\Mink\Driver\DriverInterface' ), new \Exception ( 'Something failed' ));
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 'Something failed' , $exception -> getMessage ());
}
public function testExceptionToString ()
{
$driver = $this -> getMock ( 'Behat\Mink\Driver\DriverInterface' );
2015-10-08 11:40:12 -07:00
$driver -> expects ( $this -> any ())
2015-08-17 17:00:26 -07:00
-> method ( 'getStatusCode' )
-> will ( $this -> returnValue ( 200 ));
2015-10-08 11:40:12 -07:00
$driver -> expects ( $this -> any ())
2015-08-17 17:00:26 -07:00
-> method ( 'getCurrentUrl' )
-> will ( $this -> returnValue ( 'http://localhost/test' ));
$html = " <html><head><title>Hello</title></head> \n <body> \n <h1>Hello world</h1> \n <p>Test</p> \n </body></html> " ;
2015-10-08 11:40:12 -07:00
$driver -> expects ( $this -> any ())
2015-08-17 17:00:26 -07:00
-> method ( 'getContent' )
-> will ( $this -> returnValue ( $html ));
$expected = <<< 'TXT'
Expectation failure
+-- [ HTTP / 1.1 200 | http :// localhost / test | % s ]
|
| < body >
| < h1 > Hello world </ h1 >
| < p > Test </ p >
| </ body >
|
TXT ;
$expected = sprintf ( $expected . ' ' , get_class ( $driver ));
2015-10-08 11:40:12 -07:00
$exception = new ExpectationException ( 'Expectation failure' , $driver );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( $expected , $exception -> __toString ());
}
public function testBigContent ()
{
$driver = $this -> getMock ( 'Behat\Mink\Driver\DriverInterface' );
2015-10-08 11:40:12 -07:00
$driver -> expects ( $this -> any ())
2015-08-17 17:00:26 -07:00
-> method ( 'getStatusCode' )
-> will ( $this -> returnValue ( 200 ));
2015-10-08 11:40:12 -07:00
$driver -> expects ( $this -> any ())
2015-08-17 17:00:26 -07:00
-> method ( 'getCurrentUrl' )
-> will ( $this -> returnValue ( 'http://localhost/test' ));
$body = str_repeat ( 'a' , 1001 - strlen ( '<body></body>' ));
$html = sprintf ( " <html><head><title>Hello</title></head> \n <body>%s</body></html> " , $body );
2015-10-08 11:40:12 -07:00
$driver -> expects ( $this -> any ())
2015-08-17 17:00:26 -07:00
-> method ( 'getContent' )
-> will ( $this -> returnValue ( $html ));
$expected = <<< 'TXT'
Expectation failure
+-- [ HTTP / 1.1 200 | http :// localhost / test | % s ]
|
| < body >% s </ b ...
|
TXT ;
$expected = sprintf ( $expected . ' ' , get_class ( $driver ), $body );
2015-10-08 11:40:12 -07:00
$exception = new ExpectationException ( 'Expectation failure' , $driver );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( $expected , $exception -> __toString ());
}
public function testExceptionWhileRenderingString ()
{
2015-10-08 11:40:12 -07:00
$driver = $this -> getMock ( 'Behat\Mink\Driver\DriverInterface' );
$driver -> expects ( $this -> any ())
-> method ( 'getContent' )
2015-08-17 17:00:26 -07:00
-> will ( $this -> throwException ( new \Exception ( 'Broken page' )));
2015-10-08 11:40:12 -07:00
$exception = new ExpectationException ( 'Expectation failure' , $driver );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 'Expectation failure' , $exception -> __toString ());
}
2015-10-08 11:40:12 -07:00
/**
* @ group legacy
*/
public function testConstructWithSession ()
2015-08-17 17:00:26 -07:00
{
2015-10-08 11:40:12 -07:00
$driver = $this -> getMock ( 'Behat\Mink\Driver\DriverInterface' );
$session = $this -> getMockBuilder ( 'Behat\Mink\Session' )
2015-08-17 17:00:26 -07:00
-> disableOriginalConstructor ()
-> getMock ();
2015-10-08 11:40:12 -07:00
$session -> expects ( $this -> any ())
-> method ( 'getDriver' )
-> will ( $this -> returnValue ( $driver ));
2015-08-17 17:00:26 -07:00
2015-10-08 11:40:12 -07:00
$exception = new ExpectationException ( '' , $session , new \Exception ( 'Something failed' ));
$this -> assertEquals ( 'Something failed' , $exception -> getMessage ());
2015-08-17 17:00:26 -07:00
}
}