3333use aik099 \PHPUnit \MinkDriver \IMinkDriverFactory ;
3434use Behat \Mink \Session ;
3535use ConsoleHelpers \CodeCoverageCompat \Driver \Driver ;
36+ use Behat \Mink \Exception \DriverException ;
3637
37- class BrowserTestCaseTest extends AbstractTestCase
38+ class BrowserTestCaseTest extends BrowserTestCase
3839{
3940
4041 use ExpectException;
42+ use TVerifyTestExpectations;
4143
4244 const BROWSER_CLASS = BrowserConfiguration::class;
4345
@@ -76,9 +78,14 @@ public function testSetSessionStrategyManager()
7678 $ manager = m::mock (self ::MANAGER_CLASS );
7779
7880 $ test_case = new WithoutBrowserConfig ('test name ' );
79- $ test_case ->setSessionStrategyManager ($ manager );
8081
81- $ property = new \ReflectionProperty ($ test_case , 'sessionStrategyManager ' );
82+ $ this ->assertSame (
83+ $ test_case ,
84+ $ test_case ->setSessionStrategyManager ($ manager ),
85+ 'The fluid interface doesn \'t work. '
86+ );
87+
88+ $ property = new \ReflectionProperty (BrowserTestCase::class, '_sessionStrategyManager ' );
8289 $ property ->setAccessible (true );
8390
8491 $ this ->assertSame ($ manager , $ property ->getValue ($ test_case ));
@@ -94,7 +101,7 @@ public function testSetBrowserCorrect()
94101 /** @var ISessionStrategy $session_strategy */
95102 $ session_strategy = m::mock (self ::SESSION_STRATEGY_INTERFACE );
96103
97- $ test_case = $ this ->getFixture ($ session_strategy );
104+ $ test_case = $ this ->getFixture (true , $ session_strategy );
98105
99106 $ browser = new BrowserConfiguration ($ this ->createDriverFactoryRegistry ());
100107
@@ -118,6 +125,7 @@ protected function createDriverFactoryRegistry()
118125 $ registry
119126 ->shouldReceive ('get ' )
120127 ->with ('selenium2 ' )
128+ ->once ()
121129 ->andReturn ($ driver_factory );
122130
123131 return $ registry ;
@@ -143,9 +151,10 @@ public function testGetBrowserNotSpecified()
143151 */
144152 public function testSetBrowserFromConfigurationDefault ()
145153 {
146- $ test_case = $ this ->getFixture ();
154+ $ test_case = $ this ->getFixture (true );
155+ $ test_case ->setBrowserConfigurationFactory ($ this ->browserConfigurationFactory );
147156
148- $ browser = $ this ->getBrowser (0 );
157+ $ browser = $ this ->getBrowserMock (0 );
149158 $ browser_config = array ('browserName ' => 'safari ' );
150159
151160 $ this ->browserConfigurationFactory
@@ -200,9 +209,9 @@ public function testGetSessionStrategySharing()
200209 *
201210 * @return void
202211 */
203- public function testGetSession ()
212+ public function testGetSessionWithAutoCreate ()
204213 {
205- $ browser = $ this ->getBrowser (0 );
214+ $ browser = $ this ->getBrowserMock (0 );
206215
207216 $ expected_session1 = m::mock (Session::class);
208217 $ expected_session2 = m::mock (Session::class);
@@ -211,7 +220,7 @@ public function testGetSession()
211220 $ session_strategy = m::mock (self ::SESSION_STRATEGY_INTERFACE );
212221 $ session_strategy ->shouldReceive ('session ' )->with ($ browser )->andReturn ($ expected_session1 , $ expected_session2 );
213222
214- $ test_case = $ this ->getFixture ($ session_strategy );
223+ $ test_case = $ this ->getFixture (true , $ session_strategy );
215224 $ test_case ->setBrowser ($ browser );
216225 $ test_case ->setTestResultObject (new TestResult ());
217226
@@ -224,20 +233,27 @@ public function testGetSession()
224233 $ this ->assertSame ($ session1 , $ session2 );
225234 }
226235
236+ public function testGetSessionWithoutAutoCreate ()
237+ {
238+ $ test_case = $ this ->getFixture (false );
239+
240+ $ this ->assertNull ($ test_case ->getSession (false ), 'The session was created upon request. ' );
241+ }
242+
227243 /**
228244 * Test description.
229245 *
230246 * @return void
231247 */
232248 public function testGetSessionDriverError ()
233249 {
234- $ browser = $ this ->getBrowser (1 );
250+ $ browser = $ this ->getBrowserMock (1 );
235251
236252 /** @var ISessionStrategy $session_strategy */
237253 $ session_strategy = m::mock (self ::SESSION_STRATEGY_INTERFACE );
238- $ session_strategy ->shouldReceive ('session ' )->andThrow (' \Behat\Mink\Exception\ DriverException' );
254+ $ session_strategy ->shouldReceive ('session ' )->andThrow (DriverException::class );
239255
240- $ test_case = $ this ->getFixture ($ session_strategy );
256+ $ test_case = $ this ->getFixture (true , $ session_strategy );
241257 $ test_case ->setBrowser ($ browser );
242258
243259 // On PHPUnit 5.x usage of expectException/expectExceptionMessage results in this test being marked as skipped.
@@ -264,7 +280,7 @@ public function testGetSessionDriverError()
264280 *
265281 * @return BrowserConfiguration
266282 */
267- protected function getBrowser ($ times )
283+ protected function getBrowserMock ($ times )
268284 {
269285 $ browser = m::mock (self ::BROWSER_CLASS );
270286 $ browser ->shouldReceive ('getHost ' )->times ($ times )->andReturn ('{hostname} ' );
@@ -280,7 +296,7 @@ protected function getBrowser($times)
280296 */
281297 public function testGetCollectCodeCoverageInformationSuccess ($ remote_coverage_script_url )
282298 {
283- $ test_case = $ this ->getFixture ();
299+ $ test_case = $ this ->getFixture (false );
284300
285301 $ test_result = new TestResult ();
286302
@@ -613,7 +629,7 @@ public function testOnTestFailed()
613629 /** @var ISessionStrategy $session_strategy */
614630 $ session_strategy = m::mock (self ::SESSION_STRATEGY_INTERFACE );
615631
616- $ test_case = $ this ->getFixture ($ session_strategy );
632+ $ test_case = $ this ->getFixture (false , $ session_strategy );
617633 $ test_case ->setSessionStrategy ($ session_strategy );
618634
619635 $ exception = new \Exception ('MSG_TEST ' );
@@ -632,7 +648,7 @@ public function testOnTestFailed()
632648 */
633649 public function testGetBrowserAliases ()
634650 {
635- $ test_case = $ this ->getFixture ();
651+ $ test_case = $ this ->getFixture (false );
636652
637653 $ this ->assertEmpty ($ test_case ->getBrowserAliases (), 'Browser configuration aliases are empty by default ' );
638654 }
@@ -647,12 +663,12 @@ protected function prepareForRun()
647663 /** @var ISessionStrategy $session_strategy */
648664 $ session_strategy = m::mock (self ::SESSION_STRATEGY_INTERFACE );
649665
650- $ test_case = $ this ->getFixture ($ session_strategy );
666+ $ test_case = $ this ->getFixture (true , $ session_strategy );
651667 $ test_case ->setName ('testSuccess ' );
652668
653669 $ session_strategy ->shouldReceive ('onTestEnded ' )->with ($ test_case )->once ();
654670
655- $ browser = $ this ->getBrowser (0 );
671+ $ browser = $ this ->getBrowserMock (0 );
656672 $ browser ->shouldReceive ('onTestSetup ' )->with ($ test_case )->once ();
657673 $ browser ->shouldReceive ('onTestEnded ' )->with ($ test_case , m::type (TestResult::class))->once ();
658674
@@ -664,22 +680,24 @@ protected function prepareForRun()
664680 /**
665681 * Returns test case fixture.
666682 *
683+ * @param boolean $return_strategy Session strategy manager would be asked for a strategy.
667684 * @param ISessionStrategy|null $session_strategy Session strategy.
668685 *
669686 * @return WithoutBrowserConfig
670687 */
671- protected function getFixture (ISessionStrategy $ session_strategy = null )
688+ protected function getFixture ($ return_strategy , ISessionStrategy $ session_strategy = null )
672689 {
673690 if ( !isset ($ session_strategy ) ) {
674691 $ session_strategy = m::mock (self ::SESSION_STRATEGY_INTERFACE );
675692 }
676693
677694 /** @var SessionStrategyManager $manager */
678695 $ manager = m::mock (self ::MANAGER_CLASS );
679- $ manager ->shouldReceive ('getSessionStrategy ' )->andReturn ($ session_strategy );
696+ $ manager ->shouldReceive ('getSessionStrategy ' )
697+ ->times ($ return_strategy ? 1 : 0 )
698+ ->andReturn ($ session_strategy );
680699
681700 $ test_case = new WithoutBrowserConfig ('test name ' );
682- $ test_case ->setBrowserConfigurationFactory ($ this ->browserConfigurationFactory );
683701 $ test_case ->setSessionStrategyManager ($ manager );
684702
685703 return $ test_case ;
0 commit comments