Skip to content

Commit

Permalink
Merge pull request #5 from rich-id/v0.1.3
Browse files Browse the repository at this point in the history
v0.1.3
  • Loading branch information
mdevlamynck committed Dec 10, 2021
2 parents 56be0ca + 919ddf5 commit d80a2b3
Show file tree
Hide file tree
Showing 11 changed files with 3,484 additions and 1,478 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/tests.yml
@@ -1,5 +1,5 @@
name: Tests
on: [push, pull_request]
on: [push]

jobs:
build-and-test:
Expand All @@ -24,11 +24,10 @@ jobs:
- name: Install the dependencies 🔧
run: |
docker-compose exec -T application composer install --prefer-dist --no-interaction --no-progress
docker-compose exec -T application php -d zend_extension=xdebug.so ./bin/phpunit --configuration phpunit.xml.dist --dump-xdebug-filter phpunit-filter.php
- name: Execute the tests 🔧
run: |
docker-compose exec -T application php -d zend_extension=xdebug.so ./bin/phpunit --prepend phpunit-filter.php --configuration phpunit.xml.dist --colors=never --coverage-clover build/logs/clover.xml
docker-compose exec -T application php -d extension=pcov.so ./bin/phpunit --configuration phpunit.xml.dist --colors=never --coverage-clover build/logs/clover.xml
- name: Upload results to Coveralls 🚀
env:
Expand Down
11 changes: 2 additions & 9 deletions Helper/GlobalNamespaceHelper.php
Expand Up @@ -13,16 +13,9 @@ function debug($object, bool $verbose = false): void
ob_start();
\var_dump($object);
$result = ob_get_clean();

if (!is_string($result)) {
return;
}

$result = is_string($result) ? $result : '';
$result = preg_replace('/^(.*)GlobalNamespaceHelper\.php(.*)(\s*)/', '', $result);

if (!is_string($result)) {
return;
}
$result = is_string($result) ? $result : '';

echo trim($result);
}
Expand Down
28 changes: 26 additions & 2 deletions TestTrait/Assertion/Parameter.php
Expand Up @@ -27,10 +27,16 @@ class Parameter
public $regex;

/**
* @var string
* @var string[]
*/
public $choice;

/** @var array<string, mixed> */
public $arraySubMatch;

/** @var bool */
public $isNullable = false;

/**
* @return static
*/
Expand Down Expand Up @@ -85,7 +91,7 @@ public static function regex(string $regex): self
}

/**
* @param array $choice
* @param string[] $choice
*
* @return self
*/
Expand All @@ -97,6 +103,16 @@ public static function choice(array $choice): self
return $parameter;
}

/** @var array<string, mixed> $subMatch */
public static function arraySubMatch(array $subMatch): self
{
$parameter = new static();
$parameter->type = 'array';
$parameter->arraySubMatch = $subMatch;

return $parameter;
}

/**
* @param string $class
*
Expand All @@ -110,6 +126,14 @@ public static function instanceOf(string $class): self
return $parameter;
}


public function isNullable(): self
{
$this->isNullable = true;

return $this;
}

/**
* @param string $type
*
Expand Down
37 changes: 27 additions & 10 deletions TestTrait/MatchAssertionTrait.php
Expand Up @@ -40,7 +40,7 @@ protected static function assertMatch(array $expected, $tested): void
$testedValue = static::getMatchValue($tested, $expectedKey);

if ($expectedMatch instanceof Parameter) {
static::assertMatchParameter($expectedMatch, $testedValue);
static::assertMatchParameter($expectedMatch, $testedValue, $expectedKey);
} else if (is_array($expectedMatch) || is_object($expectedMatch)) {
static::assertMatch($expectedMatch, $testedValue);
} else if ($expectedMatch !== null) {
Expand Down Expand Up @@ -68,43 +68,60 @@ private static function getMatchValue($object, $expectedKey)
*
* @return void
*/
private static function assertMatchParameter(Parameter $parameter, $testedValue): void
private static function assertMatchParameter(Parameter $parameter, $testedValue, $testedKey): void
{
$errorMessage = sprintf('The key "%s" is invalid.', $testedKey);

if ($testedValue === null && $parameter->isNullable) {
self::assertNull($testedValue);

return;
}

// Type test
switch ($parameter->type) {
case 'string':
self::assertIsString($testedValue);
self::assertIsString($testedValue, $errorMessage);
break;

case 'integer':
self::assertIsInt($testedValue);
self::assertIsInt($testedValue, $errorMessage);
break;

case 'float':
self::assertIsFloat($testedValue);
self::assertIsFloat($testedValue, $errorMessage);
break;

case 'array':
self::assertIsArray($testedValue);
self::assertIsArray($testedValue, $errorMessage);
break;

case 'boolean':
self::assertIsBool($testedValue);
self::assertIsBool($testedValue, $errorMessage);
break;
}

// Array sub match test
if ($parameter->arraySubMatch !== null) {
foreach ($testedValue as $subValue) {
self::assertMatch($parameter->arraySubMatch, $subValue);
}
}

// Regex test
if ($parameter->regex !== null) {
self::assertRegExp($parameter->regex, $testedValue);
method_exists(static::class, 'assertMatchesRegularExpression')
? self::assertMatchesRegularExpression($parameter->regex, $testedValue, $errorMessage)
: self::assertRegExp($parameter->regex, $testedValue, $errorMessage);
}

// Choice test
if ($parameter->choice !== null) {
self::assertContainsEquals($testedValue, $parameter->choice);
self::assertContainsEquals($testedValue, $parameter->choice, $errorMessage);
}

if ($parameter->class !== null) {
self::assertInstanceOf($parameter->class, $testedValue);
self::assertInstanceOf($parameter->class, $testedValue, $errorMessage);
}
}

Expand Down
1 change: 1 addition & 0 deletions Tests/CacheTrait/CacheGetterTraitTest.php
Expand Up @@ -14,6 +14,7 @@
* @copyright 2014 - 2020 RichCongress (https://www.richcongress.com)
*
* @covers \RichCongress\TestTools\CacheTrait\CachedGetterTrait
*
* @method DummyEvent getEvent()
* @method bool getLoop()
* @method static DummyEvent getStaticEvent()
Expand Down
13 changes: 12 additions & 1 deletion Tests/TestTrait/MatchAssertionTraitTest.php
Expand Up @@ -2,6 +2,7 @@

namespace RichCongress\TestTools\Tests\TestTrait;

use PhpParser\Node\Param;
use RichCongress\TestTools\Accessor\ForcePropertyAccessor;
use RichCongress\TestTools\TestCase\TestCase;
use RichCongress\TestTools\Tests\Resources\Entity\DummyEntity;
Expand All @@ -28,11 +29,17 @@ public function testAssertMatch(): void
'floatValue' => 3.1,
'arrayValue' => ['yes'],
'isBoolean' => true,
'isNullable' => null,
'siret' => '012345678910',
'choiceValue' => 'Certain Type',
'subArray' => [
'instanceOf' => new DummyEntity(),
]
],
'arraySubMatch' => [
['test' => true],
['test' => false],
['test' => null],
],
];

$expected = [
Expand All @@ -42,11 +49,15 @@ public function testAssertMatch(): void
'floatValue' => Parameter::float(),
'arrayValue' => Parameter::array(),
'isBoolean' => Parameter::boolean(),
'isNullable' => Parameter::string()->isNullable(),
'siret' => Parameter::regex('/\d{12}/'),
'choiceValue' => Parameter::choice(['Certain Type', 'Other Type']),
'subArray' => [
'instanceOf' => Parameter::instanceOf(DummyEntity::class),
],
'arraySubMatch' => Parameter::arraySubMatch([
'test' => Parameter::boolean()->isNullable(),
])
];

self::assertMatch($expected, $tested);
Expand Down

0 comments on commit d80a2b3

Please sign in to comment.