Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Remove MatcherFactory->adaptAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Apr 10, 2023
1 parent 9889567 commit ac10032
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 69 deletions.
43 changes: 1 addition & 42 deletions src/Matcher/MatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,47 +111,6 @@ public function adapt($value): Matcher
return new EqualToMatcher($value, true, $this->exporter);
}

/**
* Create new matchers for all supplied values.
*
* @param array<int|string,mixed> $values The values to create matchers for.
*
* @return array<int|string,Matcher> The newly created matchers.
*/
public function adaptAll(array $values): array
{
$matchers = [];

foreach ($values as $key => $value) {
if ($value instanceof Matcher) {
$matchers[$key] = $value;

continue;
}

if (is_object($value)) {
foreach ($this->driverIndex as $className => $driver) {
if (is_a($value, $className)) {
$matchers[$key] = $driver->wrapMatcher($value);

continue 2;
}
}
}

if ('*' === $value) {
$matchers[$key] = $this->wildcardAnyMatcher;
} elseif ('~' === $value) {
$matchers[$key] = $this->anyMatcher;
} else {
$matchers[$key] =
new EqualToMatcher($value, true, $this->exporter);
}
}

return $matchers;
}

/**
* Create a new matcher set for the supplied values.
*
Expand Down Expand Up @@ -256,7 +215,7 @@ public function adaptSet(array $parameterNames, array $values): MatcherSet

if (isset($declaredMatchers[$mappedKey])) {
throw new InvalidArgumentException(
"Named matcher $$key overwrites previous matcher."
"Named matcher $key overwrites previous matcher."
);
}

Expand Down
128 changes: 103 additions & 25 deletions test/suite/Matcher/MatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Eloquent\Phony\Test\TestMatcherB;
use Eloquent\Phony\Test\TestMatcherDriverA;
use Eloquent\Phony\Test\TestMatcherDriverB;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

#[AllowDynamicProperties]
Expand Down Expand Up @@ -88,7 +89,7 @@ public function testAdaptSpecialCases()
$this->assertSame($this->anyMatcher, $this->subject->adapt('~'));
}

public function testAdaptAll()
public function testAdaptSet()
{
$this->subject->addMatcherDriver($this->driverA);
$this->subject->addMatcherDriver($this->driverB);
Expand All @@ -104,24 +105,39 @@ public function testAdaptAll()
$valueC,
$valueD,
new TestMatcherA(),
'*',
'~',
'*',
];
$actual = $this->subject->adaptAll($values);
$expected = [
new EqualToMatcher('a', true, $this->exporter),
$valueB,
new EqualToMatcher($valueC, true, $this->exporter),
new EqualToMatcher($valueD, true, $this->exporter),
new EqualToMatcher('a', false, $this->exporter),
$this->wildcardMatcher,
$this->anyMatcher,
];
$actual = $this->subject->adaptSet(['a', 'b'], $values);
$expected = new MatcherSet(
parameterNames: ['a', 'b'],
keyMap: [
'a' => 0,
0 => 'a',
'b' => 1,
1 => 'b',
],
declaredCount: 2,
declaredMatchers: [
new EqualToMatcher('a', true, $this->exporter),
$valueB,
],
variadicMatchers: [
2 => new EqualToMatcher($valueC, true, $this->exporter),
3 => new EqualToMatcher($valueD, true, $this->exporter),
4 => new EqualToMatcher('a', false, $this->exporter),
5 => $this->anyMatcher,
],
wildcardMatcher: $this->wildcardMatcher,
wildcardInnerMatcher: $this->anyMatcher,
wildcardMinimum: 0,
wildcardMaximum: -1,
);

$this->assertEquals($expected, $actual);
}

public function testAdaptAllWithNamedMatchers()
public function testAdaptSetWithNamedMatchers()
{
$this->subject->addMatcherDriver($this->driverA);
$this->subject->addMatcherDriver($this->driverB);
Expand All @@ -132,28 +148,90 @@ public function testAdaptAllWithNamedMatchers()
$this->container->mockBuilderFactory->create()->full()
);
$values = [
'*',
'a' => 'a',
'b' => $valueB,
'c' => $valueC,
'd' => $valueD,
'e' => new TestMatcherA(),
'f' => '*',
'g' => '~',
];
$actual = $this->subject->adaptAll($values);
$expected = [
'a' => new EqualToMatcher('a', true, $this->exporter),
'b' => $valueB,
'c' => new EqualToMatcher($valueC, true, $this->exporter),
'd' => new EqualToMatcher($valueD, true, $this->exporter),
'e' => new EqualToMatcher('a', false, $this->exporter),
'f' => $this->wildcardMatcher,
'g' => $this->anyMatcher,
'f' => '~',
];
$actual = $this->subject->adaptSet(['a', 'b'], $values);
$expected = new MatcherSet(
parameterNames: ['a', 'b'],
keyMap: [
'a' => 0,
0 => 'a',
'b' => 1,
1 => 'b',
],
declaredCount: 2,
declaredMatchers: [
new EqualToMatcher('a', true, $this->exporter),
$valueB,
],
variadicMatchers: [
'c' => new EqualToMatcher($valueC, true, $this->exporter),
'd' => new EqualToMatcher($valueD, true, $this->exporter),
'e' => new EqualToMatcher('a', false, $this->exporter),
'f' => $this->anyMatcher,
],
wildcardMatcher: $this->wildcardMatcher,
wildcardInnerMatcher: $this->anyMatcher,
wildcardMinimum: 0,
wildcardMaximum: -1,
);

$this->assertEquals($expected, $actual);
}

public function adaptSetInvalidInputData(): array
{
return [
'positional after named' => [
'Cannot use a positional matcher after a named matcher.',
[],
['a' => 1, 2],
],
'positional after wildcard' => [
'Cannot use a positional matcher after a wildcard matcher.',
[],
['*', 2],
],
'wildcard after named' => [
'Cannot use a wildcard matcher after a named matcher.',
[],
['a' => 1, '*'],
],
'wildcard after wildcard' => [
'Cannot use a wildcard matcher after a wildcard matcher.',
[],
['*', '*'],
],
'named overwrites previous' => [
'Named matcher a overwrites previous matcher.',
['a'],
[1, 'a' => 2],
],
'named wildcard' => [
'Cannot use a named wildcard matcher.',
['a'],
['a' => '*'],
],
];
}

/**
* @dataProvider adaptSetInvalidInputData
*/
public function testAdaptSetWithInvalidInput(string $expected, array $parameterNames, array $values)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($expected);

$actual = $this->subject->adaptSet($parameterNames, $values);
}

public function testEqualTo()
{
$expected = new EqualToMatcher('x', false, $this->exporter);
Expand Down
4 changes: 2 additions & 2 deletions test/suite/Matcher/Verification/MatcherVerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public static function setUpBeforeClass(): void
public function invalidInputData(): array
{
return [
'positional after named argument' => [
'positional after named' => [
'Cannot use a positional argument after a named argument.',
[],
['a' => 1, 2],
],
'named argument overwrites previous' => [
'named overwrites previous' => [
'Named argument $a overwrites previous argument.',
['a'],
[1, 'a' => 2],
Expand Down

0 comments on commit ac10032

Please sign in to comment.