wheesnoza/assertable-json-enhancer

A Laravel Assertable Json Enhancer Adding More Assertions Functions.

1.0.0 2024-08-28 05:07 UTC

This package is auto-updated.

Last update: 2024-08-29 13:18:21 UTC


README

Available Languages

wheesnoza/assertable-json-enhancer is a package that extends Laravel's AssertableJson class. This package enables more flexible and powerful assertions when testing JSON responses. It leverages the Macroable trait to provide several useful assertion methods.

Installation

You can install this package via Composer:

composer require wheesnoza/assertable-json-enhancer

Usage

After installation, this package automatically registers the necessary macros to the AssertableJson class. No additional setup is required.

Compatibility

This package is fully compatible with Laravel Inertia's AssertableInertia. You can use the methods provided by wheesnoza/assertable-json-enhancer when writing assertions for Inertia responses.

Available Methods

Table of Contents

  1. whereStringContains
  2. whereLessThan
  3. whereLessThanOrEqual
  4. whereGreaterThan
  5. whereGreaterThanOrEqual
  6. whereIsArray
  7. whereArrayHasAtLeast
  8. whereArrayHasSize
  9. whereStringStartsWith
  10. whereStringEndsWith
  11. whereExactLength
  12. whereMatchesPattern
  13. whereIsString
  14. whereIsInteger
  15. whereIsBoolean
  16. whereIsFloat
  17. whereIsEmpty
  18. whereIsNotEmpty
  19. whereStringEquals
  20. whereStringNotEquals
  21. whereResultsAreOrderedBy
  22. whereResultsContain
  23. whereResultsMatchCriteria

1. whereStringContains(string $key, string $substring)

Asserts that the string at the given key contains the specified substring.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereStringContains('data.name', 'John')
);

2. whereLessThan(string $key, int $value)

Asserts that the value at the given key is less than the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereLessThan('data.age', 18)
);

3. whereLessThanOrEqual(string $key, int $value)

Asserts that the value at the given key is less than or equal to the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereLessThanOrEqual('data.age', 18)
);

4. whereGreaterThan(string $key, int $value)

Asserts that the value at the given key is greater than the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereGreaterThan('data.age', 18)
);

5. whereGreaterThanOrEqual(string $key, int $value)

Asserts that the value at the given key is greater than or equal to the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereGreaterThanOrEqual('data.age', 18)
);

6. whereIsArray(string $key)

Asserts that the value at the given key is an array.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsArray('data.items')
);

7. whereArrayHasAtLeast(string $key, int $minCount)

Asserts that the array at the given key has at least the specified number of elements.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereArrayHasAtLeast('data.items', 3)
);

8. whereArrayHasSize(string $key, int $size)

Asserts that the array at the given key has exactly the specified number of elements.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereArrayHasSize('data.items', 3)
);

9. whereStringStartsWith(string $key, string $prefix)

Asserts that the string at the given key starts with the specified prefix.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereStringStartsWith('data.title', 'Laravel')
);

10. whereStringEndsWith(string $key, string $suffix)

Asserts that the string at the given key ends with the specified suffix.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereStringEndsWith('data.title', 'Framework')
);

11. whereExactLength(string $key, int $length)

Asserts that the string at the given key has exactly the specified length.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereExactLength('data.code', 10)
);

12. whereMatchesPattern(string $key, string $pattern)

Asserts that the string at the given key matches the specified regular expression pattern.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereMatchesPattern('data.email', '/^[\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4}$/')
);

13. whereIsString(string $key)

Asserts that the value at the given key is a string.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsString('data.name')
);

14. whereIsInteger(string $key)

Asserts that the value at the given key is an integer.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsInteger('data.age')
);

15. whereIsBoolean(string $key)

Asserts that the value at the given key is a boolean.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsBoolean('data.active')
);

16. whereIsFloat(string $key)

Asserts that the value at the given key is a float.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsFloat('data.price')
);

17. whereIsEmpty(string $key)

Asserts that the value at the given key is empty (null, empty string, empty array, etc.).

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsEmpty('data.name')
);

18. whereIsNotEmpty(string $key)

Asserts that the value at the given key is not empty.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereIsNotEmpty('data.name')
);

19. whereStringEquals(string $key, string $value)

Asserts that the string at the given key is exactly equal to the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereStringEquals('data.status', 'active')
);

20. whereStringNotEquals(string $key, string $value)

Asserts that the string at the given key is not equal to the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereStringNotEquals('data.status', 'inactive')
);

21. whereResultsAreOrderedBy(string $key, string $orderKey, string $direction = 'asc')

Asserts that the array at the given key is ordered by the specified key in the given direction.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereResultsAreOrderedBy('data.items', 'id', 'asc')


);

22. whereResultsContain(string $key, $value)

Asserts that the array at the given key contains the specified value.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereResultsContain('data.items', 2)
);

23. whereResultsMatchCriteria(string $key, array $criteria, bool $partialMatch = false)

Asserts that the array at the given key matches the specified criteria. If partialMatch is true, it allows partial matching of the criteria values.

$response->assertJson(fn (AssertableJson $json) =>
    $json->whereResultsMatchCriteria('data.items', ['name' => 'Laravel'], true)
);

Contributing

Contributions are welcome! Feel free to submit bug reports or pull requests. If you have any suggestions or improvements, please share them on GitHub.

License

This package is open-sourced software licensed under the MIT license.