-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from juliomotol/fix/model-with-money-and-relat…
…ed-currency [Bug] Saving a record where currency is in related model
- Loading branch information
Showing
5 changed files
with
164 additions
and
4 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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Andriichuk\Laracash\Tests\Feature; | ||
|
||
use Andriichuk\Laracash\Tests\BaseTestCase; | ||
use Andriichuk\Laracash\Tests\Models\Order; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Money\Money; | ||
|
||
/** | ||
* Class ModelWithOneMoneyToOneCurrencyColumnTest | ||
* | ||
* @author Serhii Andriichuk <andriichuk29@gmail.com> | ||
*/ | ||
class ModelWithMoneyAndRelationCurrencyTest extends BaseTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->createOrdersTable(); | ||
$this->createOrderItemsTable(); | ||
} | ||
|
||
/** | ||
* @dataProvider moneyCasesProvider | ||
* | ||
* @param mixed $price | ||
*/ | ||
public function testMoneyCasts(array $options, Money $expected): void | ||
{ | ||
$orderModel = Order::create([ | ||
'total_price' => $options['price'], | ||
'currency' => $options['currency'], | ||
]); | ||
|
||
$orderModel->items()->create([ | ||
'unit_price' => $options['price'] | ||
]); | ||
|
||
$this->assertEquals($expected, $orderModel->total_price); | ||
$this->assertEquals($expected, $orderModel->items()->first()->unit_price); | ||
} | ||
|
||
public function moneyCasesProvider(): array | ||
{ | ||
return [ | ||
'from scalar' => [ | ||
[ | ||
'price' => 1000, | ||
'currency' => 'USD', | ||
], | ||
Money::USD(1000), | ||
], | ||
'from native Money object' => [ | ||
[ | ||
'price' => Money::USD(1000), | ||
'currency' => 'UAH', | ||
], | ||
Money::USD(1000), | ||
] | ||
]; | ||
} | ||
|
||
public function createOrdersTable(): void | ||
{ | ||
$this->app['db']->connection()->getSchemaBuilder()->create('orders', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('total_price'); | ||
$table->char('currency', 3); | ||
}); | ||
} | ||
|
||
public function createOrderItemsTable(): void | ||
{ | ||
$this->app['db']->connection()->getSchemaBuilder()->create('order_items', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->foreignId('order_id')->constrained(); | ||
$table->string('unit_price'); | ||
}); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace Andriichuk\Laracash\Tests\Models; | ||
|
||
use Andriichuk\Laracash\Casts\CurrencyCast; | ||
use Andriichuk\Laracash\Casts\MoneyCast; | ||
use Andriichuk\Laracash\Model\HasCurrency; | ||
use Andriichuk\Laracash\Model\HasCurrencyInterface; | ||
use Andriichuk\Laracash\Model\HasMoneyWithCurrency; | ||
use Andriichuk\Laracash\Model\HasMoneyWithCurrencyInterface; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Order extends Model implements HasMoneyWithCurrencyInterface, HasCurrencyInterface | ||
{ | ||
use HasMoneyWithCurrency; | ||
use HasCurrency; | ||
|
||
protected $table = 'orders'; | ||
|
||
public $timestamps = false; | ||
|
||
protected $fillable = ['total_price', 'currency']; | ||
|
||
protected $casts = [ | ||
'total_price' => MoneyCast::class, | ||
'currency' => CurrencyCast::class | ||
]; | ||
|
||
public function items() | ||
{ | ||
return $this->hasMany(OrderItem::class); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace Andriichuk\Laracash\Tests\Models; | ||
|
||
use Andriichuk\Laracash\Casts\MoneyCast; | ||
use Andriichuk\Laracash\Model\HasCurrency; | ||
use Andriichuk\Laracash\Model\HasCurrencyInterface; | ||
use Andriichuk\Laracash\Model\HasMoneyWithCurrency; | ||
use Andriichuk\Laracash\Model\HasMoneyWithCurrencyInterface; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Money\Currency; | ||
|
||
class OrderItem extends Model implements HasMoneyWithCurrencyInterface, HasCurrencyInterface | ||
{ | ||
use HasMoneyWithCurrency; | ||
use HasCurrency; | ||
|
||
protected $table = 'order_items'; | ||
|
||
public $timestamps = false; | ||
|
||
protected $fillable = ['unit_price']; | ||
|
||
protected $casts = [ | ||
'unit_price' => MoneyCast::class, | ||
]; | ||
|
||
public function order() | ||
{ | ||
return $this->belongsTo(Order::class); | ||
} | ||
|
||
public function getDefaultCurrencyFor(string $field): Currency | ||
{ | ||
return $this->order->currency; | ||
} | ||
|
||
public function getCurrencyColumnFor(string $field): string | ||
{ | ||
return ''; | ||
} | ||
} |