Skip to content

Commit

Permalink
Remove support for spatie/data-transfer-object, improve support for s…
Browse files Browse the repository at this point in the history
…patie/laravel-data
  • Loading branch information
bastien-phi committed Sep 20, 2023
1 parent d657b55 commit d46c09e
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"spatie/data-transfer-object": "^3.9"
"spatie/laravel-data": "^3.9"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions src/Concerns/LaravelAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use PHPUnit\Framework\Assert;
use Soyhuce\Testing\Constraints\CollectionEquals;
use Soyhuce\Testing\Constraints\DataEquals;
use Soyhuce\Testing\Constraints\IsModel;
use function is_array;

Expand All @@ -32,4 +33,11 @@ public static function assertCollectionEquals(Collection|array $expected, mixed

Assert::assertThat($actual, $constraint, $message);
}

public static function assertDataEquals(\Spatie\LaravelData\Data $expected, mixed $actual, string $message = ''): void
{
$constraint = new DataEquals($expected);

Assert::assertThat($actual, $constraint, $message);
}
}
1 change: 1 addition & 0 deletions src/Constraints/CollectionEquals.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected function matches(mixed $other): bool
foreach ($this->value as $key => $value) {
$constraint = match (true) {
$value instanceof Model => new IsModel($value),
class_exists(\Spatie\LaravelData\Data::class) && $value instanceof \Spatie\LaravelData\Data => new DataEquals($value),
is_object($value) => new IsEqual($value),
default => new IsIdentical($value),
};
Expand Down
60 changes: 60 additions & 0 deletions src/Constraints/DataEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);

namespace Soyhuce\Testing\Constraints;

use Illuminate\Support\Collection;
use PHPUnit\Framework\Constraint\Constraint;
use SebastianBergmann\Comparator\ComparisonFailure;
use Spatie\LaravelData\Data;

class DataEquals extends Constraint
{
public function __construct(
protected Data $value,
) {
}

public function evaluate(mixed $other, string $description = '', bool $returnResult = false): ?bool
{
$success = $this->matches($other);

if ($returnResult) {
return $success;
}

if ($success) {
return null;
}

$failure = $other instanceof Data
? new ComparisonFailure(
$this->value,
$other,
$this->exporter()->export($this->value),
$this->exporter()->export($other)
)
: null;

$this->fail($other, $description, $failure);
}

protected function matches(mixed $other): bool
{
if (!$other instanceof Data) {
return false;
}

return (new CollectionEquals(Collection::make($this->value->all())))
->matches(Collection::make($other->all()));
}

protected function failureDescription(mixed $other): string
{
return $this->exporter()->export($other) . ' ' . $this->toString();
}

public function toString(): string
{
return 'is same data that ' . $this->exporter()->export($this->value);
}
}
15 changes: 12 additions & 3 deletions src/Match/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public static function make(mixed ...$expected): Closure
$value = self::isModel($value);
} elseif ($value instanceof Collection) {
$value = self::collectionEquals($value);
} elseif (class_exists(\Spatie\DataTransferObject\DataTransferObject::class) && $value instanceof \Spatie\DataTransferObject\DataTransferObject) {
$value = self::of($value::class)->properties(...$value->all())->toClosure();
} // TODO : drop support for Spatie DTO
} elseif (class_exists(\Spatie\LaravelData\Data::class) && $value instanceof \Spatie\LaravelData\Data) {
$value = self::dataEquals($value);
}

if (is_callable($value)) {
$result = $value($args[$key]);
Expand Down Expand Up @@ -64,6 +64,15 @@ public static function collectionEquals(Collection|array $expected): Closure
};
}

public static function dataEquals(\Spatie\LaravelData\Data $expected): Closure
{
return function (mixed $actual) use ($expected) {
LaravelAssert::assertDataEquals($expected, $actual);

return true;
};
}

/**
* @param class-string $class
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/SimpleData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Soyhuce\Testing\Tests\Fixtures;

use Spatie\LaravelData\Data;

class SimpleData extends Data
{
public function __construct(
public string $name,
public int $age,
) {
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Testing\Concerns\InteractsWithDeprecationHandling;
use Orchestra\Testbench\TestCase as Orchestra;
use Soyhuce\Testing\TestingServiceProvider;
use Spatie\LaravelData\LaravelDataServiceProvider;

/**
* @coversNothing
Expand All @@ -24,6 +25,7 @@ protected function getPackageProviders($app)
{
return [
TestingServiceProvider::class,
LaravelDataServiceProvider::class,
];
}
}
44 changes: 44 additions & 0 deletions tests/Unit/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Collection;
use PHPUnit\Framework\ExpectationFailedException;
use Soyhuce\Testing\Match\Matcher;
use Soyhuce\Testing\Tests\Fixtures\SimpleData;
use Soyhuce\Testing\Tests\TestCase;

/**
Expand Down Expand Up @@ -129,6 +130,49 @@ public function matcherFailsMatchingCollection(): void
Matcher::collectionEquals([1, 2, 3])($collection);
}

/**
* @test
* @covers ::make
*/
public function matcherMatchesData(): void
{
$expected = new SimpleData('foo', 1);
$value = SimpleData::from(['name' => 'foo', 'age' => 1]);
$value->getPartialTrees();

$result = Matcher::make($expected)($value);

$this->assertTrue($result);
}

/**
* @test
* @covers ::make
*/
public function matcherFailsMatchingData(): void
{
$expected = new SimpleData('foo', 1);
$value = SimpleData::from(['name' => 'bar', 'age' => 12]);

$this->expectException(ExpectationFailedException::class);
Matcher::make($expected)($value);
}

/**
* @test
* @covers ::make
*/
public function matcherMatchesCollectionOfData(): void
{
$expected = new SimpleData('foo', 1);
$value = SimpleData::from(['name' => 'foo', 'age' => 1]);
$value->getPartialTrees();

$result = Matcher::make(collect([$expected]))(collect([$value]));

$this->assertTrue($result);
}

/**
* @test
* @covers ::make
Expand Down

0 comments on commit d46c09e

Please sign in to comment.