Skip to content

Commit 47a53d9

Browse files
Readme
1 parent 200b046 commit 47a53d9

File tree

3 files changed

+87
-32
lines changed

3 files changed

+87
-32
lines changed

README.md

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
1-
# This is my package tabular-assertions
1+
# Write tabular assertions with Pest or PHPUnit
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/tabular-assertions.svg?style=flat-square)](https://packagist.org/packages/spatie/tabular-assertions)
44
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/spatie/tabular-assertions/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/spatie/tabular-assertions/actions?query=workflow%3Arun-tests+branch%3Amain)
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/spatie/tabular-assertions/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/spatie/tabular-assertions/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/tabular-assertions.svg?style=flat-square)](https://packagist.org/packages/spatie/tabular-assertions)
77

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
8+
**🚧 This package is under development! Documentation is scarce and it isn't published on Packagist yet but feel free to play around already.**
9+
10+
Tabular assertions allow you to describe data in a Markdown table-like format and compare it to the actual data. This is especially useful when comparing large, ordered data sets like financial data or a time series.
11+
12+
With Pest:
13+
14+
```php
15+
$users = [
16+
['id' => 20, 'name' => 'John Doe', 'email' => 'john@doe.com'],
17+
['id' => 1245, 'name' => 'Jane Doe', 'email' => 'jane@doe.com'],
18+
];
19+
20+
test('it compares a table', function () use ($users) {
21+
$order = Order::factory()
22+
->addItem('Pen', 2)
23+
->addItem('Paper', 1)
24+
->addItem('Pencil', 5)
25+
->create();
26+
27+
expect($order->items)->toMatchTable('
28+
| #id | #order_id | name | quantity |
29+
| #1 | #1 | Pen | 2 |
30+
| #2 | #1 | Paper | 1 |
31+
| #3 | #1 | Pencil | 5 |
32+
');
33+
});
34+
```
35+
36+
With PHPUnit:
37+
38+
```php
39+
use PHPUnit\Framework\TestCase;
40+
use Spatie\TabularAssertions\PHPUnit\TabularAssertions;
41+
42+
class PHPUnitTest extends TestCase
43+
{
44+
use TabularAssertions;
45+
46+
public function test_it_contains_users(): void
47+
{
48+
$order = Order::factory()
49+
->addItem('Pen', 2)
50+
->addItem('Paper', 1)
51+
->addItem('Pencil', 5)
52+
->create();
53+
54+
$this->assertMatchesTable('
55+
| #id | #order_id | name | quantity |
56+
| #1 | #1 | Pen | 2 |
57+
| #2 | #1 | Paper | 1 |
58+
| #3 | #1 | Pencil | 5 |
59+
', $order->items);
60+
}
61+
}
62+
```
963

1064
## Support us
1165

@@ -23,45 +77,43 @@ You can install the package via composer:
2377
composer require spatie/tabular-assertions
2478
```
2579

26-
You can publish and run the migrations with:
27-
28-
```bash
29-
php artisan vendor:publish --tag="tabular-assertions-migrations"
30-
php artisan migrate
31-
```
80+
## Usage
3281

33-
You can publish the config file with:
82+
### Basic usage: Pest
3483

35-
```bash
36-
php artisan vendor:publish --tag="tabular-assertions-config"
37-
```
84+
With Pest, the plugin will be autoloaded and readily available. Use the custom `toMatchTable()` expectation to compare data with a table.
3885

39-
This is the contents of the published config file:
86+
### Basic usage: PHPUnit
4087

41-
```php
42-
return [
43-
];
44-
```
88+
With PHPUnit, add the `Spatie\TabularAssertions\PHPUnit\TabularAssertions` trait to the tests you want to use tabular assertions with. Use `$this->assertMatchesTable()` to compare data with a table.
4589

46-
Optionally, you can publish the views using
90+
### Dynamic values
4791

48-
```bash
49-
php artisan vendor:publish --tag="tabular-assertions-views"
50-
```
92+
Sometimes you want to compare data without actually comparing the exact value. For example, you want to assert that each person is in the same team, but don't know the team ID because the data is randomly seeded on every run. A column can be marked as "dynamic" by prefixing its name with a `#`. Dynamic columns will replace values with placeholders. A placeholder is unique for the value in the column. So a team with ID `123` would always be rendered as `#1`, another team `456` with `#2` etc.
5193

52-
## Usage
94+
For example, Sebastian & Freek are in team Spatie which has a random ID, and Christoph is in team Laravel with another random ID.
5395

54-
```php
55-
$tabularAssertions = new Spatie\TabularAssertions();
56-
echo $tabularAssertions->echoPhrase('Hello, Spatie!');
96+
```
97+
| name | #team_id |
98+
| Sebastian | #1 |
99+
| Freek | #1 |
100+
| Christoph | #2 |
57101
```
58102

59103
## Testing
60104

105+
Tests are written with Pest. You can either use Pest's CLI or run `composer test` to run the suite.
106+
61107
```bash
62108
composer test
63109
```
64110

111+
In addition to tests, PhpStan statically analyses the code. Use `composer analyse` to run PhpStan.
112+
113+
```bash
114+
composer analyse
115+
```
116+
65117
## Changelog
66118

67119
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spatie/tabular-assertions",
3-
"description": "This is my package tabular-assertions",
3+
"description": "Write tabular assertions with Pest or PHPUnit",
44
"keywords": [
55
"spatie",
66
"tabular-assertions"
@@ -21,12 +21,12 @@
2121
"laravel/pint": "^1.0",
2222
"pestphp/pest": "^2.28",
2323
"phpstan/phpstan": "^1.10",
24+
"phpunit/phpunit": "^10.5",
2425
"spatie/ray": "^1.40"
2526
},
2627
"autoload": {
2728
"psr-4": {
28-
"Spatie\\TabularAssertions\\": "src/",
29-
"Spatie\\TabularAssertions\\Database\\Factories\\": "database/factories/"
29+
"Spatie\\TabularAssertions\\": "src/"
3030
},
3131
"files": ["src/Pest/Autoload.php"]
3232
},
@@ -37,6 +37,7 @@
3737
},
3838
"scripts": {
3939
"test": "vendor/bin/pest",
40+
"analyse": "vendor/bin/phpstan",
4041
"format": "vendor/bin/pint"
4142
},
4243
"config": {

tests/Integration/PHPUnitTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ class PHPUnitTest extends TestCase
1111

1212
public function test_it_compares_a_table(): void
1313
{
14+
$users = [
15+
['id' => 20, 'name' => 'John Doe', 'email' => 'john@doe.com'],
16+
['id' => 1245, 'name' => 'Jane Doe', 'email' => 'jane@doe.com'],
17+
];
18+
1419
$this->assertMatchesTable('
1520
| id | name | email |
1621
| 20 | John Doe | john@doe.com |
1722
| 1245 | Jane Doe | jane@doe.com |
18-
', [
19-
['id' => 20, 'name' => 'John Doe', 'email' => 'john@doe.com'],
20-
['id' => 1245, 'name' => 'Jane Doe', 'email' => 'jane@doe.com'],
21-
]);
23+
', $users);
2224
}
2325
}

0 commit comments

Comments
 (0)