Skip to content

Commit

Permalink
Merge pull request #4206 from dereuromark/3.0-assert
Browse files Browse the repository at this point in the history
3.0 Consistent asserts in TestSuite
  • Loading branch information
lorenzo committed Aug 10, 2014
2 parents 7139bf7 + acf02ff commit 3bcd581
Show file tree
Hide file tree
Showing 24 changed files with 898 additions and 802 deletions.
41 changes: 35 additions & 6 deletions src/TestSuite/TestCase.php
Expand Up @@ -266,6 +266,21 @@ public function assertTextNotContains($needle, $haystack, $message = '', $ignore
}

/**
* Asserts HTML tags.
*
* @param string $string An HTML/XHTML/XML string
* @param array $expected An array, see above
* @param string $fullDebug Whether or not more verbose output should be used.
* @return void
* @deprecated 3.0. Use assertHtml() instead.
*/
public function assertTags($string, $expected, $fullDebug = false) {
static::assertHtml($expected, $string, $fullDebug);
}

/**
* Asserts HTML tags.
*
* Takes an array $expected and generates a regex from it to match the provided $string.
* Samples for $expected:
*
Expand Down Expand Up @@ -302,12 +317,12 @@ public function assertTextNotContains($needle, $haystack, $message = '', $ignore
* Important: This function is very forgiving about whitespace and also accepts any
* permutation of attribute order. It will also allow whitespace between specified tags.
*
* @param string $string An HTML/XHTML/XML string
* @param array $expected An array, see above
* @param string $string An HTML/XHTML/XML string
* @param string $fullDebug Whether or not more verbose output should be used.
* @return void
*/
public function assertTags($string, $expected, $fullDebug = false) {
public function assertHtml($expected, $string, $fullDebug = false) {
$regex = array();
$normalized = array();
foreach ((array)$expected as $key => $val) {
Expand Down Expand Up @@ -481,15 +496,29 @@ protected function _normalizePath($path) {
// @codingStandardsIgnoreStart

/**
* Compatibility function to test if value is between an acceptable range
* Compatibility function to test if a value is between an acceptable range.
*
* @param mixed $result
* @param mixed $expected
* @param mixed $margin the rage of acceptation
* @param float $result
* @param float $expected
* @param float $margin the rage of acceptation
* @param string $message the text to display if the assertion is not correct
* @return void
* @deprecated 3.0. Use assertWithinRange() instead.
*/
protected static function assertWithinMargin($result, $expected, $margin, $message = '') {
static::assertWithinRange($expected, $result, $margin, $message);
}

/**
* Compatibility function to test if a value is between an acceptable range.
*
* @param float $expected
* @param float $result
* @param float $margin the rage of acceptation
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected static function assertWithinRange($expected, $result, $margin, $message = '') {
$upper = $result + $margin;
$lower = $result - $margin;
static::assertTrue((($expected <= $upper) && ($expected >= $lower)), $message);
Expand Down
Expand Up @@ -4,48 +4,48 @@
use Cake\TestSuite\TestCase;

/**
* This class helps in indirectly testing the functionalities of CakeTestCase::assertTags
* This class helps in indirectly testing the functionalities of CakeTestCase::assertHtml
*
*/
class AssertTagsTestCase extends TestCase {
class AssertHtmlTestCase extends TestCase {

/**
* test that assertTags knows how to handle correct quoting.
* test that assertHtml knows how to handle correct quoting.
*
* @return void
*/
public function testAssertTagsQuotes() {
public function testAssertHtmlQuotes() {
$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
'a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = "<a href='/test.html' class='active'>My link</a>";
$pattern = array(
'a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = "<a href='/test.html' class='active'>My link</a>";
$pattern = array(
'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);
}

/**
* testNumericValuesInExpectationForAssertTags
* testNumericValuesInExpectationForAssertHtml
*
* @return void
*/
public function testNumericValuesInExpectationForAssertTags() {
public function testNumericValuesInExpectationForAssertHtml() {
$value = 220985;

$input = '<p><strong>' . $value . '</strong></p>';
Expand All @@ -56,7 +56,7 @@ public function testNumericValuesInExpectationForAssertTags() {
'/strong',
'/p'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = '<p><strong>' . $value . '</strong></p><p><strong>' . $value . '</strong></p>';
$pattern = array(
Expand All @@ -71,7 +71,7 @@ public function testNumericValuesInExpectationForAssertTags() {
'/strong',
'/p',
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = '<p><strong>' . $value . '</strong></p><p id="' . $value . '"><strong>' . $value . '</strong></p>';
$pattern = array(
Expand All @@ -86,37 +86,37 @@ public function testNumericValuesInExpectationForAssertTags() {
'/strong',
'/p',
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);
}

/**
* testBadAssertTags
* testBadAssertHtml
*
* @return void
*/
public function testBadAssertTags() {
public function testBadAssertHtml() {
$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
'a' => array('hRef' => '/test.html', 'clAss' => 'active'),
'My link2',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);
}

/**
* testBadAssertTags
* testBadAssertHtml
*
* @return void
*/
public function testBadAssertTags2() {
public function testBadAssertHtml2() {
$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
'<a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);
}

}
Expand Up @@ -389,7 +389,7 @@ public function testWriteArrayValues() {
$result = $this->Controller->response->cookie('Testing');

$time = new Time('now');
$this->assertWithinMargin($result['expire'], $time->format('U') + 10, 1);
$this->assertWithinRange($time->format('U') + 10, $result['expire'], 1);
unset($result['expire']);
$this->assertEquals($expected, $result);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2297,7 +2297,7 @@ function($q) {
->select(['d' => $query->func()->now('time')])
->execute();

$this->assertWithinMargin(
$this->assertWithinRange(
date('U'),
(new \DateTime($result->fetchAll('assoc')[0]['d']))->format('U'),
1
Expand All @@ -2307,7 +2307,7 @@ function($q) {
$result = $query
->select(['d' => $query->func()->now()])
->execute();
$this->assertWithinMargin(
$this->assertWithinRange(
date('U'),
(new \DateTime($result->fetchAll('assoc')[0]['d']))->format('U'),
1
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Network/Session/DatabaseSessionTest.php
Expand Up @@ -100,7 +100,7 @@ public function testWrite() {
$this->assertEquals($expected, $result);

$expected = time() + ini_get('session.gc_maxlifetime');
$this->assertWithinMargin($expires, $expected, 1);
$this->assertWithinRange($expected, $expires, 1);
}

/**
Expand Down
42 changes: 21 additions & 21 deletions tests/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -20,7 +20,7 @@
use Cake\Core\Plugin;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Test\Fixture\AssertTagsTestCase;
use Cake\Test\Fixture\AssertHtmlTestCase;
use Cake\Test\Fixture\FixturizedTestCase;

/**
Expand All @@ -30,47 +30,47 @@
class TestCaseTest extends TestCase {

/**
* testAssertTags
* testAssertHtml
*
* @return void
*/
public function testAssertTagsBasic() {
$test = new AssertTagsTestCase('testAssertTagsQuotes');
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 assertTags works with single and double quotes
* test assertHtml works with single and double quotes
*
* @return void
*/
public function testAssertTagsQuoting() {
public function testAssertHtmlQuoting() {
$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
'a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = "<a href='/test.html' class='active'>My link</a>";
$pattern = array(
'a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = "<a href='/test.html' class='active'>My link</a>";
$pattern = array(
'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = "<span><strong>Text</strong></span>";
$pattern = array(
Expand All @@ -80,7 +80,7 @@ public function testAssertTagsQuoting() {
'/strong',
'/span'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);

$input = "<span class='active'><strong>Text</strong></span>";
$pattern = array(
Expand All @@ -90,15 +90,15 @@ public function testAssertTagsQuoting() {
'/strong',
'/span'
);
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);
}

/**
* Test that assertTags runs quickly.
* Test that assertHtml runs quickly.
*
* @return void
*/
public function testAssertTagsRuntimeComplexity() {
public function testAssertHtmlRuntimeComplexity() {
$pattern = array(
'div' => array(
'attr1' => 'val1',
Expand All @@ -117,35 +117,35 @@ public function testAssertTagsRuntimeComplexity() {
' attr1="val1" attr3="val3" attr5="val5" attr7="val7" />' .
'My div' .
'</div>';
$this->assertTags($input, $pattern);
$this->assertHtml($pattern, $input);
}

/**
* testNumericValuesInExpectationForAssertTags
* testNumericValuesInExpectationForAssertHtml
*
* @return void
*/
public function testNumericValuesInExpectationForAssertTags() {
$test = new AssertTagsTestCase('testNumericValuesInExpectationForAssertTags');
public function testNumericValuesInExpectationForAssertHtml() {
$test = new AssertHtmlTestCase('testNumericValuesInExpectationForAssertHtml');
$result = $test->run();
$this->assertEquals(0, $result->errorCount());
$this->assertTrue($result->wasSuccessful());
$this->assertEquals(0, $result->failureCount());
}

/**
* testBadAssertTags
* testBadAssertHtml
*
* @return void
*/
public function testBadAssertTags() {
$test = new AssertTagsTestCase('testBadAssertTags');
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 AssertTagsTestCase('testBadAssertTags2');
$test = new AssertHtmlTestCase('testBadAssertHtml2');
$result = $test->run();
$this->assertEquals(0, $result->errorCount());
$this->assertFalse($result->wasSuccessful());
Expand Down

0 comments on commit 3bcd581

Please sign in to comment.