-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix casting issue #2653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix casting issue #2653
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f100aa7
Create Casting model to test casts;
hans-thomas 7a6453f
Add castAttribute method at top of setAttribute method;
hans-thomas ff2aa65
Create IntegerTest.php
hans-thomas 0bca346
WIP
hans-thomas 5e5d3f6
Tests for float;
hans-thomas e4a4152
override asDecimal method;
hans-thomas 06491a7
Tests for Decimal;
hans-thomas c601d78
Add tests for string;
hans-thomas c327eb0
Add tests for boolean;
hans-thomas 75bf7c1
Override fromJson method;
hans-thomas b35c32d
Tests for object;
hans-thomas 5b0a44e
Add tests for jsonl
hans-thomas 5bbe816
Add tests for collection;
hans-thomas cf3b5cc
Add tests for date;
hans-thomas e26283e
Add tests for date;
hans-thomas 4528599
Add tests for datetime;
hans-thomas 81a1910
Merge branch '2257' of github.com:hans-thomas/laravel-mongodb into 2257
hans-thomas 4bc7351
Update Model.php
hans-thomas 572dd94
WIP
hans-thomas acaa868
Fix phpcs;
hans-thomas fdaa9dc
Update BooleanTest.php
hans-thomas fc504ee
Test values after update;
hans-thomas 187cfc0
Another sentence used;
hans-thomas 8d9736b
CastBinaryUuid merged into Casting;
hans-thomas 64ef154
CastObjectId merged into Casting;
hans-thomas 696135f
Revert "CastObjectId merged into Casting;"
hans-thomas 6b81529
Float casted to integer test;
hans-thomas c715194
Add tests with \DateTime to DateTest;
hans-thomas 9761807
Merge branch '4.1' into 2257
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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()]); | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.