Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created February 25, 2023 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remarkablemark/5e86d72c482487e94d9ee88dc484a50c to your computer and use it in GitHub Desktop.
Save remarkablemark/5e86d72c482487e94d9ee88dc484a50c to your computer and use it in GitHub Desktop.
PHPUnit example test
<?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