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
18 class AssertLegacyTraitTest extends UnitTestCase {
20 use AssertLegacyTrait;
23 * The mocked Mink session object used for testing.
25 * @var \Behat\Mink\Session|\Prophecy\Prophecy\ObjectProphecy
30 * The mocked page element used for testing.
32 * @var Behat\Mink\Element\DocumentElement|\Prophecy\Prophecy\ObjectProphecy
37 * The mocked web assert class.
39 * @var \Drupal\Tests\WebAssert|\Prophecy\Prophecy\ObjectProphecy
46 public function setUp() {
49 $this->page = $this->prophesize(DocumentElement::class);
50 $this->session = $this->prophesize(Session::class);
51 $this->session->getPage()->willReturn($this->page->reveal());
52 $this->webAssert = $this->prophesize(WebAssert::class);
56 * @covers ::assertUniqueText
58 public function testAssertUniqueText() {
59 $this->page->getText()->willReturn('foo bar bar');
60 $this->assertUniqueText('foo');
64 * @covers ::assertUniqueText
66 public function testAssertUniqueTextFail() {
67 $this->page->getText()->willReturn('foo bar bar');
68 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
69 $this->assertUniqueText('bar');
73 * @covers ::assertUniqueText
75 public function testAssertUniqueTextUnknown() {
76 $this->page->getText()->willReturn('foo bar bar');
77 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
78 $this->assertUniqueText('alice');
82 * @covers ::assertUniqueText
84 public function testAssertUniqueTextMarkup() {
85 $this->page->getText()->willReturn('foo bar bar');
86 $markupObject = $this->prophesize(MarkupInterface::class);
87 $markupObject->__toString()->willReturn('foo');
88 $this->assertUniqueText($markupObject->reveal());
92 * @covers ::assertNoUniqueText
94 public function testAssertNoUniqueText() {
95 $this->page->getText()->willReturn('foo bar bar');
96 $this->assertNoUniqueText('bar');
100 * @covers ::assertNoUniqueText
102 public function testAssertNoUniqueTextFail() {
103 $this->page->getText()->willReturn('foo bar bar');
104 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
105 $this->assertNoUniqueText('foo');
109 * @covers ::assertNoUniqueText
111 public function testAssertNoUniqueTextUnknown() {
112 $this->page->getText()->willReturn('foo bar bar');
113 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
114 $this->assertNoUniqueText('alice');
118 * @covers ::assertNoUniqueText
120 public function testAssertNoUniqueTextMarkup() {
121 $this->page->getText()->willReturn('foo bar bar');
122 $markupObject = $this->prophesize(MarkupInterface::class);
123 $markupObject->__toString()->willReturn('bar');
124 $this->assertNoUniqueText($markupObject->reveal());
128 * @covers ::assertOptionSelected
130 public function testAssertOptionSelected() {
131 $option_field = $this->prophesize(NodeElement::class);
132 $option_field->hasAttribute('selected')->willReturn(TRUE);
135 ->optionExists('myselect', 'two')
136 ->willReturn($option_field->reveal());
138 $this->assertOptionSelected('myselect', 'two');
142 * @covers ::assertOptionSelected
144 public function testAssertOptionSelectedFail() {
145 $option_field = $this->prophesize(NodeElement::class);
146 $option_field->hasAttribute('selected')->willReturn(FALSE);
149 ->optionExists('myselect', 'two')
150 ->willReturn($option_field->reveal());
152 $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
153 $this->assertOptionSelected('myselect', 'two');
157 * @covers ::assertNoPattern
159 public function testAssertNoPattern() {
161 ->responseNotMatches('/.*foo$/')
164 $this->assertNoPattern('/.*foo$/');
168 * Returns a mocked behat session object.
170 * @return \Behat\Mink\Session
171 * The mocked session.
173 protected function getSession() {
174 return $this->session->reveal();
180 public function assertSession($name = NULL) {
181 return $this->webAssert->reveal();