From 41b6e4bde6b01265ed1e5402144e57985209396d Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 27 Jan 2017 21:10:31 -0500 Subject: [PATCH] Move assertHtml() tests into a separate test case. Don't indirectly tests via a 'fixture' that is also a test case. This pattern is kind of confusing and having assertHtml() be its own test case feels simpler. --- tests/Fixture/AssertHtmlTestCase.php | 125 ----------- tests/TestCase/TestSuite/AssertHtmlTest.php | 221 ++++++++++++++++++++ tests/TestCase/TestSuite/TestCaseTest.php | 130 ------------ 3 files changed, 221 insertions(+), 255 deletions(-) delete mode 100644 tests/Fixture/AssertHtmlTestCase.php create mode 100644 tests/TestCase/TestSuite/AssertHtmlTest.php diff --git a/tests/Fixture/AssertHtmlTestCase.php b/tests/Fixture/AssertHtmlTestCase.php deleted file mode 100644 index 68dde3eaa47..00000000000 --- a/tests/Fixture/AssertHtmlTestCase.php +++ /dev/null @@ -1,125 +0,0 @@ -My link'; - $pattern = [ - 'a' => ['href' => '/test.html', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - - $input = "My link"; - $pattern = [ - 'a' => ['href' => '/test.html', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - - $input = "My link"; - $pattern = [ - 'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - } - - /** - * testNumericValuesInExpectationForAssertHtml - * - * @return void - */ - public function testNumericValuesInExpectationForAssertHtml() - { - $value = 220985; - - $input = '

' . $value . '

'; - $pattern = [ - 'assertHtml($pattern, $input); - - $input = '

' . $value . '

' . $value . '

'; - $pattern = [ - 'assertHtml($pattern, $input); - - $input = '

' . $value . '

' . $value . '

'; - $pattern = [ - ' ['id' => $value], - 'assertHtml($pattern, $input); - } - - /** - * testBadAssertHtml - * - * @return void - */ - public function testBadAssertHtml() - { - $input = 'My link'; - $pattern = [ - 'a' => ['hRef' => '/test.html', 'clAss' => 'active'], - 'My link2', - '/a' - ]; - $this->assertHtml($pattern, $input); - } - - /** - * testBadAssertHtml - * - * @return void - */ - public function testBadAssertHtml2() - { - $input = 'My link'; - $pattern = [ - ' ['href' => '/test.html', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - } -} diff --git a/tests/TestCase/TestSuite/AssertHtmlTest.php b/tests/TestCase/TestSuite/AssertHtmlTest.php new file mode 100644 index 00000000000..6f4d5a8b39a --- /dev/null +++ b/tests/TestCase/TestSuite/AssertHtmlTest.php @@ -0,0 +1,221 @@ +My link'; + $pattern = [ + 'a' => ['href' => '/test.html', 'class' => 'active'], + 'My link', + '/a' + ]; + $this->assertHtml($pattern, $input); + + $input = "My link"; + $pattern = [ + 'a' => ['href' => '/test.html', 'class' => 'active'], + 'My link', + '/a' + ]; + $this->assertHtml($pattern, $input); + + $input = "My link"; + $pattern = [ + 'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'], + 'My link', + '/a' + ]; + $this->assertHtml($pattern, $input); + + $input = "Text"; + $pattern = [ + 'assertHtml($pattern, $input); + + $input = "Text"; + $pattern = [ + 'span' => ['class'], + '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 = '
' . + 'My div' . + '
'; + $this->assertHtml($pattern, $input); + } + + + /** + * test that assertHtml knows how to handle correct quoting. + * + * @return void + */ + public function testAssertHtmlQuotes() + { + $input = 'My link'; + $pattern = [ + 'a' => ['href' => '/test.html', 'class' => 'active'], + 'My link', + '/a' + ]; + $this->assertHtml($pattern, $input); + + $input = "My link"; + $pattern = [ + 'a' => ['href' => '/test.html', 'class' => 'active'], + 'My link', + '/a' + ]; + $this->assertHtml($pattern, $input); + + $input = "My link"; + $pattern = [ + 'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'], + 'My link', + '/a' + ]; + $this->assertHtml($pattern, $input); + } + + /** + * testNumericValuesInExpectationForAssertHtml + * + * @return void + */ + public function testNumericValuesInExpectationForAssertHtml() + { + $value = 220985; + + $input = '

' . $value . '

'; + $pattern = [ + 'assertHtml($pattern, $input); + + $input = '

' . $value . '

' . $value . '

'; + $pattern = [ + 'assertHtml($pattern, $input); + + $input = '

' . $value . '

' . $value . '

'; + $pattern = [ + ' ['id' => $value], + 'assertHtml($pattern, $input); + } + + /** + * test assertions fail when attributes are wrong. + * + * @return void + */ + public function testBadAssertHtmlInvalidAttribute() + { + $input = 'My link'; + $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 = 'My link'; + $pattern = [ + ' ['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 getMessage() + ); + } + } +} diff --git a/tests/TestCase/TestSuite/TestCaseTest.php b/tests/TestCase/TestSuite/TestCaseTest.php index b3a90a784f1..9bb1f06d5b2 100644 --- a/tests/TestCase/TestSuite/TestCaseTest.php +++ b/tests/TestCase/TestSuite/TestCaseTest.php @@ -24,7 +24,6 @@ use Cake\ORM\Table; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; -use Cake\Test\Fixture\AssertHtmlTestCase; use Cake\Test\Fixture\FixturizedTestCase; /** @@ -122,135 +121,6 @@ public function testEventFired() $this->assertEventFired('my.event', $manager); } - /** - * 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 = 'My link'; - $pattern = [ - 'a' => ['href' => '/test.html', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - - $input = "My link"; - $pattern = [ - 'a' => ['href' => '/test.html', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - - $input = "My link"; - $pattern = [ - 'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'], - 'My link', - '/a' - ]; - $this->assertHtml($pattern, $input); - - $input = "Text"; - $pattern = [ - 'assertHtml($pattern, $input); - - $input = "Text"; - $pattern = [ - 'span' => ['class'], - '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 = '
' . - 'My 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 *