From acf5fb1ec70b7de4902daf75301356702a26e6da Mon Sep 17 00:00:00 2001 From: Alexander Obuhovich Date: Fri, 17 Jun 2016 23:16:58 +0300 Subject: [PATCH] Auto-start session only upon 1st "->visit(...)" method call --- src/Mink.php | 11 ++--------- src/Session.php | 7 ++++++- tests/MinkTest.php | 24 ------------------------ tests/SessionTest.php | 30 +++++++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/Mink.php b/src/Mink.php index 333bfbdec..955558bb7 100644 --- a/src/Mink.php +++ b/src/Mink.php @@ -100,7 +100,7 @@ public function getDefaultSessionName() } /** - * Returns registered session by it's name or active one and automatically starts it if required. + * Returns registered session by it's name or default one. * * @param string $name session name * @@ -110,14 +110,7 @@ public function getDefaultSessionName() */ public function getSession($name = null) { - $session = $this->locateSession($name); - - // start session if needed - if (!$session->isStarted()) { - $session->start(); - } - - return $session; + return $this->locateSession($name); } /** diff --git a/src/Session.php b/src/Session.php index c8815f958..d8107724c 100644 --- a/src/Session.php +++ b/src/Session.php @@ -134,12 +134,17 @@ public function getSelectorsHandler() } /** - * Visit specified URL. + * Visit specified URL and automatically start session if not already running. * * @param string $url url of the page */ public function visit($url) { + // start session if needed + if (!$this->isStarted()) { + $this->start(); + } + $this->driver->visit($url); } diff --git a/tests/MinkTest.php b/tests/MinkTest.php index 11d6466ae..bb945020b 100644 --- a/tests/MinkTest.php +++ b/tests/MinkTest.php @@ -67,14 +67,6 @@ public function testNotStartedSession() { $session = $this->getSessionMock(); - $session - ->expects($this->once()) - ->method('isStarted') - ->will($this->returnValue(false)); - $session - ->expects($this->once()) - ->method('start'); - $this->mink->registerSession('mock_session', $session); $this->assertSame($session, $this->mink->getSession('mock_session')); @@ -83,22 +75,6 @@ public function testNotStartedSession() $this->mink->getSession('not_registered'); } - public function testGetAlreadyStartedSession() - { - $session = $this->getSessionMock(); - - $session - ->expects($this->once()) - ->method('isStarted') - ->will($this->returnValue(true)); - $session - ->expects($this->never()) - ->method('start'); - - $this->mink->registerSession('mock_session', $session); - $this->assertSame($session, $this->mink->getSession('mock_session')); - } - public function testSetDefaultSessionName() { $this->assertNull($this->mink->getDefaultSessionName()); diff --git a/tests/SessionTest.php b/tests/SessionTest.php index 3e0104a71..f89705ae7 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -83,8 +83,36 @@ public function testRestart() $this->session->restart(); } - public function testVisit() + public function testVisitWithoutRunningSession() { + $this->driver + ->expects($this->once()) + ->method('isStarted') + ->willReturn(false); + + $this->driver + ->expects($this->once()) + ->method('start'); + + $this->driver + ->expects($this->once()) + ->method('visit') + ->with($url = 'some_url'); + + $this->session->visit($url); + } + + public function testVisitWithRunningSession() + { + $this->driver + ->expects($this->once()) + ->method('isStarted') + ->willReturn(true); + + $this->driver + ->expects($this->never()) + ->method('start'); + $this->driver ->expects($this->once()) ->method('visit')