Skip to content
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

Fix casting issue #2653

Merged
merged 29 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 20 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 Oct 28, 2023
7a6453f
Add castAttribute method at top of setAttribute method;
hans-thomas Oct 28, 2023
ff2aa65
Create IntegerTest.php
hans-thomas Oct 28, 2023
0bca346
WIP
hans-thomas Oct 28, 2023
5e5d3f6
Tests for float;
hans-thomas Oct 28, 2023
e4a4152
override asDecimal method;
hans-thomas Oct 28, 2023
06491a7
Tests for Decimal;
hans-thomas Oct 28, 2023
c601d78
Add tests for string;
hans-thomas Oct 28, 2023
c327eb0
Add tests for boolean;
hans-thomas Oct 28, 2023
75bf7c1
Override fromJson method;
hans-thomas Oct 28, 2023
b35c32d
Tests for object;
hans-thomas Oct 28, 2023
5b0a44e
Add tests for jsonl
hans-thomas Oct 28, 2023
5bbe816
Add tests for collection;
hans-thomas Oct 28, 2023
cf3b5cc
Add tests for date;
hans-thomas Oct 29, 2023
e26283e
Add tests for date;
hans-thomas Oct 29, 2023
4528599
Add tests for datetime;
hans-thomas Oct 29, 2023
81a1910
Merge branch '2257' of github.com:hans-thomas/laravel-mongodb into 2257
hans-thomas Oct 29, 2023
4bc7351
Update Model.php
hans-thomas Oct 29, 2023
572dd94
WIP
hans-thomas Oct 29, 2023
acaa868
Fix phpcs;
hans-thomas Oct 29, 2023
fdaa9dc
Update BooleanTest.php
hans-thomas Oct 30, 2023
fc504ee
Test values after update;
hans-thomas Oct 30, 2023
187cfc0
Another sentence used;
hans-thomas Oct 30, 2023
8d9736b
CastBinaryUuid merged into Casting;
hans-thomas Oct 30, 2023
64ef154
CastObjectId merged into Casting;
hans-thomas Oct 30, 2023
696135f
Revert "CastObjectId merged into Casting;"
hans-thomas Oct 30, 2023
6b81529
Float casted to integer test;
hans-thomas Oct 30, 2023
c715194
Add tests with \DateTime to DateTest;
hans-thomas Oct 30, 2023
9761807
Merge branch '4.1' into 2257
GromNaN Oct 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace MongoDB\Laravel\Eloquent;

use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException as BrickMathException;
use Brick\Math\RoundingMode;
use DateTimeInterface;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Casts\Json;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Exceptions\MathException;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use MongoDB\BSON\Binary;
use MongoDB\BSON\Decimal128;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Query\Builder as QueryBuilder;
Expand Down Expand Up @@ -205,6 +211,11 @@ protected function getAttributeFromArray($key)
/** @inheritdoc */
public function setAttribute($key, $value)
{
//Add casts
if ($this->hasCast($key)) {
$value = $this->castAttribute($key, $value);
}

// Convert _id to ObjectID.
if ($key === '_id' && is_string($value)) {
$builder = $this->newBaseQueryBuilder();
Expand All @@ -231,6 +242,28 @@ public function setAttribute($key, $value)
return parent::setAttribute($key, $value);
}

/** @inheritdoc */
protected function asDecimal($value, $decimals)
{
try {
$value = (string) BigDecimal::of((string) $value)->toScale((int) $decimals, RoundingMode::HALF_UP);

return new Decimal128($value);
} catch (BrickMathException $e) {
throw new MathException('Unable to cast value to a decimal.', previous: $e);
}
}

/** @inheritdoc */
public function fromJson($value, $asObject = false)
{
if (! is_string($value)) {
$value = Json::encode($value ?? '');
}

return Json::decode($value ?? '', ! $asObject);
}

/** @inheritdoc */
public function attributesToArray()
{
Expand Down
58 changes: 58 additions & 0 deletions tests/Casts/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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::assertEquals(true, $model->booleanValue);

$model->update(['booleanValue' => false]);
$check = Casting::query()->find($model->_id);

self::assertIsBool($check->booleanValue);
self::assertEquals(false, $check->booleanValue);

$model->update(['booleanValue' => 1]);
$check = Casting::query()->find($model->_id);

self::assertIsBool($check->booleanValue);
self::assertEquals(true, $check->booleanValue);

$model->update(['booleanValue' => 0]);
$check = Casting::query()->find($model->_id);

self::assertIsBool($check->booleanValue);
self::assertEquals(false, $check->booleanValue);
}

public function testBoolAsString(): void
{
$model = Casting::query()->create(['booleanValue' => '1.79']);

self::assertIsBool($model->booleanValue);
self::assertEquals(true, $model->booleanValue);

$model->update(['booleanValue' => '0']);
$check = Casting::query()->find($model->_id);

self::assertIsBool($check->booleanValue);
self::assertEquals(false, $check->booleanValue);
}
}
35 changes: 35 additions & 0 deletions tests/Casts/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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']]);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Collection::class, $check->collectionValue);
self::assertEquals(collect(['Dont let me go' => 'Even the longest of nights turn days']), $check->collectionValue);
}
}
56 changes: 56 additions & 0 deletions tests/Casts/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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 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()]);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Carbon::class, $check->dateField);
self::assertEquals(now()->subDay()->startOfDay()->format('Y-m-d H:i:s'), (string) $check->dateField);
}

public function testDateAsString(): void
{
$model = Casting::query()->create(['dateField' => '2023-10-29']);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Carbon::class, $check->dateField);
self::assertEquals(
Carbon::createFromTimestamp(1698577443)->startOfDay()->format('Y-m-d H:i:s'),
(string) $check->dateField,
);

$model->update(['dateField' => '2023-10-28']);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Carbon::class, $check->dateField);
self::assertEquals(
Carbon::createFromTimestamp(1698577443)->subDay()->startOfDay()->format('Y-m-d H:i:s'),
(string) $check->dateField,
);
}
}
55 changes: 55 additions & 0 deletions tests/Casts/DatetimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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()]);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Carbon::class, $check->datetimeField);
self::assertEquals(now()->subDay()->format('Y-m-d H:i:s'), (string) $check->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']);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Carbon::class, $check->datetimeField);
self::assertEquals(
Carbon::createFromTimestamp(1698577443)->subDay()->format('Y-m-d H:i:s'),
(string) $check->datetimeField,
);
}
}
47 changes: 47 additions & 0 deletions tests/Casts/DecimalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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]);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Decimal128::class, $model->decimalNumber);
self::assertEquals('9999.90', $check->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']);
$check = Casting::query()->find($model->_id);

self::assertInstanceOf(Decimal128::class, $model->decimalNumber);
self::assertEquals('795.00', $check->decimalNumber);
}
}
46 changes: 46 additions & 0 deletions tests/Casts/FloatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Casts;

use MongoDB\Laravel\Tests\Models\Casting;
use MongoDB\Laravel\Tests\TestCase;

class FloatTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Casting::truncate();
}

public function testFloat(): void
{
$model = Casting::query()->create(['floatNumber' => 1.79]);

self::assertIsFloat($model->floatNumber);
self::assertEquals(1.79, $model->floatNumber);

$model->update(['floatNumber' => 7E-5]);
$check = Casting::query()->find($model->_id);

self::assertIsFloat($check->floatNumber);
self::assertEquals(7E-5, $check->floatNumber);
}

public function testFloatAsString(): void
{
$model = Casting::query()->create(['floatNumber' => '1.79']);

self::assertIsFloat($model->floatNumber);
self::assertEquals(1.79, $model->floatNumber);

$model->update(['floatNumber' => '7E-5']);
$check = Casting::query()->find($model->_id);

self::assertIsFloat($check->floatNumber);
self::assertEquals(7E-5, $check->floatNumber);
}
}
Loading