-
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
22 changed files
with
275 additions
and
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/vendor/ | ||
composer.lock | ||
.phpunit.cache | ||
.phpunit.result.cache | ||
.idea | ||
infection.log |
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 |
---|---|---|
@@ -1,12 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="Jasny"> | ||
<description>The Jasny coding standard.</description> | ||
|
||
<!-- Include the whole PSR-1 standard --> | ||
<rule ref="PSR1"/> | ||
<!-- Include the whole PSR-2 standard --> | ||
<rule ref="PSR2"/> | ||
|
||
<!-- TODO: Add own rules --> | ||
<ruleset name="Coding standard"> | ||
<description>PSR-12 coding standard</description> | ||
<rule ref="PSR12"/> | ||
</ruleset> | ||
|
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 |
---|---|---|
|
@@ -2,5 +2,4 @@ parameters: | |
level: 3 | ||
paths: | ||
- src | ||
includes: | ||
- vendor/phpstan/phpstan-strict-rules/rules.neon | ||
|
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 |
---|---|---|
@@ -1,27 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
<testsuites> | ||
<testsuite name="unit"> | ||
<directory>tests/unit</directory> | ||
</testsuite> | ||
<testsuite name="functional"> | ||
<directory>tests/functional</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-text" target="php://stdout"/> | ||
</logging> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd" cacheDirectory=".phpunit.cache"> | ||
<testsuites> | ||
<testsuite name="unit"> | ||
<directory>tests/unit</directory> | ||
</testsuite> | ||
<testsuite name="functional"> | ||
<directory>tests/functional</directory> | ||
</testsuite> | ||
</testsuites> | ||
<logging/> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</source> | ||
</phpunit> | ||
|
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
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jasny\SwitchRoute\Tests\Utils; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\MockObject\MockBuilder; | ||
use PHPUnit\Framework\MockObject\Rule\InvocationOrder; | ||
|
||
/** | ||
* Trait to create a mock for a callback. | ||
*/ | ||
trait CallbackMockTrait | ||
{ | ||
/** | ||
* Returns a builder object to create mock objects using a fluent interface. | ||
* | ||
* @param string $className | ||
* @return MockBuilder | ||
*/ | ||
abstract public function getMockBuilder(string $className): MockBuilder; | ||
|
||
/** | ||
* Create mock for next callback. | ||
* | ||
* <code> | ||
* $callback = $this->createCallbackMock($this->once(), ['abc'], 10); | ||
* </code> | ||
* | ||
* OR | ||
* | ||
* <code> | ||
* $callback = $this->createCallbackMock( | ||
* $this->once(), | ||
* function(InvocationMocker $invoke) { | ||
* $invoke->with('abc')->willReturn(10); | ||
* } | ||
* ); | ||
* </code> | ||
* | ||
* @param InvocationOrder $matcher | ||
* @param \Closure|array|null $assert | ||
* @param mixed $return | ||
* @return MockObject|callable | ||
*/ | ||
protected function createCallbackMock($matcher, $assert = null, $return = null): MockObject | ||
{ | ||
if (isset($assert) && !is_array($assert) && !$assert instanceof \Closure) { | ||
$type = (is_object($assert) ? get_class($assert) . ' ' : '') . gettype($assert); | ||
throw new \InvalidArgumentException("Expected an array or Closure, got a $type"); | ||
} | ||
|
||
$callback = $this->getMockBuilder(DummyCallback::class) | ||
->setMockClassName('CallbackMock_' . \substr(\md5((string) \mt_rand()), 0, 8)) | ||
->getMock(); | ||
$invoke = $callback->expects($matcher)->method('__invoke'); | ||
|
||
if ($assert instanceof \Closure) { | ||
$assert($invoke); | ||
} elseif (is_array($assert)) { | ||
$invoke->with(...$assert)->willReturn($return); | ||
} | ||
|
||
return $callback; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
* @codeCoverageIgnore | ||
*/ | ||
class DummyCallback | ||
{ | ||
/** | ||
* Invoke object. | ||
* | ||
* @return mixed | ||
*/ | ||
public function __invoke() | ||
{ | ||
return null; | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
/** | ||
Usage: ->with(...Consecutive::create(...$withCodes)) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jasny\SwitchRoute\Tests\Utils; | ||
|
||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
use PHPUnit\Framework\Constraint\IsEqual; | ||
use RuntimeException; | ||
|
||
class Consecutive | ||
{ | ||
public static function create(...$parameterGroups): array | ||
{ | ||
$result = []; | ||
$parametersCount = null; | ||
$groups = []; | ||
$values = []; | ||
|
||
foreach ($parameterGroups as $index => $parameters) { | ||
// initial | ||
$parametersCount ??= count($parameters); | ||
|
||
// compare | ||
if ($parametersCount !== count($parameters)) { | ||
throw new RuntimeException('Parameters count max much in all groups'); | ||
} | ||
|
||
// prepare parameters | ||
foreach ($parameters as $parameter) { | ||
if (!$parameter instanceof Constraint) { | ||
$parameter = new IsEqual($parameter); | ||
} | ||
|
||
$groups[$index][] = $parameter; | ||
} | ||
} | ||
|
||
// collect values | ||
foreach ($groups as $parameters) { | ||
foreach ($parameters as $index => $parameter) { | ||
$values[$index][] = $parameter; | ||
} | ||
} | ||
|
||
// build callback | ||
for ($index = 0; $index < $parametersCount; ++$index) { | ||
$result[$index] = Assert::callback(static function ($value) use ($values, $index) { | ||
static $map = null; | ||
$map ??= $values[$index]; | ||
|
||
$expectedArg = array_shift($map); | ||
if ($expectedArg === null) { | ||
throw new RuntimeException('No more expected calls'); | ||
} | ||
$expectedArg->evaluate($value); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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
Oops, something went wrong.