AssertLegacyTraitTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
View source
<?php
namespace Drupal\Tests\Core\Assert;
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Session;
use Drupal\Component\Render\MarkupInterface;
use Drupal\FunctionalTests\AssertLegacyTrait;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\WebAssert;
use PHPUnit\Framework\ExpectationFailedException;
class AssertLegacyTraitTest extends UnitTestCase {
use AssertLegacyTrait;
protected $session;
protected $page;
protected $webAssert;
public function setUp() {
parent::setUp();
$this->page = $this
->prophesize(DocumentElement::class);
$this->session = $this
->prophesize(Session::class);
$this->session
->getPage()
->willReturn($this->page
->reveal());
$this->webAssert = $this
->prophesize(WebAssert::class);
}
public function testAssertUniqueText() {
$this->page
->getText()
->willReturn('foo bar bar');
$this
->assertUniqueText('foo');
}
public function testAssertUniqueTextFail() {
$this->page
->getText()
->willReturn('foo bar bar');
$this
->expectException(ExpectationFailedException::class);
$this
->assertUniqueText('bar');
}
public function testAssertUniqueTextUnknown() {
$this->page
->getText()
->willReturn('foo bar bar');
$this
->expectException(ExpectationFailedException::class);
$this
->assertUniqueText('alice');
}
public function testAssertUniqueTextMarkup() {
$this->page
->getText()
->willReturn('foo bar bar');
$markupObject = $this
->prophesize(MarkupInterface::class);
$markupObject
->__toString()
->willReturn('foo');
$this
->assertUniqueText($markupObject
->reveal());
}
public function testAssertNoUniqueText() {
$this->page
->getText()
->willReturn('foo bar bar');
$this
->assertNoUniqueText('bar');
}
public function testAssertNoUniqueTextFail() {
$this->page
->getText()
->willReturn('foo bar bar');
$this
->expectException(ExpectationFailedException::class);
$this
->assertNoUniqueText('foo');
}
public function testAssertNoUniqueTextUnknown() {
$this->page
->getText()
->willReturn('foo bar bar');
$this
->expectException(ExpectationFailedException::class);
$this
->assertNoUniqueText('alice');
}
public function testAssertNoUniqueTextMarkup() {
$this->page
->getText()
->willReturn('foo bar bar');
$markupObject = $this
->prophesize(MarkupInterface::class);
$markupObject
->__toString()
->willReturn('bar');
$this
->assertNoUniqueText($markupObject
->reveal());
}
public function testAssertOptionSelected() {
$option_field = $this
->prophesize(NodeElement::class);
$option_field
->hasAttribute('selected')
->willReturn(TRUE);
$this->webAssert
->optionExists('myselect', 'two')
->willReturn($option_field
->reveal());
$this
->assertOptionSelected('myselect', 'two');
}
public function testAssertOptionSelectedFail() {
$option_field = $this
->prophesize(NodeElement::class);
$option_field
->hasAttribute('selected')
->willReturn(FALSE);
$this->webAssert
->optionExists('myselect', 'two')
->willReturn($option_field
->reveal());
$this
->expectException(ExpectationFailedException::class);
$this
->assertOptionSelected('myselect', 'two');
}
public function testAssertNoPattern() {
$this->webAssert
->responseNotMatches('/.*foo$/')
->shouldBeCalled();
$this
->assertNoPattern('/.*foo$/');
}
public function testAssertNoCacheTag() {
$this->webAssert
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'some-cache-tag')
->shouldBeCalled();
$this
->assertNoCacheTag('some-cache-tag');
}
protected function getSession() {
return $this->session
->reveal();
}
public function assertSession($name = NULL) {
return $this->webAssert
->reveal();
}
}