Last active
June 20, 2021 11:05
-
-
Save Chemaclass/9f7f96242153b696b3f8da5c7fa80461 to your computer and use it in GitHub Desktop.
Unit testing effectively - Test example (Part II)
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 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