Skip to content

Commit

Permalink
Closes #3340
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 11, 2018
1 parent 2e5304d commit 90e9e03
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog-7.5.md
Expand Up @@ -4,5 +4,9 @@ All notable changes of the PHPUnit 7.5 release series are documented in this fil

## [7.5.0] - 2018-12-07

### Added

* Implemented [#3340](https://github.com/sebastianbergmann/phpunit/issues/3340): Added `assertEqualsCanonicalizing()`, `assertEqualsIgnoringCase()`, `assertEqualsWithDelta()`, `assertNotEqualsCanonicalizing()`, `assertNotEqualsIgnoringCase()`, and `assertNotEqualsWithDelta()` as alternatives to using `assertEquals()` and `assertNotEquals()` with the `$delta`, `$canonicalize`, or `$ignoreCase` parameters

[7.5.0]: https://github.com/sebastianbergmann/phpunit/compare/7.4...7.5.0

114 changes: 114 additions & 0 deletions src/Framework/Assert.php
Expand Up @@ -486,6 +486,60 @@ public static function assertEquals($expected, $actual, string $message = '', fl
static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that two variables are equal (canonicalizing).
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertEqualsCanonicalizing($expected, $actual, string $message = ''): void
{
$constraint = new IsEqual(
$expected,
0.0,
10,
true,
false
);

static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that two variables are equal (ignoring case).
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertEqualsIgnoringCase($expected, $actual, string $message = ''): void
{
$constraint = new IsEqual(
$expected,
0.0,
10,
false,
true
);

static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that two variables are equal (with delta).
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertEqualsWithDelta($expected, $actual, float $delta, string $message = ''): void
{
$constraint = new IsEqual(
$expected,
$delta
);

static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that a variable is equal to an attribute of an object.
*
Expand Down Expand Up @@ -533,6 +587,66 @@ public static function assertNotEquals($expected, $actual, string $message = '',
static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that two variables are not equal (canonicalizing).
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertNotEqualsCanonicalizing($expected, $actual, string $message = ''): void
{
$constraint = new LogicalNot(
new IsEqual(
$expected,
0.0,
10,
true,
false
)
);

static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that two variables are not equal (ignoring case).
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertNotEqualsIgnoringCase($expected, $actual, string $message = ''): void
{
$constraint = new LogicalNot(
new IsEqual(
$expected,
0.0,
10,
false,
true
)
);

static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that two variables are not equal (with delta).
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertNotEqualsWithDelta($expected, $actual, float $delta, string $message = ''): void
{
$constraint = new LogicalNot(
new IsEqual(
$expected,
$delta
)
);

static::assertThat($actual, $constraint, $message);
}

/**
* Asserts that a variable is not equal to an attribute of an object.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/Framework/AssertTest.php
Expand Up @@ -2506,6 +2506,41 @@ public function testAssertStringNotMatchesFormatFile(): void
$this->assertStringNotMatchesFormatFile(TEST_FILES_PATH . 'expectedFileFormat.txt', "FOO\n");
}

public function testStringsCanBeComparedForEqualityIgnoringCase(): void
{
$this->assertEqualsIgnoringCase('a', 'A');

$this->assertNotEqualsIgnoringCase('a', 'B');
}

public function testArraysOfStringsCanBeComparedForEqualityIgnoringCase(): void
{
$this->assertEqualsIgnoringCase(['a'], ['A']);

$this->assertNotEqualsIgnoringCase(['a'], ['B']);
}

public function testStringsCanBeComparedForEqualityWithDelta(): void
{
$this->assertEqualsWithDelta(2.3, 2.5, 0.5);

$this->assertNotEqualsWithDelta(2.3, 3.5, 0.5);
}

public function testArraysOfStringsCanBeComparedForEqualityWithDelta(): void
{
$this->assertEqualsWithDelta([2.3], [2.5], 0.5);

$this->assertNotEqualsWithDelta([2.3], [3.5], 0.5);
}

public function testArraysCanBeComparedForEqualityWithCanonicalization(): void
{
$this->assertEqualsCanonicalizing([3, 2, 1], [2, 3, 1]);

$this->assertNotEqualsCanonicalizing([3, 2, 1], [2, 3, 4]);
}

protected function sameValues(): array
{
$object = new \SampleClass(4, 8, 15);
Expand Down

0 comments on commit 90e9e03

Please sign in to comment.