Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Added failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
VGirol committed Sep 30, 2019
2 parents 9a2e94e + 4741afe commit dbda340
Show file tree
Hide file tree
Showing 49 changed files with 185 additions and 295 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
### Removed

- Moved Factory classes into another package : VGirol/JsonApi-Faker

## 1.2.1 - 2019-09-30

### Added

- Added some failure messages
- Added new methods for testing (VGirol\JsonApiAssert\SetExceptionsTrait::setFailure and VGirol\JsonApiAssert\SetExceptionsTrait::formatAsRegex)
10 changes: 0 additions & 10 deletions scripts/build.sh

This file was deleted.

14 changes: 0 additions & 14 deletions scripts/doc.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/infection.sh

This file was deleted.

33 changes: 0 additions & 33 deletions scripts/lint.sh

This file was deleted.

12 changes: 0 additions & 12 deletions scripts/metrics.sh

This file was deleted.

5 changes: 0 additions & 5 deletions scripts/phan.sh

This file was deleted.

66 changes: 0 additions & 66 deletions scripts/php-no-xdebug.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phpcpd.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phpcs.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phpdox.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phploc.sh

This file was deleted.

6 changes: 0 additions & 6 deletions scripts/phpmd.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phpmetrics.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phpmnd.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/phpstan.sh

This file was deleted.

5 changes: 0 additions & 5 deletions scripts/phpunit.sh

This file was deleted.

1 change: 0 additions & 1 deletion src/Asserts/Content/AssertErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace VGirol\JsonApiAssert\Asserts\Content;

use DMS\PHPUnitExtensions\ArraySubset\Assert as AssertArray;
use DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\ExpectationFailedException;
Expand Down
15 changes: 11 additions & 4 deletions src/Asserts/Content/AssertLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint;
use VGirol\JsonApiAssert\Messages;

/**
* This trait adds the ability to test links objects.
Expand All @@ -29,7 +30,14 @@ trait AssertLinks
*/
public static function assertLinksObjectEquals($expected, $links): void
{
PHPUnit::assertEquals(count($expected), count($links));
$countExpected = count($expected);
$count = count($links);
PHPUnit::assertEquals(
$countExpected,
$count,
sprintf(Messages::LINKS_OBJECT_HAVE_NOT_SAME_LENGTH, $count, $countExpected)
);

foreach ($expected as $name => $value) {
static::assertLinksObjectContains($name, $value, $links);
}
Expand Down Expand Up @@ -64,14 +72,13 @@ public static function assertLinksObjectContains($name, $expected, $links): void
*
* @param array|string|null $expected The expected link value
* @param array|string|null $link The link to test
* @param string $message An optional message to explain why the test failed
*
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
*/
public static function assertLinkObjectEquals($expected, $link, string $message = ''): void
public static function assertLinkObjectEquals($expected, $link): void
{
PHPUnit::assertThat($link, self::linkEqualsConstraint($expected), $message);
PHPUnit::assertThat($link, self::linkEqualsConstraint($expected));
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Asserts/Content/AssertPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint;
use VGirol\JsonApiAssert\Members;
use VGirol\JsonApiAssert\Messages;

/**
* This trait adds the ability to test pagination (links and meta).
Expand Down Expand Up @@ -76,7 +77,11 @@ public static function assertHasNoPaginationLinks($links): void
*/
public static function assertPaginationLinksEquals($expected, $json): void
{
PHPUnit::assertThat($json, self::paginationLinksEqualConstraint($expected));
PHPUnit::assertThat(
$json,
self::paginationLinksEqualConstraint($expected),
Messages::PAGINATION_LINKS_NOT_EQUAL
);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Asserts/Content/AssertResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ public static function assertResourceCollectionEquals($expected, $json)
sprintf(Messages::RESOURCE_COLLECTION_HAVE_NOT_SAME_LENGTH, $count, $expectedCount)
);

$index = 0;
foreach ($expected as $resource) {
foreach ($expected as $index => $resource) {
static::assertResourceObjectEquals($resource, $json[$index]);
$index++;
}
}

Expand Down
26 changes: 21 additions & 5 deletions src/Asserts/Content/AssertResourceLinkage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace VGirol\JsonApiAssert\Asserts\Content;

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Messages;

/**
* This trait adds the ability to test resource linkage (single or collection).
Expand All @@ -25,7 +26,11 @@ trait AssertResourceLinkage
*/
public static function assertResourceIdentifierEquals($expected, $json)
{
PHPUnit::assertSame($expected, $json);
PHPUnit::assertSame(
$expected,
$json,
sprintf(Messages::RESOURCE_IDENTIFIER_IS_NOT_EQUAL, var_export($json, true), var_export($expected, true))
);
}

/**
Expand All @@ -49,7 +54,14 @@ public static function assertResourceIdentifierEquals($expected, $json)
public static function assertResourceIdentifierCollectionEquals($expected, $json)
{
static::assertIsArrayOfObjects($json);
PHPUnit::assertEquals(count($expected), count($json));

$expectedCount = count($expected);
$count = count($json);
PHPUnit::assertEquals(
$expectedCount,
$count,
sprintf(Messages::RESOURCE_LINKAGE_COLLECTION_HAVE_NOT_SAME_LENGTH, $count, $expectedCount)
);

$index = 0;
foreach ($expected as $resource) {
Expand Down Expand Up @@ -89,12 +101,16 @@ public static function assertResourceLinkageEquals($expected, $json, $strict)
static::assertIsValidResourceLinkage($json, $strict);

if ($expected === null) {
PHPUnit::assertNull($json);
PHPUnit::assertNull(
$json,
sprintf(Messages::RESOURCE_LINKAGE_MUST_BE_NULL, var_export($json, true))
);

return;
}

PHPUnit::assertNotNull($json);
PHPUnit::assertNotNull($json, Messages::RESOURCE_LINKAGE_MUST_NOT_BE_NULL);

/** @var array $json */

if (!static::isArrayOfObjects($expected)) {
Expand All @@ -105,7 +121,7 @@ public static function assertResourceLinkageEquals($expected, $json, $strict)
}

if (count($expected) == 0) {
PHPUnit::assertEmpty($json);
PHPUnit::assertEmpty($json, Messages::RESOURCE_LINKAGE_COLLECTION_MUST_BE_EMPTY);

return;
}
Expand Down
1 change: 0 additions & 1 deletion src/Constraint/PaginationLinksEqualConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\Constraint\Constraint;
use VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint;
use VGirol\JsonApiAssert\Members;

/**
* A constraint class to assert that a link object equals an expected value.
Expand Down
14 changes: 14 additions & 0 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,18 @@ abstract class Messages
'Failed asserting that the resource collection length (%u) is equal to %u.';
const RESOURCE_IS_NOT_EQUAL =
'Failed asserting that the resource %s is equal to %s.';
const PAGINATION_LINKS_NOT_EQUAL =
'Failed asserting that pagination links equal expected values.';
const RESOURCE_LINKAGE_COLLECTION_MUST_BE_EMPTY =
'Failed asserting that the resource linkage collection is empty.';
const RESOURCE_LINKAGE_COLLECTION_HAVE_NOT_SAME_LENGTH =
'Failed asserting that the resource linkage collection length (%u) is equal to %u.';
const RESOURCE_IDENTIFIER_IS_NOT_EQUAL =
'Failed asserting that the resource identifier %s is equal to %s.';
const RESOURCE_LINKAGE_MUST_BE_NULL =
'Failed asserting that the resource linkage %s is null.';
const RESOURCE_LINKAGE_MUST_NOT_BE_NULL =
'Failed asserting that the resource linkage is not null.';
const LINKS_OBJECT_HAVE_NOT_SAME_LENGTH =
'Failed asserting that the links collection length (%u) is equal to %u.';
}
Loading

0 comments on commit dbda340

Please sign in to comment.