Created
February 25, 2023 21:26
-
-
Save remarkablemark/5e86d72c482487e94d9ee88dc484a50c to your computer and use it in GitHub Desktop.
PHPUnit example test
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
<?php | |
declare(strict_types=1); | |
namespace App\Tests\Catalog\Value; | |
use App\Catalog\Value\Amount; | |
use PHPUnit\Framework\TestCase; | |
final class AmountTest extends TestCase | |
{ | |
protected function setUp(): void | |
{ | |
// ... | |
} | |
public function testGetCents_WithValidCents_ReturnsUnchangedCents(): void | |
{ | |
$amount = new Amount(1000); | |
$cents = $amount->getCents(); | |
self::assertEquals(1000, $cents); | |
} | |
/** | |
* @test | |
* @dataProvider getInvalidMarkupValues | |
* @param $cents | |
*/ | |
public function constructor_WithInvalidCents_ThrowsException($cents): void | |
{ | |
$this->expectException(\Exception::class); | |
new Amount(-1); | |
} | |
public function getInvalidMarkupValues(): array | |
{ | |
return [ | |
'int' => [0], | |
'sring' => ['0.25'], | |
'boolean' => [true], | |
'array' => [[]], | |
'null' => [null], | |
'object' => [new \stdClass()], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment