Browse Source

Merge pull request #10120 from cakephp/issue-10117

assertHtml() should accept whitespace before a closing tag.
Mark Story 9 years ago
parent
commit
1c630322f3

+ 1 - 1
src/TestSuite/TestCase.php

@@ -401,7 +401,7 @@ abstract class TestCase extends PHPUnit_Framework_TestCase
                     }
                     $regex[] = [
                         sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
-                        sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
+                        sprintf('%s\s*<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
                         $i,
                     ];
                     continue;

+ 0 - 125
tests/Fixture/AssertHtmlTestCase.php

@@ -1,125 +0,0 @@
-<?php
-namespace Cake\Test\Fixture;
-
-use Cake\TestSuite\TestCase;
-
-/**
- * This class helps in indirectly testing the functionality of TestCase::assertHtml
- */
-class AssertHtmlTestCase extends TestCase
-{
-
-    /**
-     * test that assertHtml knows how to handle correct quoting.
-     *
-     * @return void
-     */
-    public function testAssertHtmlQuotes()
-    {
-        $input = '<a href="/test.html" class="active">My link</a>';
-        $pattern = [
-            'a' => ['href' => '/test.html', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = "<a href='/test.html' class='active'>My link</a>";
-        $pattern = [
-            'a' => ['href' => '/test.html', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = "<a href='/test.html' class='active'>My link</a>";
-        $pattern = [
-            'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-    }
-
-    /**
-     * testNumericValuesInExpectationForAssertHtml
-     *
-     * @return void
-     */
-    public function testNumericValuesInExpectationForAssertHtml()
-    {
-        $value = 220985;
-
-        $input = '<p><strong>' . $value . '</strong></p>';
-        $pattern = [
-            '<p',
-                '<strong',
-                    $value,
-                '/strong',
-            '/p'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = '<p><strong>' . $value . '</strong></p><p><strong>' . $value . '</strong></p>';
-        $pattern = [
-            '<p',
-                '<strong',
-                    $value,
-                '/strong',
-            '/p',
-            '<p',
-                '<strong',
-                    $value,
-                '/strong',
-            '/p',
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = '<p><strong>' . $value . '</strong></p><p id="' . $value . '"><strong>' . $value . '</strong></p>';
-        $pattern = [
-            '<p',
-                '<strong',
-                    $value,
-                '/strong',
-            '/p',
-            'p' => ['id' => $value],
-                '<strong',
-                    $value,
-                '/strong',
-            '/p',
-        ];
-        $this->assertHtml($pattern, $input);
-    }
-
-    /**
-     * testBadAssertHtml
-     *
-     * @return void
-     */
-    public function testBadAssertHtml()
-    {
-        $input = '<a href="/test.html" class="active">My link</a>';
-        $pattern = [
-            'a' => ['hRef' => '/test.html', 'clAss' => 'active'],
-            'My link2',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-    }
-
-    /**
-     * testBadAssertHtml
-     *
-     * @return void
-     */
-    public function testBadAssertHtml2()
-    {
-        $input = '<a href="/test.html" class="active">My link</a>';
-        $pattern = [
-            '<a' => ['href' => '/test.html', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-    }
-}

+ 239 - 0
tests/TestCase/TestSuite/AssertHtmlTest.php

@@ -0,0 +1,239 @@
+<?php
+namespace Cake\Test\Fixture;
+
+use Cake\TestSuite\TestCase;
+use PHPUnit_Framework_ExpectationFailedException;
+
+/**
+ * This class helps in indirectly testing the functionality of TestCase::assertHtml
+ */
+class AssertHtmlTest extends TestCase
+{
+    public function testAssertHtmlWhitespace()
+    {
+        $input = <<<HTML
+<div class="wrapper">
+    <h4 class="widget-title">Popular tags
+        <i class="i-icon"></i>
+    </h4>
+</div>
+HTML;
+        $pattern = [
+            'div' => ['class' => 'wrapper'],
+            'h4' => ['class' => 'widget-title'], 'Popular tags',
+            'i' => ['class' => 'i-icon'], '/i',
+            '/h4',
+            '/div'
+        ];
+        $this->assertHtml($pattern, $input);
+    }
+    /**
+     * test assertHtml works with single and double quotes
+     *
+     * @return void
+     */
+    public function testAssertHtmlQuoting()
+    {
+        $input = '<a href="/test.html" class="active">My link</a>';
+        $pattern = [
+            'a' => ['href' => '/test.html', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = "<a href='/test.html' class='active'>My link</a>";
+        $pattern = [
+            'a' => ['href' => '/test.html', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = "<a href='/test.html' class='active'>My link</a>";
+        $pattern = [
+            'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = "<span><strong>Text</strong></span>";
+        $pattern = [
+            '<span',
+            '<strong',
+            'Text',
+            '/strong',
+            '/span'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = "<span class='active'><strong>Text</strong></span>";
+        $pattern = [
+            'span' => ['class'],
+            '<strong',
+            'Text',
+            '/strong',
+            '/span'
+        ];
+        $this->assertHtml($pattern, $input);
+    }
+
+    /**
+     * Test that assertHtml runs quickly.
+     *
+     * @return void
+     */
+    public function testAssertHtmlRuntimeComplexity()
+    {
+        $pattern = [
+            'div' => [
+                'attr1' => 'val1',
+                'attr2' => 'val2',
+                'attr3' => 'val3',
+                'attr4' => 'val4',
+                'attr5' => 'val5',
+                'attr6' => 'val6',
+                'attr7' => 'val7',
+                'attr8' => 'val8',
+            ],
+            'My div',
+            '/div'
+        ];
+        $input = '<div attr8="val8" attr6="val6" attr4="val4" attr2="val2"' .
+            ' attr1="val1" attr3="val3" attr5="val5" attr7="val7" />' .
+            'My div' .
+            '</div>';
+        $this->assertHtml($pattern, $input);
+    }
+
+
+    /**
+     * test that assertHtml knows how to handle correct quoting.
+     *
+     * @return void
+     */
+    public function testAssertHtmlQuotes()
+    {
+        $input = '<a href="/test.html" class="active">My link</a>';
+        $pattern = [
+            'a' => ['href' => '/test.html', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = "<a href='/test.html' class='active'>My link</a>";
+        $pattern = [
+            'a' => ['href' => '/test.html', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = "<a href='/test.html' class='active'>My link</a>";
+        $pattern = [
+            'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        $this->assertHtml($pattern, $input);
+    }
+
+    /**
+     * testNumericValuesInExpectationForAssertHtml
+     *
+     * @return void
+     */
+    public function testNumericValuesInExpectationForAssertHtml()
+    {
+        $value = 220985;
+
+        $input = '<p><strong>' . $value . '</strong></p>';
+        $pattern = [
+            '<p',
+                '<strong',
+                    $value,
+                '/strong',
+            '/p'
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = '<p><strong>' . $value . '</strong></p><p><strong>' . $value . '</strong></p>';
+        $pattern = [
+            '<p',
+                '<strong',
+                    $value,
+                '/strong',
+            '/p',
+            '<p',
+                '<strong',
+                    $value,
+                '/strong',
+            '/p',
+        ];
+        $this->assertHtml($pattern, $input);
+
+        $input = '<p><strong>' . $value . '</strong></p><p id="' . $value . '"><strong>' . $value . '</strong></p>';
+        $pattern = [
+            '<p',
+                '<strong',
+                    $value,
+                '/strong',
+            '/p',
+            'p' => ['id' => $value],
+                '<strong',
+                    $value,
+                '/strong',
+            '/p',
+        ];
+        $this->assertHtml($pattern, $input);
+    }
+
+    /**
+     * test assertions fail when attributes are wrong.
+     *
+     * @return void
+     */
+    public function testBadAssertHtmlInvalidAttribute()
+    {
+        $input = '<a href="/test.html" class="active">My link</a>';
+        $pattern = [
+            'a' => ['hRef' => '/test.html', 'clAss' => 'active'],
+            'My link2',
+            '/a'
+        ];
+        try {
+            $this->assertHtml($pattern, $input);
+            $this->fail('Assertion should fail');
+        } catch (PHPUnit_Framework_ExpectationFailedException $e) {
+            $this->assertContains(
+                'Attribute did not match. Was expecting Attribute "clAss" == "active"',
+                $e->getMessage()
+            );
+        }
+    }
+
+    /**
+     * test assertion failure on incomplete HTML
+     *
+     * @return void
+     */
+    public function testBadAssertHtmlMissingTags()
+    {
+        $input = '<a href="/test.html" class="active">My link</a>';
+        $pattern = [
+            '<a' => ['href' => '/test.html', 'class' => 'active'],
+            'My link',
+            '/a'
+        ];
+        try {
+            $this->assertHtml($pattern, $input);
+        } catch (PHPUnit_Framework_ExpectationFailedException $e) {
+            $this->assertContains(
+                'Item #1 / regex #0 failed: Open <a tag',
+                $e->getMessage()
+            );
+        }
+    }
+}

+ 0 - 130
tests/TestCase/TestSuite/TestCaseTest.php

@@ -24,7 +24,6 @@ use Cake\ORM\Entity;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
-use Cake\Test\Fixture\AssertHtmlTestCase;
 use Cake\Test\Fixture\FixturizedTestCase;
 
 /**
@@ -123,135 +122,6 @@ class TestCaseTest extends TestCase
     }
 
     /**
-     * testAssertHtml
-     *
-     * @return void
-     */
-    public function testAssertHtmlBasic()
-    {
-        $test = new AssertHtmlTestCase('testAssertHtmlQuotes');
-        $result = $test->run();
-
-        $this->assertEquals(0, $result->errorCount());
-        $this->assertTrue($result->wasSuccessful());
-        $this->assertEquals(0, $result->failureCount());
-    }
-
-    /**
-     * test assertHtml works with single and double quotes
-     *
-     * @return void
-     */
-    public function testAssertHtmlQuoting()
-    {
-        $input = '<a href="/test.html" class="active">My link</a>';
-        $pattern = [
-            'a' => ['href' => '/test.html', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = "<a href='/test.html' class='active'>My link</a>";
-        $pattern = [
-            'a' => ['href' => '/test.html', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = "<a href='/test.html' class='active'>My link</a>";
-        $pattern = [
-            'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'],
-            'My link',
-            '/a'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = "<span><strong>Text</strong></span>";
-        $pattern = [
-            '<span',
-            '<strong',
-            'Text',
-            '/strong',
-            '/span'
-        ];
-        $this->assertHtml($pattern, $input);
-
-        $input = "<span class='active'><strong>Text</strong></span>";
-        $pattern = [
-            'span' => ['class'],
-            '<strong',
-            'Text',
-            '/strong',
-            '/span'
-        ];
-        $this->assertHtml($pattern, $input);
-    }
-
-    /**
-     * Test that assertHtml runs quickly.
-     *
-     * @return void
-     */
-    public function testAssertHtmlRuntimeComplexity()
-    {
-        $pattern = [
-            'div' => [
-                'attr1' => 'val1',
-                'attr2' => 'val2',
-                'attr3' => 'val3',
-                'attr4' => 'val4',
-                'attr5' => 'val5',
-                'attr6' => 'val6',
-                'attr7' => 'val7',
-                'attr8' => 'val8',
-            ],
-            'My div',
-            '/div'
-        ];
-        $input = '<div attr8="val8" attr6="val6" attr4="val4" attr2="val2"' .
-            ' attr1="val1" attr3="val3" attr5="val5" attr7="val7" />' .
-            'My div' .
-            '</div>';
-        $this->assertHtml($pattern, $input);
-    }
-
-    /**
-     * testNumericValuesInExpectationForAssertHtml
-     *
-     * @return void
-     */
-    public function testNumericValuesInExpectationForAssertHtml()
-    {
-        $test = new AssertHtmlTestCase('testNumericValuesInExpectationForAssertHtml');
-        $result = $test->run();
-        $this->assertEquals(0, $result->errorCount());
-        $this->assertTrue($result->wasSuccessful());
-        $this->assertEquals(0, $result->failureCount());
-    }
-
-    /**
-     * testBadAssertHtml
-     *
-     * @return void
-     */
-    public function testBadAssertHtml()
-    {
-        $test = new AssertHtmlTestCase('testBadAssertHtml');
-        $result = $test->run();
-        $this->assertEquals(0, $result->errorCount());
-        $this->assertFalse($result->wasSuccessful());
-        $this->assertEquals(1, $result->failureCount());
-
-        $test = new AssertHtmlTestCase('testBadAssertHtml2');
-        $result = $test->run();
-        $this->assertEquals(0, $result->errorCount());
-        $this->assertFalse($result->wasSuccessful());
-        $this->assertEquals(1, $result->failureCount());
-    }
-
-    /**
      * testLoadFixturesOnDemand
      *
      * @return void