Skip to content

Commit

Permalink
Add infectionphp
Browse files Browse the repository at this point in the history
now this package using infectionphp to check the qualitify of the unittests
  • Loading branch information
Dropelikeit committed Oct 25, 2023
1 parent cc91fa7 commit 49376a3
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 24 deletions.
12 changes: 9 additions & 3 deletions composer.json
Expand Up @@ -46,14 +46,15 @@
"symfony/cache": "^6.3",
"vimeo/psalm": "^5.15",
"psalm/plugin-laravel": "^2.8",
"psalm/plugin-phpunit": "^0.18.4"
"psalm/plugin-phpunit": "^0.18.4",
"infection/infection": "^0.27.6"
},
"scripts": {
"lint": "parallel-lint --exclude .git --exclude vendor .",
"cs-check": "php-cs-fixer -v --dry-run --using-cache=no fix",
"cs-fix": "php-cs-fixer --using-cache=no fix",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover build/logs/clover.xml",
"test": "export XDEBUG_MODE=coverage && phpunit",
"test-coverage": "export XDEBUG_MODE=coverage && phpunit --coverage-clover build/logs/clover.xml --coverage-html build/logs/clover.html",
"analyze": "phpstan analyze --no-progress --memory-limit=-1 --xdebug",
"psalm": "psalm --no-cache -c psalm.xml",
"check": [
Expand All @@ -64,5 +65,10 @@
"@lint",
"@psalm"
]
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" cacheDirectory=".phpunit.cache" beStrictAboutCoverageMetadata="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" cacheDirectory=".phpunit.cache" beStrictAboutCoverageMetadata="true">
<coverage>
<report>
<clover outputFile="build/output/tests/coverage.xml"/>
Expand Down
48 changes: 32 additions & 16 deletions src/Config/Config.php
Expand Up @@ -3,6 +3,7 @@

namespace Dropelikeit\LaravelJmsSerializer\Config;

use Webmozart\Assert\Assert;
use function array_diff;
use function array_keys;
use Dropelikeit\LaravelJmsSerializer\Contracts\Config as ResponseBuilderConfig;
Expand All @@ -13,7 +14,6 @@
use function implode;
use function in_array;
use function sprintf;
use Webmozart\Assert\Assert;

/**
* @author Marcel Strahl <info@marcel-strahl.de>
Expand Down Expand Up @@ -45,7 +45,7 @@ private function __construct(
array $customHandlers,
) {
$cacheDir = sprintf('%s%s', $cacheDir, self::CACHE_DIR);
Assert::stringNotEmpty($cacheDir);
#Assert::stringNotEmpty($cacheDir);

$this->cacheDir = $cacheDir;
$this->customHandlers = $customHandlers;
Expand All @@ -59,32 +59,48 @@ private function __construct(
public static function fromConfig(array $config): self
{
$missing = array_diff([
'serialize_null',
'cache_dir',
'serialize_type',
'debug',
'add_default_handlers',
'custom_handlers',
self::KEY_SERIALIZE_NULL,
self::KEY_CACHE_DIR,
self::KEY_SERIALIZE_TYPE,
self::KEY_DEBUG,
self::KEY_ADD_DEFAULT_HANDLERS,
self::KEY_CUSTOM_HANDLERS,
], array_keys($config));

if (!empty($missing)) {
throw MissingRequiredItems::fromConfig(implode(',', $missing));
}

if (!in_array($config['serialize_type'], [
$serializeType = $config[self::KEY_SERIALIZE_TYPE];
if (!in_array($serializeType, [
self::SERIALIZE_TYPE_JSON,
self::SERIALIZE_TYPE_XML,
], true)) {
throw SerializeType::fromUnsupportedSerializeType($config['serialize_type']);
throw SerializeType::fromUnsupportedSerializeType($serializeType);
}

$serializeNull = $config[self::KEY_SERIALIZE_NULL];
Assert::boolean($serializeNull);

$debug = $config[self::KEY_DEBUG];
Assert::boolean($debug);

$addDefaultHandlers = $config[self::KEY_ADD_DEFAULT_HANDLERS];
Assert::boolean($addDefaultHandlers);

$cacheDir = $config[self::KEY_CACHE_DIR];
Assert::string($cacheDir);

$customHandlers = $config[self::KEY_CUSTOM_HANDLERS];
Assert::isArray($customHandlers);

return new self(
cacheDir: $config['cache_dir'],
customHandlers: (array) $config['custom_handlers'],
shouldSerializeNull: (bool) $config['serialize_null'],
serializeType: $config['serialize_type'],
debug: (bool) $config['debug'],
addDefaultHandlers: (bool) $config['add_default_handlers'],
shouldSerializeNull: $serializeNull,
serializeType: $serializeType,
debug: $debug,
addDefaultHandlers: $addDefaultHandlers,
cacheDir: $cacheDir,
customHandlers: $customHandlers,
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/Config.php
Expand Up @@ -12,6 +12,13 @@ interface Config
public const SERIALIZE_TYPE_XML = 'xml';
public const CACHE_DIR = '/serializer/';

public const KEY_SERIALIZE_NULL = 'serialize_null';
public const KEY_CACHE_DIR = 'cache_dir';
public const KEY_SERIALIZE_TYPE = 'serialize_type';
public const KEY_DEBUG = 'debug';
public const KEY_ADD_DEFAULT_HANDLERS = 'add_default_handlers';
public const KEY_CUSTOM_HANDLERS = 'custom_handlers';

public function getCacheDir(): string;

public function shouldSerializeNull(): bool;
Expand Down
7 changes: 6 additions & 1 deletion src/Serializer/Factory.php
Expand Up @@ -69,6 +69,11 @@ public function getSerializer(Config $config): SerializerInterface
});
}

return $builder->setCacheDir($config->getCacheDir())->setDebug($config->debug())->build();
$cacheDir = $config->getCacheDir();
if ($cacheDir !== '') {
$builder->setCacheDir($cacheDir);
}

return $builder->setDebug($config->debug())->build();
}
}
109 changes: 108 additions & 1 deletion tests/Config/ConfigTest.php
Expand Up @@ -7,6 +7,9 @@
use Dropelikeit\LaravelJmsSerializer\Contracts\CustomHandlerConfiguration;
use Dropelikeit\LaravelJmsSerializer\Exception\MissingRequiredItems;
use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -26,6 +29,7 @@ public function canCreateConfig(array $config, bool $throwMissingException, bool
{
if ($throwMissingException) {
$this->expectException(MissingRequiredItems::class);
$this->expectExceptionCode(400);
}

if ($throwWrongTypeException) {
Expand All @@ -38,7 +42,7 @@ public function canCreateConfig(array $config, bool $throwMissingException, bool
self::assertEquals(sprintf('%s%s', $config['cache_dir'], '/serializer/'), $configTest->getCacheDir());
self::assertEquals($config['serialize_type'], $configTest->getSerializeType());
self::assertEquals($config['debug'], $configTest->debug());
self::assertEquals(true, $configTest->shouldAddDefaultHeaders());
self::assertTrue($configTest->shouldAddDefaultHeaders());
self::assertCount(0, $configTest->getCustomHandlers());
}

Expand Down Expand Up @@ -83,6 +87,109 @@ public static function dataProviderCanCreateConfig(): array
false,
true,
],
'missing_required_serialize_null_key' => [
[
'cache_dir' => '/storage',
'serialize_type' => 'yaml',
'debug' => false,
'add_default_handlers' => true,
'custom_handlers' => [],
],
true,
false,
],
];
}

#[Test]
#[DataProvider('dataProviderCanNotCreateConfigBecauseInvalidArgumentExceptionThrows')]
public function canNotCreateConfigBecauseInvalidArgumentExceptionThrows(array $config, int $statusCode, string $expectedError): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode($statusCode);
$this->expectExceptionMessage($expectedError);

Config::fromConfig($config);
}

/**
* @return array<string, array<int, mixed>>
*/
public static function dataProviderCanNotCreateConfigBecauseInvalidArgumentExceptionThrows(): array
{
return [
'serialize_null_not_a_boolean' => [
[
'serialize_null' => 1,
'cache_dir' => '/storage',
'serialize_type' => 'json',
'debug' => false,
'add_default_handlers' => true,
'custom_handlers' => [],
],
0,
'Expected a boolean. Got: integer',
],
'debug_is_not_a_boolean' => [
[
'serialize_null' => false,
'cache_dir' => '/storage',
'debug' => 2,
'serialize_type' => 'json',
'add_default_handlers' => true,
'custom_handlers' => [],
],
0,
'Expected a boolean. Got: integer',
],
'add_default_handler_is_not_a_boolean' => [
[
'serialize_null' => false,
'cache_dir' => '/storage',
'serialize_type' => 'json',
'debug' => false,
'add_default_handlers' => 2,
'custom_handlers' => [],
],
0,
'Expected a boolean. Got: integer',
],
'serialize_type_is_not_json_or_xml' => [
[
'serialize_null' => false,
'cache_dir' => '/storage',
'serialize_type' => 'yaml',
'debug' => false,
'add_default_handlers' => true,
'custom_handlers' => [],
],
400,
'Unknown given type "yaml" allowed types are "json" and "xml"',
],
'cache_dir_is_not_a_string' => [
[
'serialize_null' => false,
'cache_dir' => 123,
'serialize_type' => 'xml',
'debug' => false,
'add_default_handlers' => true,
'custom_handlers' => [],
],
0,
'Expected a string. Got: integer',
],
'custom_handlers' => [
[
'serialize_null' => false,
'cache_dir' => '/storage',
'serialize_type' => 'xml',
'debug' => false,
'add_default_handlers' => true,
'custom_handlers' => '',
],
0,
'Expected an array. Got: string',
],
];
}
}
6 changes: 4 additions & 2 deletions tests/Serializer/FactoryTest.php
@@ -1,7 +1,9 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\Serializer;

use DateTime;
use Dropelikeit\LaravelJmsSerializer\Config\Config;
use Dropelikeit\LaravelJmsSerializer\Contracts\CustomHandlerConfiguration;
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
Expand All @@ -15,7 +17,7 @@
/**
* @author Marcel Strahl <info@marcel-strahl.de>
*/
class FactoryTest extends TestCase
final class FactoryTest extends TestCase
{
/**
* @test
Expand Down Expand Up @@ -98,7 +100,7 @@ public function canCreateSerializerWithCustomHandlerAsObject(): void
->expects(self::once())
->method('getCallable')
->willReturn(
static function (JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) {
static function (JsonSerializationVisitor $visitor, DateTime $date, array $type, Context $context) {
return 'hello world!';
}
);
Expand Down

0 comments on commit 49376a3

Please sign in to comment.