Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session autostart #733

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public function isStarted()
}

/**
* Starts session driver.
* Starts session driver if session is not started.
*
* Calling any action before visiting a page is an undefined behavior.
* The only supported method calls on a fresh driver are
* - resizeWindow()
* - maximizeWindow()
* - visit()
* - setRequestHeader()
* - setBasicAuth()
Expand All @@ -67,7 +69,9 @@ public function isStarted()
*/
public function start()
{
$this->driver->start();
if (!$this->isStarted()) {
$this->driver->start();
}
}

/**
Expand All @@ -92,6 +96,8 @@ public function restart()
*
* Calling any action before visiting a page is an undefined behavior.
* The only supported method calls on a fresh driver are
* - resizeWindow()
* - maximizeWindow()
* - visit()
* - setRequestHeader()
* - setBasicAuth()
Expand Down Expand Up @@ -140,11 +146,7 @@ public function getSelectorsHandler()
*/
public function visit($url)
{
// start session if needed
if (!$this->isStarted()) {
$this->start();
}

$this->start();
$this->driver->visit($url);
}

Expand Down Expand Up @@ -363,6 +365,7 @@ public function wait($time, $condition = 'false')
*/
public function resizeWindow($width, $height, $name = null)
{
$this->start();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, these actions are not defined as supported before visiting (see the phpdoc of start)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But @peterrehm is using them and they work perfectly. Otherwise there is no way to open window initially with given size so that upon page visiting we won't be resizing it again.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the list of Methods i the phpdoc. Is this what you meant?

$this->driver->resizeWindow($width, $height, $name);
}

Expand All @@ -373,6 +376,7 @@ public function resizeWindow($width, $height, $name = null)
*/
public function maximizeWindow($name = null)
{
$this->start();
$this->driver->maximizeWindow($name);
}
}
54 changes: 28 additions & 26 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,32 @@ public function testIsStarted()
$this->assertTrue($this->session->isStarted());
}

public function testStart()
public function testStartWithoutRunningSession()
{
$this->driver
->expects($this->once())
->method('isStarted')
->willReturn(false);

$this->driver->expects($this->once())
->method('start');

$this->session->start();
}

public function testStartWithRunningSession()
{
$this->driver
->expects($this->once())
->method('isStarted')
->willReturn(true);

$this->driver->expects($this->never())
->method('start');

$this->session->start();
}

public function testStop()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test must not be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the test as I added the tests for the start() method.
As start() is tested for both options I thought it is enough to test just that start()
is called in e.g. the visit method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, the test you kept describes the wrong interaction with the driver (it does not describe the usage of isStarted.

and I would keep the test ensuring we can visit without starting, as the test does not need to know that the code is calling $this->start() internally rather than directly calling the driver.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the test ensures that start is called:

    public function testVisit()
    {
        $this->driver
            ->expects($this->once())
            ->method('start');

        $this->driver
            ->expects($this->once())
            ->method('visit')
            ->with($url = 'some_url');

        $this->session->visit($url);
    }

The test with isStarted is not contained in testStartWithoutRunningSession and testStartWithRunningSession as the logic is moved to the start method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But when you call $this->visit, the isStarted method of the driver is still called. So your description of the interaction is incomplete (and btw, because it is incomplete, isStarted would return null, not false).

PHPUnit mocks don't complain about incomplete interaction specifications, which is why your test is passing (and a reason why I actually prefer Prophecy, but I haven't converted the whole Mink testsuite to it)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But IMHO it doesn't matter in this case as we are finde if started() is called and afterwards the visit() method is called. Otherwise tests would need to be duplicated for the other autostart methods as well.

If you want I can add this in.

Copy link
Member

@aik099 aik099 Jul 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof , @peterrehm , any updates? This seem to be last change to be made before merging.

P.S.
Change actually needs to be made on #732 instead and this PR rebased on top of it.

{
$this->driver->expects($this->once())
Expand All @@ -83,34 +101,10 @@ public function testRestart()
$this->session->restart();
}

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()
public function testVisit()
{
$this->driver
->expects($this->once())
->method('isStarted')
->willReturn(true);

$this->driver
->expects($this->never())
->method('start');

$this->driver
Expand Down Expand Up @@ -322,6 +316,10 @@ public function testWait()

public function testResizeWindow()
{
$this->driver
->expects($this->once())
->method('start');

$this->driver->expects($this->once())
->method('resizeWindow')
->with(800, 600, 'test');
Expand All @@ -331,6 +329,10 @@ public function testResizeWindow()

public function testMaximizeWindow()
{
$this->driver
->expects($this->once())
->method('start');

$this->driver->expects($this->once())
->method('maximizeWindow')
->with('test');
Expand Down