Skip to content

Commit

Permalink
fix: refactor configurable morph type and rewrite tests for pest
Browse files Browse the repository at this point in the history
* ci: load database migrations when running `phpstan`
* refactor: refactor configurable morph type code
* refactor: refactor tests to pest format
* test: fix tests
  • Loading branch information
marijoo authored Aug 21, 2024
1 parent 53df4fa commit 801fbbe
Show file tree
Hide file tree
Showing 24 changed files with 2,958 additions and 3,588 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
"illuminate/contracts": "^9.0|^10.0|^11.0"
},
"require-dev": {
"larastan/larastan": "^2.0.1",
"laravel/pint": "^1.0",
"mattiasgeniar/phpunit-query-count-assertions": "^1.1",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^6.1|^7.0|^8.0",
"larastan/larastan": "^2.0.1",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"pestphp/pest": "^1.21|^2.0",
"pestphp/pest": "^1.1|^2.35",
"pestphp/pest-plugin-laravel": "^1.1|^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.0|^10.0|^11.0"
"phpstan/phpstan-phpunit": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 3 additions & 11 deletions src/HasConfigurableMorphType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,19 @@ protected function morphType(): string
*/
protected function usesUniqueIdsInMorphType(): bool
{
return $this->morphType() !== 'integer';
return in_array($this->morphType(), ['uuid', 'ulid']);
}

/**
* Determine if the given value is a valid unique id.
*/
protected function isValidUniqueMorphId(mixed $value): bool
protected function isValidUniqueMorphId(string $value): bool
{
if (!is_string($value)) {
return false;
}

if ($this->morphType() === 'ulid') {
return Str::isUlid($value);
}

if ($this->morphType() === 'uuid') {
return Str::isUuid($value);
}

return false;
return Str::isUuid($value);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/MultiplexServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ class MultiplexServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
if ($this->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/multiplex.php' => config_path('multiplex.php'),
], 'multiplex-config');
}

if (config('multiplex.migrations', true)) {
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
$this->loadMultiplexMigrations();
}
}

protected function runningInConsole(): bool
{
return $this->app->runningInConsole();
}

protected function loadMultiplexMigrations(): void
{
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
}

public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/multiplex.php', 'multiplex');
Expand Down
69 changes: 25 additions & 44 deletions tests/DataTypeArrayHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
<?php

namespace Kolossal\Multiplex\Tests;

use Kolossal\Multiplex\DataType\ArrayHandler;
use PHPUnit\Framework\Attributes\Test;

final class DataTypeArrayHandlerTest extends TestCase
{
/** @test */
public function it_will_resolve_null_as_null(): void
{
$handler = new ArrayHandler;
it('will resolve null as null', function () {
$handler = new ArrayHandler;

$this->assertInstanceOf(ArrayHandler::class, $handler);
expect($handler)->toBeInstanceOf(ArrayHandler::class);

$this->assertNull($handler->unserializeValue(null));
}
expect($handler->unserializeValue(null))->toBeNull();
});

/** @test */
public function it_can_handle_arrays(): void
{
$handler = new ArrayHandler;
it('can handle arrays', function () {
$handler = new ArrayHandler;

$this->assertTrue($handler->canHandleValue(['id' => 123]));
}
expect($handler->canHandleValue(['id' => 123]))->toBeTrue();
});

/** @test */
public function it_cannot_handle_other_values(): void
{
$handler = new ArrayHandler;
it('cannot handle other values', function () {
$handler = new ArrayHandler;

$this->assertFalse($handler->canHandleValue((object) ['id' => 123]));
$this->assertFalse($handler->canHandleValue(123));
$this->assertFalse($handler->canHandleValue(false));
}
expect($handler->canHandleValue((object) ['id' => 123]))->toBeFalse();
expect($handler->canHandleValue(123))->toBeFalse();
expect($handler->canHandleValue(false))->toBeFalse();
});

/** @test */
public function it_serializes_value(): void
{
$handler = new ArrayHandler;
it('serializes value', function () {
$handler = new ArrayHandler;

$this->assertSame(
'{"id":123}',
$handler->serializeValue(['id' => 123])
);
}
expect($handler->serializeValue(['id' => 123]))->toBe('{"id":123}');
});

/** @test */
public function it_unserializes_value(): void
{
$handler = new ArrayHandler;
it('unserializes value', function () {
$handler = new ArrayHandler;

$value = $handler->unserializeValue('{"id":123}');
$value = $handler->unserializeValue('{"id":123}');

$this->assertIsArray($value);
expect($value)->toBeArray();

$this->assertSame(123, $value['id']);
}
}
expect($value['id'])->toBe(123);
});
91 changes: 34 additions & 57 deletions tests/DataTypeDateHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,38 @@
<?php

namespace Kolossal\Multiplex\Tests;

use Carbon\Carbon;
use Kolossal\Multiplex\DataType\DateHandler;
use PHPUnit\Framework\Attributes\Test;

final class DataTypeDateHandlerTest extends TestCase
{
/** @test */
public function it_will_parse_to_specified_date_format(): void
{
$handler = new DateHandler;

$this->assertSame(
'2022-04-01',
$handler->serializeValue('2022-04-01 14:00 Europe/Berlin')
);
}

/** @test */
public function it_will_unserialize_using_specified_date_format_if_possible(): void
{
$handler = new DateHandler;

$this->assertTrue(
$handler->unserializeValue('2022-04-01')->eq(Carbon::create(2022, 4, 1))
);
}

/** @test */
public function it_will_fallback_to_carbon_parse(): void
{
$handler = new DateHandler;

$this->assertTrue(
$handler->unserializeValue('01.04.2022 14:00 Europe/Berlin')->eq(Carbon::create(2022, 3, 31, 22))
);
}

/** @test */
public function it_can_handle_handle_date_strings(): void
{
$handler = new DateHandler;

$this->assertTrue($handler->canHandleValue('2024-03-29'));
$this->assertTrue($handler->canHandleValue('1970-01-01'));
}

/** @test */
public function it_cannot_handle_handle_invalid_values(): void
{
$handler = new DateHandler;

$this->assertFalse($handler->canHandleValue(now()));
$this->assertFalse($handler->canHandleValue('2024-3-29'));
$this->assertFalse($handler->canHandleValue('20240329'));
$this->assertFalse($handler->canHandleValue(true));
}
}

it('will parse to specified date format', function () {
$handler = new DateHandler;

expect($handler->serializeValue('2022-04-01 14:00 Europe/Berlin'))->toBe('2022-04-01');
});

it('will unserialize using specified date format if possible', function () {
$handler = new DateHandler;

expect($handler->unserializeValue('2022-04-01')->eq(Carbon::create(2022, 4, 1)))->toBeTrue();
});

it('will fallback to carbon parse', function () {
$handler = new DateHandler;

expect($handler->unserializeValue('01.04.2022 14:00 Europe/Berlin')->eq(Carbon::create(2022, 3, 31, 22)))->toBeTrue();
});

it('can handle handle date strings', function () {
$handler = new DateHandler;

expect($handler->canHandleValue('2024-03-29'))->toBeTrue();
expect($handler->canHandleValue('1970-01-01'))->toBeTrue();
});

it('cannot handle handle invalid values', function () {
$handler = new DateHandler;

expect($handler->canHandleValue(now()))->toBeFalse();
expect($handler->canHandleValue('2024-3-29'))->toBeFalse();
expect($handler->canHandleValue('20240329'))->toBeFalse();
expect($handler->canHandleValue(true))->toBeFalse();
});
43 changes: 12 additions & 31 deletions tests/DataTypeDateTimeHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
<?php

namespace Kolossal\Multiplex\Tests;

use Carbon\Carbon;
use Kolossal\Multiplex\DataType\DateTimeHandler;
use PHPUnit\Framework\Attributes\Test;

final class DataTypeDateTimeHandlerTest extends TestCase
{
/** @test */
public function it_will_parse_to_specified_datetime_format(): void
{
$handler = new DateTimeHandler;
it('will parse to specified datetime format', function () {
$handler = new DateTimeHandler;

$this->assertSame(
'2022-04-01 14:00:00.000000+0200',
$handler->serializeValue('2022-04-01 14:00 Europe/Berlin')
);
}
expect($handler->serializeValue('2022-04-01 14:00 Europe/Berlin'))->toBe('2022-04-01 14:00:00.000000+0200');
});

/** @test */
public function it_will_unserialize_using_specified_datetime_format_if_possible(): void
{
$handler = new DateTimeHandler;
it('will unserialize using specified datetime format if possible', function () {
$handler = new DateTimeHandler;

$this->assertTrue(
$handler->unserializeValue('2022-04-01 14:00:00.000000+0200')->eq(Carbon::create(2022, 4, 1, 12))
);
}
expect($handler->unserializeValue('2022-04-01 14:00:00.000000+0200')->eq(Carbon::create(2022, 4, 1, 12)))->toBeTrue();
});

/** @test */
public function it_will_fallback_to_carbon_parse(): void
{
$handler = new DateTimeHandler;
it('will fallback to carbon parse', function () {
$handler = new DateTimeHandler;

$this->assertTrue(
$handler->unserializeValue('01.04.2022 14:00 Europe/Berlin')->eq(Carbon::create(2022, 4, 1, 12))
);
}
}
expect($handler->unserializeValue('01.04.2022 14:00 Europe/Berlin')->eq(Carbon::create(2022, 4, 1, 12)))->toBeTrue();
});
Loading

0 comments on commit 801fbbe

Please sign in to comment.