Skip to content

Commit

Permalink
add test for cast
Browse files Browse the repository at this point in the history
  • Loading branch information
opheus2 committed Jun 1, 2024
1 parent e62dcf7 commit 267aa9a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/Casts/CountryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ public function __construct(
$this->isoCodeVersion = $isoCodeVersion;
}

/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, $value, array $attributes)
public function get($model, string $key, $value, array $attributes)
{
if (is_null($value)) {
return null;
Expand Down
45 changes: 45 additions & 0 deletions tests/CountryCastTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Orpheus\LaravelCountries\Tests;

class CountryCastTest extends LaravelCountriesTestCase
{

/** @var \Orpheus\LaravelCountries\Country $country */
protected $country;

public function setUp(): void
{
parent::setUp();

$this->country = $this->countries->getByAlpha2Code('CA');
}


/** @test */
public function it_casts_country_to_alpha2_code()
{
$cast = new \Orpheus\LaravelCountries\Casts\CountryCast('alpha2');

$this->assertEquals('CA', $cast->set(null, 'key', $this->country, []));
$this->assertEquals($this->country, $cast->get(null, 'key', 'CA', []));
}

/** @test */
public function it_casts_country_to_alpha3_code()
{
$cast = new \Orpheus\LaravelCountries\Casts\CountryCast('alpha3');

$this->assertEquals('CAN', $cast->set(null, 'key', $this->country, []));
$this->assertEquals($this->country, $cast->get(null, 'key', 'CAN', []));
}

/** @test */
public function it_casts_country_to_numeric_code()
{
$cast = new \Orpheus\LaravelCountries\Casts\CountryCast('numeric');

$this->assertEquals(124, $cast->set(null, 'key', $this->country, []));
$this->assertEquals($this->country, $cast->get(null, 'key', 124, []));
}
}

0 comments on commit 267aa9a

Please sign in to comment.