Skip to content

Commit

Permalink
UPDATE docs to be a little clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveLiddament committed Dec 14, 2023
1 parent ca0a9f6 commit c23dc72
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,7 @@ use DaveLiddament\PhpstanRuleTestHelper\AbstractRuleTestCase;

class CallableFromRuleTest extends AbstractRuleTestCase
{
protected function getRule(): Rule
{
return new CallableFromRule($this->createReflectionProvider());
}

public function testAllowedCall(): void
{
$this->assertIssuesReported(__DIR__ . '/Fixtures/SomeCode.php');
}
// `getRule` and `testAllowedCall` methods omitted for brevity

protected function getErrorFormatter(): string
{
Expand All @@ -81,7 +73,9 @@ class CallableFromRuleTest extends AbstractRuleTestCase
}
```

The fixture file is simplified as there is no need to specify the error message. Any lines where an error is expected need to end with `// ERROR`.
The fixture file is simplified as there is no need to specify the error message.
Any lines where an error is expected need to end with `// ERROR`, the expected error message is taken from the `getErrorFormatter` method.

#### Fixture:

```php
Expand All @@ -101,6 +95,15 @@ class SomeCode
}
```

The expected error messages would be:

- Line 6: `Can not call method`
- Line 12: `Can not call method`

The benefits of this approach are no duplication of the error message text.
Any changes to the error message only need to be made in one place in the test case.


### Adding context to error messages

Good error message require context. The context is added to the fixture file after `// ERROR `. Multiple pieces of context can be added by separating them with the `|` character.
Expand All @@ -111,15 +114,7 @@ use DaveLiddament\PhpstanRuleTestHelper\AbstractRuleTestCase;

class CallableFromRuleTest extends AbstractRuleTestCase
{
protected function getRule(): Rule
{
return new CallableFromRule($this->createReflectionProvider());
}

public function testAllowedCall(): void
{
$this->assertIssuesReported(__DIR__ . '/Fixtures/SomeCode.php');
}
// `getRule` and `testAllowedCall` methods omitted for brevity

protected function getErrorFormatter(): string
{
Expand All @@ -128,7 +123,7 @@ class CallableFromRuleTest extends AbstractRuleTestCase
}
```

The fixture file is simplified as there is no need to specify the error message. Any lines where an error is expected need to end with `// ERROR`.

#### Fixture:

```php
Expand Down Expand Up @@ -157,22 +152,16 @@ The expected error messages would be:

If you need more flexibility in the error message, you can return an object that implements the `ErrorMessageFormatter` [interface](src/ErrorMessageFormatter.php).

In the example below the message changes depending on the number of parts in the error context.
In the example below the message changes depending on the number of parts in the error context.

NOTE: This is a contrived example, but it shows how you can use `ErrorMessageFormatter` to create more flexible error messages.

```php
use DaveLiddament\PhpstanRuleTestHelper\AbstractRuleTestCase;

class CallableFromRuleTest extends AbstractRuleTestCase
{
protected function getRule(): Rule
{
return new CallableFromRule($this->createReflectionProvider());
}

public function testAllowedCall(): void
{
$this->assertIssuesReported(__DIR__ . '/Fixtures/SomeCode.php');
}
// `getRule` and `testAllowedCall` methods omitted for brevity

protected function getErrorFormatter(): ErrorMessageFormatter
{
Expand Down

0 comments on commit c23dc72

Please sign in to comment.