-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix casting when setting an attribute (#2653)
- Loading branch information
1 parent
f5ed7bf
commit bc209f7
Showing
14 changed files
with
525 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Casts; | ||
|
||
use MongoDB\Laravel\Tests\Models\Casting; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class BooleanTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Casting::truncate(); | ||
} | ||
|
||
public function testBool(): void | ||
{ | ||
$model = Casting::query()->create(['booleanValue' => true]); | ||
|
||
self::assertIsBool($model->booleanValue); | ||
self::assertSame(true, $model->booleanValue); | ||
|
||
$model->update(['booleanValue' => false]); | ||
|
||
self::assertIsBool($model->booleanValue); | ||
self::assertSame(false, $model->booleanValue); | ||
|
||
$model->update(['booleanValue' => 1]); | ||
|
||
self::assertIsBool($model->booleanValue); | ||
self::assertSame(true, $model->booleanValue); | ||
|
||
$model->update(['booleanValue' => 0]); | ||
|
||
self::assertIsBool($model->booleanValue); | ||
self::assertSame(false, $model->booleanValue); | ||
} | ||
|
||
public function testBoolAsString(): void | ||
{ | ||
$model = Casting::query()->create(['booleanValue' => '1.79']); | ||
|
||
self::assertIsBool($model->booleanValue); | ||
self::assertSame(true, $model->booleanValue); | ||
|
||
$model->update(['booleanValue' => '0']); | ||
|
||
self::assertIsBool($model->booleanValue); | ||
self::assertSame(false, $model->booleanValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Casts; | ||
|
||
use Illuminate\Support\Collection; | ||
use MongoDB\Laravel\Tests\Models\Casting; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
use function collect; | ||
|
||
class CollectionTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Casting::truncate(); | ||
} | ||
|
||
public function testCollection(): void | ||
{ | ||
$model = Casting::query()->create(['collectionValue' => ['g' => 'G-Eazy']]); | ||
|
||
self::assertInstanceOf(Collection::class, $model->collectionValue); | ||
self::assertEquals(collect(['g' => 'G-Eazy']), $model->collectionValue); | ||
|
||
$model->update(['collectionValue' => ['Dont let me go' => 'Even the longest of nights turn days']]); | ||
|
||
self::assertInstanceOf(Collection::class, $model->collectionValue); | ||
self::assertEquals(collect(['Dont let me go' => 'Even the longest of nights turn days']), $model->collectionValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Casts; | ||
|
||
use DateTime; | ||
use Illuminate\Support\Carbon; | ||
use MongoDB\Laravel\Tests\Models\Casting; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
use function now; | ||
|
||
class DateTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Casting::truncate(); | ||
} | ||
|
||
public function testDate(): void | ||
{ | ||
$model = Casting::query()->create(['dateField' => now()]); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->dateField); | ||
self::assertEquals(now()->startOfDay()->format('Y-m-d H:i:s'), (string) $model->dateField); | ||
|
||
$model->update(['dateField' => now()->subDay()]); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->dateField); | ||
self::assertEquals(now()->subDay()->startOfDay()->format('Y-m-d H:i:s'), (string) $model->dateField); | ||
|
||
$model->update(['dateField' => new DateTime()]); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->dateField); | ||
self::assertEquals(now()->startOfDay()->format('Y-m-d H:i:s'), (string) $model->dateField); | ||
|
||
$model->update(['dateField' => (new DateTime())->modify('-1 day')]); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->dateField); | ||
self::assertEquals(now()->subDay()->startOfDay()->format('Y-m-d H:i:s'), (string) $model->dateField); | ||
} | ||
|
||
public function testDateAsString(): void | ||
{ | ||
$model = Casting::query()->create(['dateField' => '2023-10-29']); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->dateField); | ||
self::assertEquals( | ||
Carbon::createFromTimestamp(1698577443)->startOfDay()->format('Y-m-d H:i:s'), | ||
(string) $model->dateField, | ||
); | ||
|
||
$model->update(['dateField' => '2023-10-28']); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->dateField); | ||
self::assertEquals( | ||
Carbon::createFromTimestamp(1698577443)->subDay()->startOfDay()->format('Y-m-d H:i:s'), | ||
(string) $model->dateField, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Casts; | ||
|
||
use Illuminate\Support\Carbon; | ||
use MongoDB\Laravel\Tests\Models\Casting; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
use function now; | ||
|
||
class DatetimeTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Casting::truncate(); | ||
} | ||
|
||
public function testDate(): void | ||
{ | ||
$model = Casting::query()->create(['datetimeField' => now()]); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->datetimeField); | ||
self::assertEquals(now()->format('Y-m-d H:i:s'), (string) $model->datetimeField); | ||
|
||
$model->update(['datetimeField' => now()->subDay()]); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->datetimeField); | ||
self::assertEquals(now()->subDay()->format('Y-m-d H:i:s'), (string) $model->datetimeField); | ||
} | ||
|
||
public function testDateAsString(): void | ||
{ | ||
$model = Casting::query()->create(['datetimeField' => '2023-10-29']); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->datetimeField); | ||
self::assertEquals( | ||
Carbon::createFromTimestamp(1698577443)->startOfDay()->format('Y-m-d H:i:s'), | ||
(string) $model->datetimeField, | ||
); | ||
|
||
$model->update(['datetimeField' => '2023-10-28 11:04:03']); | ||
|
||
self::assertInstanceOf(Carbon::class, $model->datetimeField); | ||
self::assertEquals( | ||
Carbon::createFromTimestamp(1698577443)->subDay()->format('Y-m-d H:i:s'), | ||
(string) $model->datetimeField, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Casts; | ||
|
||
use MongoDB\BSON\Decimal128; | ||
use MongoDB\Laravel\Tests\Models\Casting; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class DecimalTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Casting::truncate(); | ||
} | ||
|
||
public function testDecimal(): void | ||
{ | ||
$model = Casting::query()->create(['decimalNumber' => 100.99]); | ||
|
||
self::assertInstanceOf(Decimal128::class, $model->decimalNumber); | ||
self::assertEquals('100.99', $model->decimalNumber); | ||
|
||
$model->update(['decimalNumber' => 9999.9]); | ||
|
||
self::assertInstanceOf(Decimal128::class, $model->decimalNumber); | ||
self::assertEquals('9999.90', $model->decimalNumber); | ||
} | ||
|
||
public function testDecimalAsString(): void | ||
{ | ||
$model = Casting::query()->create(['decimalNumber' => '120.79']); | ||
|
||
self::assertInstanceOf(Decimal128::class, $model->decimalNumber); | ||
self::assertEquals('120.79', $model->decimalNumber); | ||
|
||
$model->update(['decimalNumber' => '795']); | ||
|
||
self::assertInstanceOf(Decimal128::class, $model->decimalNumber); | ||
self::assertEquals('795.00', $model->decimalNumber); | ||
} | ||
} |
Oops, something went wrong.