Skip to content

Commit

Permalink
Fix call undefined method (#160)
Browse files Browse the repository at this point in the history
* Fix call undefined method

* Formatting

* Fix test name

* Fix load migrations
  • Loading branch information
ricardogobbosouza committed Mar 16, 2024
1 parent d54ebc5 commit 82d5062
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cknow\Money;

use BadMethodCallException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
Expand Down Expand Up @@ -190,6 +191,8 @@ public function __toString()
*
* @param string $method
* @return \Cknow\Money\Money|\Cknow\Money\Money[]|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, array $arguments)
{
Expand All @@ -198,7 +201,11 @@ public function __call($method, array $arguments)
}

if (! method_exists($this->money, $method)) {
return $this;
throw new BadMethodCallException(sprintf(
'Call to undefined method %s::%s()',
static::class,
$method
));
}

$result = call_user_func_array([$this->money, $method], static::getArguments($arguments));
Expand Down
8 changes: 5 additions & 3 deletions tests/MoneyCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class MoneyCastTest extends TestCase
{
use RefreshDatabase;

/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();
Expand All @@ -30,6 +27,11 @@ protected function setUp(): void
Money::setCurrencies(config('money.currencies'));
}

protected function defineDatabaseMigrations()
{
$this->loadMigrationsFrom(__DIR__.'/Database/Migrations');
}

public function testCastsMoneyWhenRetrievingCastedValues()
{
DB::table('users')->insert([
Expand Down
2 changes: 1 addition & 1 deletion tests/MoneyFormatterSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cknow\Money\Serializer\StringMoneySerializer;
use InvalidArgumentException;

class MoneySerializerTraitTest extends TestCase
class MoneyFormatterSerializerTest extends TestCase
{
public function testSerialize()
{
Expand Down
6 changes: 5 additions & 1 deletion tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cknow\Money\Tests;

use BadMethodCallException;
use Cknow\Money\Money;
use Money\Currency;

Expand Down Expand Up @@ -191,7 +192,10 @@ public function testAllocateTo()

public function testCallUndefinedMethod()
{
static::assertEquals(Money::USD(15), Money::USD(15)->undefined());
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method '.Money::class.'::undefined()');

Money::USD(15)->undefined();
}

public function testGetters()
Expand Down

0 comments on commit 82d5062

Please sign in to comment.