|
10 | 10 | * @testdox Constraints\ElementContainsString
|
11 | 11 | *
|
12 | 12 | * @covers SteveGrunwell\PHPUnit_Markup_Assertions\Constraints\ElementContainsString
|
| 13 | + * |
| 14 | + * @group Constraints |
13 | 15 | */
|
14 | 16 | class ElementContainsStringTest extends TestCase
|
15 | 17 | {
|
@@ -63,24 +65,116 @@ public function it_should_fail_if_no_match_is_found()
|
63 | 65 | $this->assertFalse($constraint->evaluate($html, '', true));
|
64 | 66 | }
|
65 | 67 |
|
| 68 | + /** |
| 69 | + * @test |
| 70 | + * |
| 71 | + * @dataProvider provideGreetingsInDifferentLanguages |
| 72 | + * |
| 73 | + * @ticket https://github.com/stevegrunwell/phpunit-markup-assertions/issues/31 |
| 74 | + */ |
| 75 | + public function it_should_be_able_to_handle_various_character_sets($greeting) |
| 76 | + { |
| 77 | + $constraint = new ElementContainsString(new Selector('h1'), $greeting); |
| 78 | + $html = sprintf('<div><h1>%s</h1></div>', $greeting); |
| 79 | + |
| 80 | + $this->assertTrue($constraint->evaluate($html, '', true)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @test |
| 85 | + */ |
| 86 | + public function it_should_test_against_the_inner_contents_of_the_found_nodes() |
| 87 | + { |
| 88 | + $constraint = new ElementContainsString(new Selector('p'), 'class'); |
| 89 | + $html = '<p class="first">First</p><p class="second">Second</p>'; |
| 90 | + |
| 91 | + $this->assertFalse( |
| 92 | + $constraint->evaluate($html, '', true), |
| 93 | + 'The string "class" does not appear in either paragraph, and thus should not be matched.' |
| 94 | + ); |
| 95 | + } |
| 96 | + |
66 | 97 | /**
|
67 | 98 | * @test
|
68 | 99 | */
|
69 | 100 | public function it_should_fail_with_a_useful_error_message()
|
70 | 101 | {
|
71 |
| - $selector = new Selector('p.body'); |
72 | 102 | $html = '<p>Some other string</p>';
|
| 103 | + $expected = <<<'MSG' |
| 104 | +Failed asserting that element matching selector 'p' contains string 'some string'. |
| 105 | +Matching element: |
| 106 | +[ |
| 107 | + <p>Some other string</p> |
| 108 | +] |
| 109 | +MSG; |
| 110 | + |
| 111 | + try { |
| 112 | + (new ElementContainsString(new Selector('p'), 'some string'))->evaluate($html); |
| 113 | + } catch (\Throwable $e) { |
| 114 | + $this->assertSame($expected, $e->getMessage()); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $this->fail('Did not receive the expected error message.'); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @test |
| 123 | + */ |
| 124 | + public function it_should_include_all_relevant_matches_in_error_messages() |
| 125 | + { |
| 126 | + $html = '<p>Some other string</p><p>Yet another string</p>'; |
| 127 | + $expected = <<<'MSG' |
| 128 | +Failed asserting that any elements matching selector 'p' contain string 'some string'. |
| 129 | +Matching elements: |
| 130 | +[ |
| 131 | + <p>Some other string</p> |
| 132 | + <p>Yet another string</p> |
| 133 | +] |
| 134 | +MSG; |
| 135 | + |
| 136 | + try { |
| 137 | + (new ElementContainsString(new Selector('p'), 'some string'))->evaluate($html); |
| 138 | + } catch (\Throwable $e) { |
| 139 | + $this->assertSame($expected, $e->getMessage()); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + $this->fail('Did not receive the expected error message.'); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @test |
| 148 | + */ |
| 149 | + public function it_should_provide_a_simple_error_message_if_no_selector_matches_are_found() |
| 150 | + { |
| 151 | + $html = '<p>Some other string</p><p>Yet another string</p>'; |
| 152 | + $expected = "Failed asserting that any elements match selector 'h1'."; |
73 | 153 |
|
74 | 154 | try {
|
75 |
| - (new ElementContainsString($selector, 'some string'))->evaluate($html); |
76 |
| - } catch (\Exception $e) { |
77 |
| - $this->assertSame( |
78 |
| - "Failed asserting that element with selector '{$selector}' in '{$html}' contains string 'some string'.", |
79 |
| - $e->getMessage() |
80 |
| - ); |
| 155 | + (new ElementContainsString(new Selector('h1'), 'some string'))->evaluate($html); |
| 156 | + } catch (\Throwable $e) { |
| 157 | + $this->assertSame($expected, $e->getMessage()); |
81 | 158 | return;
|
82 | 159 | }
|
83 | 160 |
|
84 | 161 | $this->fail('Did not receive the expected error message.');
|
85 | 162 | }
|
| 163 | + |
| 164 | + /** |
| 165 | + * Provide a list of strings in various language. |
| 166 | + * |
| 167 | + * @return Iterable<string,array<string>> |
| 168 | + */ |
| 169 | + public function provideGreetingsInDifferentLanguages() |
| 170 | + { |
| 171 | + yield 'Arabic' => ['مرحبا!']; |
| 172 | + yield 'Chinese' => ['你好']; |
| 173 | + yield 'English' => ['Hello']; |
| 174 | + yield 'Hebrew' => ['שלום']; |
| 175 | + yield 'Japanese' => ['こんにちは']; |
| 176 | + yield 'Korean' => ['안녕하십니까']; |
| 177 | + yield 'Punjabi' => ['ਸਤ ਸ੍ਰੀ ਅਕਾਲ']; |
| 178 | + yield 'Ukrainian' => ['Привіт']; |
| 179 | + } |
86 | 180 | }
|
0 commit comments