3 namespace Drupal\Tests\Core\Assert;
5 use Behat\Mink\Element\DocumentElement;
6 use Behat\Mink\Element\NodeElement;
7 use Behat\Mink\Session;
8 use Drupal\Component\Render\MarkupInterface;
9 use Drupal\FunctionalTests\AssertLegacyTrait;
10 use Drupal\Tests\UnitTestCase;
11 use Drupal\Tests\WebAssert;
12 use PHPUnit_Framework_ExpectationFailedException;
15 * @coversDefaultClass \Drupal\FunctionalTests\AssertLegacyTrait
19 class AssertLegacyTraitTest extends UnitTestCase {
21 use AssertLegacyTrait;
24 * The mocked Mink session object used for testing.
26 * @var \Behat\Mink\Session|\Prophecy\Prophecy\ObjectProphecy
31 * The mocked page element used for testing.
33 * @var Behat\Mink\Element\DocumentElement|\Prophecy\Prophecy\ObjectProphecy
38 * The mocked web assert class.
40 * @var \Drupal\Tests\WebAssert|\Prophecy\Prophecy\ObjectProphecy
47 public function setUp() {
50 $this->page = $this->prophesize(DocumentElement::class);
51 $this->session = $this->prophesize(Session::class);
52 $this->session->getPage()->willReturn($this->page->reveal());
53 $this->webAssert = $this->prophesize(WebAssert::class);
57 * @covers ::assertUniqueText
59 public function testAssertUniqueText() {
60 $this->page->getText()->willReturn('foo bar bar');
61 $this->assertUniqueText('foo');
65 * @covers ::assertUniqueText
67 public function testAssertUniqueTextFail() {
68 $this->page->getText()->willReturn('foo bar bar');
69 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
70 $this->assertUniqueText('bar');
74 * @covers ::assertUniqueText
76 public function testAssertUniqueTextUnknown() {
77 $this->page->getText()->willReturn('foo bar bar');
78 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
79 $this->assertUniqueText('alice');
83 * @covers ::assertUniqueText
85 public function testAssertUniqueTextMarkup() {
86 $this->page->getText()->willReturn('foo bar bar');
87 $markupObject = $this->prophesize(MarkupInterface::class);
88 $markupObject->__toString()->willReturn('foo');
89 $this->assertUniqueText($markupObject->reveal());
93 * @covers ::assertNoUniqueText
95 public function testAssertNoUniqueText() {
96 $this->page->getText()->willReturn('foo bar bar');
97 $this->assertNoUniqueText('bar');
101 * @covers ::assertNoUniqueText
103 public function testAssertNoUniqueTextFail() {
104 $this->page->getText()->willReturn('foo bar bar');
105 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
106 $this->assertNoUniqueText('foo');
110 * @covers ::assertNoUniqueText
112 public function testAssertNoUniqueTextUnknown() {
113 $this->page->getText()->willReturn('foo bar bar');
114 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
115 $this->assertNoUniqueText('alice');
119 * @covers ::assertNoUniqueText
121 public function testAssertNoUniqueTextMarkup() {
122 $this->page->getText()->willReturn('foo bar bar');
123 $markupObject = $this->prophesize(MarkupInterface::class);
124 $markupObject->__toString()->willReturn('bar');
125 $this->assertNoUniqueText($markupObject->reveal());
129 * @covers ::assertOptionSelected
131 public function testAssertOptionSelected() {
132 $option_field = $this->prophesize(NodeElement::class);
133 $option_field->hasAttribute('selected')->willReturn(TRUE);
136 ->optionExists('myselect', 'two')
137 ->willReturn($option_field->reveal());
139 $this->assertOptionSelected('myselect', 'two');
143 * @covers ::assertOptionSelected
145 public function testAssertOptionSelectedFail() {
146 $option_field = $this->prophesize(NodeElement::class);
147 $option_field->hasAttribute('selected')->willReturn(FALSE);
150 ->optionExists('myselect', 'two')
151 ->willReturn($option_field->reveal());
153 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
154 $this->assertOptionSelected('myselect', 'two');
158 * @covers ::assertNoPattern
160 public function testAssertNoPattern() {
162 ->responseNotMatches('/.*foo$/')
165 $this->assertNoPattern('/.*foo$/');
169 * @covers ::assertNoCacheTag
171 public function testAssertNoCacheTag() {
173 ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'some-cache-tag')
176 $this->assertNoCacheTag('some-cache-tag');
180 * Returns a mocked behat session object.
182 * @return \Behat\Mink\Session
183 * The mocked session.
185 protected function getSession() {
186 return $this->session->reveal();
192 public function assertSession($name = NULL) {
193 return $this->webAssert->reveal();