Skip to content

Instantly share code, notes, and snippets.

@Chemaclass
Last active June 20, 2021 11:05
Show Gist options
  • Save Chemaclass/9f7f96242153b696b3f8da5c7fa80461 to your computer and use it in GitHub Desktop.
Save Chemaclass/9f7f96242153b696b3f8da5c7fa80461 to your computer and use it in GitHub Desktop.
Unit testing effectively - Test example (Part II)
<?php
declare(strict_types=1);
namespace CompanyTest\Domain;
use PHPUnit\Framework\TestCase;
final class MyBusinessLogicTest extends TestCase
{
/**
* @dataProvider providerApplySomeLogic
*/
public function test_apply_some_logic(
array $concreteMapping,
string $argInput,
ReturnType $expected
): void {
$myBusinessLogic = $this->createBusinessLogic($concreteMapping);
$input = $this->createInput($argInput);
$returnType = $myBusinessLogic->applySomeLogic($input);
self::assertEquals($expected, $returnType);
}
public function providerApplySomeLogic(): Generator
{
yield 'scenario A' => [
'concreteMapping' => ['key' => 'value'],
'argInput' => 'something',
'expected' => ReturnType::withProperty('expected-value-A'),
];
yield 'scenario B' => [
'concreteMapping' => ['key2' => 'value2'],
'argInput' => 'something-else',
'expected' => ReturnType::withProperty('expected-value-B'),
];
}
private function createBusinessLogic(array $concreteMapping): MyBusinessLogic
{
return new MyBusinessLogic(
new FakeDependency(),
new ConcreteDependency($concreteMapping),
/* ... */
);
}
private function createInput(string $argInput): Input
{
return new Input($argInput, /* ... */);
}
}
final class FakeDependency implements DependencyInterface
{
public function getSomeValue(): string
{
return 'A value for your test';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment