Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 9, 2024
1 parent ad355a3 commit 830ec87
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@
"nette/php-generator": "^3.5",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.6|^8.0",
"pestphp/pest": "^1.22",
"pestphp/pest-plugin-laravel": "^1.3",
"pestphp/pest": "^1.22|^2.0",
"pestphp/pest-plugin-laravel": "^1.3|^2.0",
"phpbench/phpbench": "^1.2",
"phpstan/extension-installer": "^1.1",
"phpunit/phpunit": "^9.3",
"phpunit/phpunit": "^9.3|^10.0",
"spatie/invade": "^1.0",
"spatie/laravel-typescript-transformer": "^2.3",
"spatie/pest-plugin-snapshots": "^1.1",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/pest-plugin-snapshots": "^1.1|^2.0",
"spatie/test-time": "^1.2"
},
"autoload" : {
Expand Down
8 changes: 4 additions & 4 deletions tests/Attributes/Validation/PasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ function (callable|null $setDefaults, array $expectedConfig) {
}
)->with(function () {
yield 'min length set to 42' => [
'setDefaults' => fn () => fn () => ValidationPassword::defaults(fn () => ValidationPassword::min(42)),
'setDefaults' => fn () => ValidationPassword::defaults(fn () => ValidationPassword::min(42)),
'expectedConfig' => [
'min' => 42,
],
];

//
yield 'unconfigured' => [
'setDefaults' => fn () => fn () => ValidationPath::create(),
'setDefaults' => fn () => ValidationPath::create(),
'expectedConfig' => [
'min' => 8,
],
];

yield 'uncompromised' => [
'setDefaults' => fn () => fn () => ValidationPassword::defaults(fn () => ValidationPassword::min(69)->uncompromised(7)),
'setDefaults' => fn () => ValidationPassword::defaults(fn () => ValidationPassword::min(69)->uncompromised(7)),
'expectedConfig' => [
'min' => 69,
'uncompromised' => true,
Expand Down
4 changes: 2 additions & 2 deletions tests/DataPipes/CastPropertiesDataPipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
->float->toEqual(3.14)
->string->toEqual('Hello world')
->array->toEqual([1, 1, 2, 3, 5, 8])
->nullable->toBeNull([1, 1, 2, 3, 5, 8])
->nullable->toBeNull()
->undefinable->toBeInstanceOf(Optional::class)
->mixed->toEqual(42)
->defaultCast->toEqual(DateTime::createFromFormat(DATE_ATOM, '1994-05-16T12:00:00+01:00'))
Expand Down Expand Up @@ -110,7 +110,7 @@
->float->toEqual(3.14)
->string->toEqual('Hello world')
->array->toEqual([1, 1, 2, 3, 5, 8])
->nullable->toBeNull([1, 1, 2, 3, 5, 8])
->nullable->toBeNull()
->mixed->toBe(42)
->defaultCast->toEqual(DateTime::createFromFormat(DATE_ATOM, '1994-05-16T12:00:00+02:00'))
->explicitCast->toEqual(DateTime::createFromFormat('d-m-Y', '16-06-1994'))
Expand Down
31 changes: 30 additions & 1 deletion tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
use Spatie\LaravelData\Contracts\DataObject;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Dto;
use Spatie\LaravelData\Exceptions\CannotCreateData;
use Spatie\LaravelData\Exceptions\CannotSetComputedValue;
use Spatie\LaravelData\Lazy;
use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Resource;
use Spatie\LaravelData\Support\DataProperty;
use Spatie\LaravelData\Support\Transformation\TransformationContext;
use Spatie\LaravelData\Support\Transformation\TransformationContextFactory;
Expand All @@ -53,14 +55,15 @@
use Spatie\LaravelData\Tests\Fakes\SimpleDataWithMappedProperty;
use Spatie\LaravelData\Tests\Fakes\SimpleDataWithoutConstructor;
use Spatie\LaravelData\Tests\Fakes\SimpleDataWithWrap;
use Spatie\LaravelData\Tests\Fakes\SimpleDto;
use Spatie\LaravelData\Tests\Fakes\SimpleResource;
use Spatie\LaravelData\Tests\Fakes\Transformers\ConfidentialDataCollectionTransformer;
use Spatie\LaravelData\Tests\Fakes\Transformers\ConfidentialDataTransformer;
use Spatie\LaravelData\Tests\Fakes\Transformers\StringToUpperTransformer;
use Spatie\LaravelData\Tests\Fakes\UlarData;
use Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer;
use Spatie\LaravelData\Transformers\Transformer;
use Spatie\LaravelData\WithData;

use function Spatie\Snapshots\assertMatchesSnapshot;

it('can create a resource', function () {
Expand Down Expand Up @@ -1555,3 +1558,29 @@ public function transform(DataProperty $property, mixed $value, TransformationCo
'dateTime' => 'Custom transformed date',
]);
});

it('can use data as an DTO', function () {
$dto = SimpleDto::from('Hello World');

expect($dto)->toBeInstanceOf(SimpleDto::class)
->toBeInstanceOf(Dto::class)
->not()->toBeInstanceOf(Data::class)
->not()->toHaveMethods(['toArray', 'toJson', 'toResponse', 'all', 'include', 'exclude', 'only', 'except', 'transform', 'with', 'jsonSerialize'])
->and($dto->string)->toEqual('Hello World');

expect(fn() => SimpleDto::validate(['string' => null]))->toThrow(ValidationException::class);
});

it('can use data as an Resource', function () {
$resource = SimpleResource::from('Hello World');

expect($resource)->toBeInstanceOf(SimpleResource::class)
->toBeInstanceOf(Resource::class)
->not()->toBeInstanceOf(Data::class)
->toHaveMethods(['toArray', 'toJson', 'toResponse', 'all', 'include', 'exclude', 'only', 'except', 'transform', 'with', 'jsonSerialize'])
->and($resource->string)->toEqual('Hello World');

expect($resource)->not()->toHaveMethods([
'validate'
]);
});
File renamed without changes.

0 comments on commit 830ec87

Please sign in to comment.