-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace TRegx\PhpUnit\DataProviders\Internal\Provider; | ||
|
||
use TRegx\PhpUnit\DataProviders\DataProvider; | ||
use TRegx\PhpUnit\DataProviders\Internal\Frame\DataRow; | ||
|
||
class MapProvider extends DataProvider | ||
{ | ||
/** @var DataProvider */ | ||
private $provider; | ||
/** @var Mapper */ | ||
private $mapper; | ||
|
||
public function __construct(DataProvider $provider, callable $mapper) | ||
{ | ||
$this->provider = $provider; | ||
$this->mapper = new Mapper($mapper); | ||
} | ||
|
||
protected function dataset(): array | ||
{ | ||
return \array_map([$this, 'mapped'], $this->provider->dataset()); | ||
} | ||
|
||
private function mapped(DataRow $row): DataRow | ||
{ | ||
return $row->set($this->mapper->apply($row->values)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace TRegx\PhpUnit\DataProviders\Internal\Provider; | ||
|
||
use TRegx\PhpUnit\DataProviders\Internal\Type; | ||
use UnexpectedValueException; | ||
|
||
class Mapper | ||
{ | ||
/** @var callable */ | ||
private $mapper; | ||
|
||
public function __construct(callable $mapper) | ||
{ | ||
$this->mapper = $mapper; | ||
} | ||
|
||
public function apply(array $arguments): array | ||
{ | ||
$mapped = ($this->mapper)(...$arguments); | ||
if (\is_array($mapped)) { | ||
return \array_values($mapped); | ||
} | ||
throw new UnexpectedValueException('Failed to map data row as array, given: ' . new Type($mapped)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace Test\Fixture; | ||
|
||
class Functions | ||
{ | ||
public static function constant($value): callable | ||
{ | ||
return function () use ($value) { | ||
return $value; | ||
}; | ||
} | ||
|
||
public static function toArray(): callable | ||
{ | ||
return function ($argument): array { | ||
return [$argument]; | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
namespace Test\Unit\entry; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Test\Fixture\Assertion\AssertsIteration; | ||
use Test\Fixture\Executes; | ||
use Test\Fixture\Functions; | ||
use Test\Fixture\TestCase\TestCaseExactMessage; | ||
use TRegx\PhpUnit\DataProviders\DataProvider; | ||
|
||
class MapTest extends TestCase | ||
{ | ||
use AssertsIteration, TestCaseExactMessage, Executes; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldMap() | ||
{ | ||
// given | ||
$provider = DataProvider::tuples( | ||
['Joffrey', 'Baratheon'], | ||
['Cersei', 'Lannister'], | ||
['Ilyn', 'Payne'], | ||
['Sandor', 'Clegane']); | ||
// when | ||
$mapped = $provider->map(function (string $name, string $lastName): array { | ||
return [ | ||
\strToLower($name), | ||
\strToUpper($lastName) | ||
]; | ||
}); | ||
// then | ||
$this->assertIteratesValues($mapped, [ | ||
['joffrey', 'BARATHEON'], | ||
['cersei', 'LANNISTER'], | ||
['ilyn', 'PAYNE'], | ||
['sandor', 'CLEGANE'], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider invalidValues | ||
*/ | ||
public function shouldThrow($value, string $name) | ||
{ | ||
// given | ||
$mapped = DataProvider::tuples(['Valar', 'Morghulis']) | ||
->map(Functions::constant($value)); | ||
// then | ||
$this->expectException(\UnexpectedValueException::class); | ||
$this->expectExceptionMessage("Failed to map data row as array, given: $name"); | ||
// when | ||
$this->execute($mapped); | ||
} | ||
|
||
public function invalidValues(): DataProvider | ||
{ | ||
return DataProvider::tuples( | ||
['invalid', "(string) 'invalid'"], | ||
[4, '(int) [4]'], | ||
[3.14, '(float) [3.14]'], | ||
[true, '(bool) true'], | ||
[new \stdClass(), '(object) \stdClass'] | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldAcceptPhpCallable() | ||
{ | ||
// given | ||
$tuples = DataProvider::tuples( | ||
['Joffrey'], | ||
['Cersei'], | ||
['Ilyn Payne']); | ||
// when | ||
$mapped = $tuples->map('str_split'); | ||
// then | ||
$this->assertIteratesValues($mapped, [ | ||
['J', 'o', 'f', 'f', 'r', 'e', 'y'], | ||
['C', 'e', 'r', 's', 'e', 'i'], | ||
['I', 'l', 'y', 'n', ' ', 'P', 'a', 'y', 'n', 'e'], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldIgnoreKeysInReturnArray() | ||
{ | ||
// given | ||
$provider = DataProvider::list('argument'); | ||
// when | ||
$mapped = $provider->map(Functions::constant(['key' => 'value'])); | ||
// then | ||
$this->assertIteratesValues($mapped, [ | ||
['value'], | ||
]); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace Test\Unit\names; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Test\Fixture\Assertion\AssertsIteration; | ||
use Test\Fixture\Functions; | ||
use TRegx\PhpUnit\DataProviders\DataProvider; | ||
|
||
class MapTest extends TestCase | ||
{ | ||
use AssertsIteration; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldName() | ||
{ | ||
// given | ||
$list = DataProvider::tuples( | ||
['Joffrey', 'Baratheon'], | ||
['Cersei', 'Lannister'], | ||
['Ilyn', 'Payne'], | ||
['Sandor', 'Clegane']); | ||
// when | ||
$mapped = $list->map(Functions::constant([]))->map(Functions::constant([])); | ||
// then | ||
$this->assertIteratesNames($mapped, [ | ||
"'Joffrey', 'Baratheon'", | ||
"'Cersei', 'Lannister'", | ||
"'Ilyn', 'Payne'", | ||
"'Sandor', 'Clegane'", | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldNameAssociative() | ||
{ | ||
// given | ||
$provider = DataProvider::zip( | ||
DataProvider::of([ | ||
'4' => ['Joffrey', 'Baratheon'], | ||
'5' => ['Cersei', 'Lannister']]), | ||
DataProvider::of([ | ||
['Ilyn', 'Payne'], | ||
['Sandor', 'Clegane']])); | ||
// when | ||
$mapped = $provider->map(Functions::toArray()); | ||
// then | ||
$this->assertIteratesNames($mapped, ['[4], #0', '[5], #1']); | ||
} | ||
} |