Skip to content

Commit

Permalink
Type hint mock object
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jul 26, 2022
1 parent 1c2b8e0 commit a04a649
Show file tree
Hide file tree
Showing 46 changed files with 111 additions and 156 deletions.
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertCheckedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertCheckedCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,8 +33,7 @@ public function testRun(bool $isSelected, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('isSelected')->willReturn($isSelected);
$this->element->expects($this->once())->method('isSelected')->willReturn($isSelected);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,7 +42,7 @@ public function testRun(bool $isSelected, ?Exception $exception): void
&& 'css selector' === $selector->getMechanism()
&& '.term-and-condition' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->command->run('css=.term-and-condition', null, $this->values, $this->driver);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertEditableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertEditableCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,7 +33,6 @@ public function testRun(bool $enabled, bool $readonly, ?Exception $exception): v
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -43,13 +41,13 @@ public function testRun(bool $enabled, bool $readonly, ?Exception $exception): v
&& 'name' === $selector->getMechanism()
&& 'username' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->driver
->expects($this->once())
->method('executeScript')
->with(
'return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };',
[$element]
[$this->element]
)
->willReturn((object) ['enabled' => $enabled, 'readonly' => $readonly]);
$this->command->run('name=username', null, $this->values, $this->driver);
Expand Down
4 changes: 1 addition & 3 deletions tests/Command/Assert/AssertElementNotPresentCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertElementNotPresentCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,7 +33,6 @@ public function testRun(int $count, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElements')
Expand All @@ -45,7 +43,7 @@ public function testRun(int $count, ?Exception $exception): void
&& '.cart' === $selector->getValue();
})
)
->willReturn(array_fill(0, $count, $element));
->willReturn(array_fill(0, $count, $this->element));
$this->command->run('css=.cart', null, $this->values, $this->driver);
}

Expand Down
4 changes: 1 addition & 3 deletions tests/Command/Assert/AssertElementPresentCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertElementPresentCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,7 +33,6 @@ public function testRun(int $count, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElements')
Expand All @@ -45,7 +43,7 @@ public function testRun(int $count, ?Exception $exception): void
&& '.cart' === $selector->getValue();
})
)
->willReturn(array_fill(0, $count, $element));
->willReturn(array_fill(0, $count, $this->element));
$this->command->run('css=.cart', null, $this->values, $this->driver);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertNotCheckedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertNotCheckedCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,8 +33,7 @@ public function testRun(bool $isSelected, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('isSelected')->willReturn($isSelected);
$this->element->expects($this->once())->method('isSelected')->willReturn($isSelected);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,7 +42,7 @@ public function testRun(bool $isSelected, ?Exception $exception): void
&& 'css selector' === $selector->getMechanism()
&& '.term-and-condition' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->command->run('css=.term-and-condition', null, $this->values, $this->driver);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertNotEditableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertNotEditableCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,7 +33,6 @@ public function testRun(bool $enabled, bool $readonly, ?Exception $exception): v
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -43,13 +41,13 @@ public function testRun(bool $enabled, bool $readonly, ?Exception $exception): v
&& 'name' === $selector->getMechanism()
&& 'username' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->driver
->expects($this->once())
->method('executeScript')
->with(
'return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };',
[$element]
[$this->element]
)
->willReturn((object) ['enabled' => $enabled, 'readonly' => $readonly]);
$this->command->run('name=username', null, $this->values, $this->driver);
Expand Down
5 changes: 2 additions & 3 deletions tests/Command/Assert/AssertNotSelectedLabelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,13 +43,13 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'partial link text' === $selector->getMechanism()
&& 'Language' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$option = $this->createMock(WebDriverElement::class);
$option->expects($this->once())->method('getText')->willReturn($actual);
$select = $this->createMock(WebDriverSelect::class);
$select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option);
$command = $this->createPartialMock(AssertNotSelectedLabelCommand::class, ['getSelect']);
$command->expects($this->once())->method('getSelect')->with($element)->willReturn($select);
$command->expects($this->once())->method('getSelect')->with($this->element)->willReturn($select);
$command->run('partialLinkText=Language', 'United Kingdom', $this->values, $this->driver);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Command/Assert/AssertNotSelectedValueCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,13 +43,13 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'partial link text' === $selector->getMechanism()
&& 'Language' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$option = $this->createMock(WebDriverElement::class);
$option->expects($this->once())->method('getAttribute')->with('value')->willReturn($actual);
$select = $this->createMock(WebDriverSelect::class);
$select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option);
$command = $this->createPartialMock(AssertNotSelectedValueCommand::class, ['getSelect']);
$command->expects($this->once())->method('getSelect')->with($element)->willReturn($select);
$command->expects($this->once())->method('getSelect')->with($this->element)->willReturn($select);
$command->run('partialLinkText=Language', 'en_GB', $this->values, $this->driver);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertNotTextCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertNotTextCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,8 +33,7 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('getText')->willReturn($actual);
$this->element->expects($this->once())->method('getText')->willReturn($actual);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,7 +42,7 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'xpath' === $selector->getMechanism()
&& '//h4[@href="#"]' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->command->run('xpath=//h4[@href="#"]', 'Welcome to our store', $this->values, $this->driver);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Command/Assert/AssertSelectedLabelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,13 +43,13 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'partial link text' === $selector->getMechanism()
&& 'Language' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$option = $this->createMock(WebDriverElement::class);
$option->expects($this->once())->method('getText')->willReturn($actual);
$select = $this->createMock(WebDriverSelect::class);
$select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option);
$command = $this->createPartialMock(AssertSelectedLabelCommand::class, ['getSelect']);
$command->expects($this->once())->method('getSelect')->with($element)->willReturn($select);
$command->expects($this->once())->method('getSelect')->with($this->element)->willReturn($select);
$command->run('partialLinkText=Language', 'United Kingdom', $this->values, $this->driver);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Command/Assert/AssertSelectedValueCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,13 +43,13 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'partial link text' === $selector->getMechanism()
&& 'Language' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$option = $this->createMock(WebDriverElement::class);
$option->expects($this->once())->method('getAttribute')->with('value')->willReturn($actual);
$select = $this->createMock(WebDriverSelect::class);
$select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option);
$command = $this->createPartialMock(AssertSelectedValueCommand::class, ['getSelect']);
$command->expects($this->once())->method('getSelect')->with($element)->willReturn($select);
$command->expects($this->once())->method('getSelect')->with($this->element)->willReturn($select);
$command->run('partialLinkText=Language', 'en_GB', $this->values, $this->driver);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertTextCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertTextCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,8 +33,7 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('getText')->willReturn($actual);
$this->element->expects($this->once())->method('getText')->willReturn($actual);
$this->driver
->expects($this->once())
->method('findElement')
Expand All @@ -44,7 +42,7 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'xpath' === $selector->getMechanism()
&& '//h4[@href="#"]' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->command->run('xpath=//h4[@href="#"]', 'Welcome to our store', $this->values, $this->driver);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Command/Assert/AssertValueCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Assert\AssertValueCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand Down Expand Up @@ -34,8 +33,7 @@ public function testRun(string $actual, ?Exception $exception): void
if ($exception) {
$this->expectExceptionObject($exception);
}
$element = $this->createMock(WebDriverElement::class);
$element
$this->element
->expects($this->once())
->method('getAttribute')
->with('value')
Expand All @@ -48,7 +46,7 @@ public function testRun(string $actual, ?Exception $exception): void
&& 'css selector' === $selector->getMechanism()
&& '.quality' === $selector->getValue();
}))
->willReturn($element);
->willReturn($this->element);
$this->command->run('css=.quality', '14', $this->values, $this->driver);
}

Expand Down
8 changes: 3 additions & 5 deletions tests/Command/Keyboard/SendKeysCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tienvx\Bundle\MbtBundle\Tests\Command\Keyboard;

use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\Keyboard\SendKeysCommand;
use Tienvx\Bundle\MbtBundle\Tests\Command\CommandTestCase;

Expand All @@ -27,14 +26,13 @@ protected function createCommand(): SendKeysCommand

public function testRun(): void
{
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('click')->willReturnSelf();
$element->expects($this->once())->method('sendKeys')->with('123');
$this->element->expects($this->once())->method('click')->willReturnSelf();
$this->element->expects($this->once())->method('sendKeys')->with('123');
$this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) {
return $selector instanceof WebDriverBy
&& 'css selector' === $selector->getMechanism()
&& '.quantity' === $selector->getValue();
}))->willReturn($element);
}))->willReturn($this->element);
$this->command->run('css=.quantity', '123', $this->values, $this->driver);
}

Expand Down
Loading

0 comments on commit a04a649

Please sign in to comment.