Skip to content

Commit

Permalink
Merge pull request minkphp#705 from aik099/678-lazy-starting-of-sessi…
Browse files Browse the repository at this point in the history
…ons-feat

Auto-start session only upon 1st "->visit(...)" method call
  • Loading branch information
stof authored Feb 6, 2017
2 parents bb28774 + acf5fb1 commit 9ea1ceb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
11 changes: 2 additions & 9 deletions src/Mink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
24 changes: 0 additions & 24 deletions tests/MinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand All @@ -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());
Expand Down
30 changes: 29 additions & 1 deletion tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9ea1ceb

Please sign in to comment.