Skip to content

Commit f20efae

Browse files
committed
fix toBe expectation
1 parent ab18b24 commit f20efae

File tree

9 files changed

+87
-34
lines changed

9 files changed

+87
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ coverage.xml
99
.temp/coverage.php
1010
*.swp
1111
*.swo
12+
/.phpunit.cache/

docs/11.expectations/8.time.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ menuTitle: Time
55

66
### `toBeAfter()`
77

8+
Assert the date equals the given one.
9+
10+
```php
11+
expect('2021-10-20')->toBe(today());
12+
```
13+
14+
### `toBeAfter()`
15+
816
Assert the date is after the given one.
917

1018
```php

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ parameters:
99
- src
1010

1111
checkMissingIterableValueType: true
12-
checkGenericClassInNonGenericObjectType: false
12+
1313
reportUnmatchedIgnoredErrors: false
1414

1515
ignoreErrors:
16+
- identifier: missingType.generics
1617
- "#Undefined variable: \\$this#"
1718
- "#Call to static method user\\(\\) on an unknown class Auth#"
1819
- "#Variable method call on Illuminate\\\\Database\\\\Eloquent\\\\Model#"

phpunit.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4-
colors="true"
5-
>
6-
<testsuites>
7-
<testsuite name="default">
8-
<directory suffix=".php">./tests/Expect</directory>
9-
</testsuite>
10-
</testsuites>
11-
<coverage processUncoveredFiles="true">
12-
<include>
13-
<directory suffix=".php">./src</directory>
14-
</include>
15-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" colors="true" cacheDirectory=".phpunit.cache">
3+
<testsuites>
4+
<testsuite name="default">
5+
<directory suffix=".php">./tests/Expect</directory>
6+
</testsuite>
7+
</testsuites>
8+
<source>
9+
<include>
10+
<directory suffix=".php">./src</directory>
11+
</include>
12+
</source>
1613
</phpunit>

src/Expectations/Models.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ function (Model $related, string $relationshipName = ''): Expectation {
143143
}
144144
);
145145

146-
expect()->intercept('toBe', Model::class, fn (Model $anotherModel) => expect($this->value)->toBeSameModelAs($anotherModel)); //@phpstan-ignore-line
146+
expect()->intercept('toBe', Model::class, fn (Model $anotherModel) => expect($this->value)->toBeSameModelAs($anotherModel)); // @phpstan-ignore-line

src/Expectations/Time.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@
44

55
declare(strict_types=1);
66

7-
use Carbon\CarbonInterface;
7+
use Carbon\Carbon;
88
use DefStudio\PestLaravelExpectations\Helpers\ValueProcessor;
99
use Pest\Expectation;
1010

1111
use function PHPUnit\Framework\assertTrue;
1212

13-
expect()->intercept(
13+
expect()->pipe(
1414
'toBe',
15-
CarbonInterface::class,
16-
function (CarbonInterface $expected) {
17-
expect($this->value->timestamp)->toBe($expected->timestamp);
15+
function (Closure $next, mixed $date) {
16+
try {
17+
$value = Carbon::make($this->value);
18+
$expected = Carbon::make($date);
19+
} catch (Exception) { // @phpstan-ignore-line
20+
return $next();
21+
}
22+
23+
if ($value === null) {
24+
return $next();
25+
}
26+
27+
if ($expected === null) {
28+
return $next();
29+
}
30+
31+
return expect($value->timestamp)->toBe($expected->timestamp, sprintf('Failed to assert that date [%s] is equal to [%s]', $value->toString(), $expected->toString()));
1832
}
1933
);
2034

src/Helpers/Models/RelationshipGuesser.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,40 @@ public function throwException(bool $throw): self
6969

7070
public function guess(): string
7171
{
72-
if (! empty($this->hintedRelationshipName)) {
72+
if ($this->hintedRelationshipName !== '' && $this->hintedRelationshipName !== '0') {
7373
$this->foundRelationshipName = $this->hintedRelationshipName;
7474
}
7575

7676
if ($this->relationshipClass == BelongsTo::class) {
77-
$this->try(Str::camel(class_basename($this->related))); //@phpstan-ignore-line
78-
$this->try(Str::snake(class_basename($this->related))); //@phpstan-ignore-line
77+
$this->try(Str::camel(class_basename($this->related))); // @phpstan-ignore-line
78+
$this->try(Str::snake(class_basename($this->related))); // @phpstan-ignore-line
7979
}
8080

8181
if ($this->relationshipClass == HasOne::class) {
82-
$this->try(Str::camel(class_basename($this->related))); //@phpstan-ignore-line
83-
$this->try(Str::snake(class_basename($this->related))); //@phpstan-ignore-line
82+
$this->try(Str::camel(class_basename($this->related))); // @phpstan-ignore-line
83+
$this->try(Str::snake(class_basename($this->related))); // @phpstan-ignore-line
8484
}
8585

8686
if ($this->relationshipClass == HasMany::class) {
87-
$this->try(Str::camel(Str::plural(class_basename($this->related)))); //@phpstan-ignore-line
88-
$this->try(Str::snake(Str::plural(class_basename($this->related)))); //@phpstan-ignore-line
87+
$this->try(Str::camel(Str::plural(class_basename($this->related)))); // @phpstan-ignore-line
88+
$this->try(Str::snake(Str::plural(class_basename($this->related)))); // @phpstan-ignore-line
8989
}
9090

91-
if ($this->throwException && empty($this->foundRelationshipName)) {
91+
if ($this->throwException && ($this->foundRelationshipName === '' || $this->foundRelationshipName === '0')) {
9292
$triedNames = implode(' / ', array_unique($this->triedRelationshipNames));
93-
throw new ExpectationFailedException(sprintf('Failed to assert that [%s] has relationship [%s]', $this->model::class, $triedNames)); //@phpstan-ignore-line
93+
throw new ExpectationFailedException(sprintf('Failed to assert that [%s] has relationship [%s]', $this->model::class, $triedNames)); // @phpstan-ignore-line
9494
}
9595

9696
return $this->foundRelationshipName;
9797
}
9898

9999
private function try(string $relationshipName): void
100100
{
101-
if (empty($relationshipName)) {
101+
if ($relationshipName === '' || $relationshipName === '0') {
102102
return;
103103
}
104104

105-
if (! empty($this->foundRelationshipName)) {
105+
if ($this->foundRelationshipName !== '' && $this->foundRelationshipName !== '0') {
106106
return;
107107
}
108108

@@ -118,7 +118,7 @@ private function try(string $relationshipName): void
118118
private function validateRelationship(string $relationshipName): void
119119
{
120120
if (! $this->model->{$relationshipName}() instanceof $this->relationshipClass) {
121-
throw new ExpectationFailedException(sprintf('Failed to assert that [%s] has relationship [%s] of type [%s]', $this->model::class, $relationshipName, $this->relationshipClass)); //@phpstan-ignore-line
121+
throw new ExpectationFailedException(sprintf('Failed to assert that [%s] has relationship [%s] of type [%s]', $this->model::class, $relationshipName, $this->relationshipClass)); // @phpstan-ignore-line
122122
}
123123
}
124124

src/Helpers/ValueProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function getCarbonDate(mixed $value): CarbonInterface
2020
{
2121
$carbon = Carbon::make($value);
2222

23-
if (! $carbon instanceof \Carbon\Carbon) {
23+
if (! $carbon instanceof Carbon) {
2424
throw InvalidDataException::cannotCast($value, Carbon::class);
2525
}
2626

tests/Expect/Time/toBe.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Carbon\Carbon;
4+
use PHPUnit\Framework\ExpectationFailedException;
5+
6+
test('pass with string values', function () {
7+
expect('2021-01-02 01:05:36')->toBe(new DateTime('2021-01-02 01:05:36'));
8+
});
9+
10+
test('pass with string expectation', function () {
11+
expect(new DateTime('2021-01-02 01:05:36'))->toBe('2021-01-02 01:05:36');
12+
});
13+
14+
test('pass with datetime values', function () {
15+
expect(new DateTime('2021-01-02 01:05:36'))->toBe(new DateTime('2021-01-02 01:05:36'));
16+
});
17+
18+
test('pass with carbon values', function () {
19+
expect(Carbon::make('2021-01-02 01:05:36'))->toBe(Carbon::make('2021-01-02 01:05:36'));
20+
});
21+
22+
test('fails', function () {
23+
expect(new DateTime('2021-01-01'))->toBe(new DateTime('2021-01-02'));
24+
})->throws(ExpectationFailedException::class, 'Failed to assert that date [Fri Jan 01 2021 00:00:00 GMT+0000] is equal to [Sat Jan 02 2021 00:00:00 GMT+0000]');
25+
26+
test('pass negated', function () {
27+
expect(now())->not->toBe(now()->addHour());
28+
});
29+
30+
test('fails negated', function () {
31+
expect(Carbon::make('2021-01-02'))->not->toBe(Carbon::make('2021-01-02'));
32+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)