add tests

This commit is contained in:
Daniel Seifert 2024-12-24 00:07:06 +01:00
parent b50624a0be
commit d2ce82454c
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
5 changed files with 267 additions and 0 deletions

View File

@ -25,6 +25,7 @@
},
"require-dev": {
"php": "^8.2",
"phpunit/phpunit": "^10.5",
"friendsofphp/php-cs-fixer": "^3.65",
"phpstan/phpstan": "^2.0"
},
@ -33,7 +34,13 @@
"D3\\SensitiveMessageFormatter\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"D3\\SensitiveMessageFormatter\\tests\\": "tests/"
}
},
"scripts": {
"test": "./vendor/bin/phpunit",
"check-style": "./vendor/bin/php-cs-fixer fix --verbose --dry-run",
"fix-style": "./vendor/bin/php-cs-fixer fix --verbose",
"check-code": "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress --ansi"

20
phpunit.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuite name="sensitiveMessageFormatter">
<directory>./tests</directory>
</testsuite>
<logging/>
</phpunit>

75
tests/ApiTestCase.php Normal file
View File

@ -0,0 +1,75 @@
<?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Max Buhe, Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\SensitiveMessageFormatter\tests;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionException;
abstract class ApiTestCase extends TestCase
{
/**
* Calls a private or protected object method.
*
* @param object $object
* @param string $methodName
* @param array $arguments
*
* @return mixed
* @throws ReflectionException
*/
public function callMethod(object $object, string $methodName, array $arguments = []): mixed
{
$class = new ReflectionClass($object);
$method = $class->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $arguments);
}
/**
* Sets a private or protected property in defined class instance
*
* @param object $object
* @param string $valueName
* @param $value
* @throws ReflectionException
*/
public function setValue(object $object, string $valueName, $value): void
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($valueName);
$property->setAccessible(true);
$property->setValue($object, $value);
}
/**
* get a private or protected property from defined class instance
*
* @param object $object
* @param string $valueName
* @return mixed
* @throws ReflectionException
*/
public function getValue(object $object, string $valueName): mixed
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($valueName);
$property->setAccessible(true);
return $property->getValue($object);
}
}

11
tests/README.md Normal file
View File

@ -0,0 +1,11 @@
# Installation
```
composer create-project -s dev --prefer-source [--repository '{"type": "vcs", "url": "repository url"}'] d3/sensitive-message-formatter .
```
# Run tests
```
./vendor/bin/phpunit [--no-coverage] [--coverage-html=cov]
```

View File

@ -0,0 +1,154 @@
<?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Max Buhe, Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\SensitiveMessageFormatter\tests;
use D3\SensitiveMessageFormatter\sensitiveMessageFormatter;
use Generator;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use ReflectionException;
/**
* @covers \D3\SensitiveMessageFormatter\sensitiveMessageFormatter
*/
class sensitiveMessageFormatterTest extends ApiTestCase
{
protected sensitiveMessageFormatter $sut;
public function setUp(): void
{
parent::setUp();
$this->sut = new sensitiveMessageFormatter(
'{method} {uri} HTTP/{version} {req_headers} {req_body} -- RESPONSE: {code} - {res_headers} {res_body}'
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\SensitiveMessageFormatter\sensitiveMessageFormatter::__construct
*/
public function testConstruct(): void
{
$sutMock = $this->getMockBuilder(sensitiveMessageFormatter::class)
->disableOriginalConstructor()
->onlyMethods(['createReplacements'])
->getMock();
$sutMock->expects($this->once())->method('createReplacements');
$sutMock->__construct();
$this->assertSame(
'*',
$this->getValue(
$this->sut,
'replaceChar'
)
);
}
/**
* @test
* @throws ReflectionException
* @dataProvider createReplacementsDataProvider
* @covers \D3\SensitiveMessageFormatter\sensitiveMessageFormatter::createReplacements
*/
public function testCreateReplacements(array $input, array $expected, ?string $replacement = null): void
{
if ($replacement) {
$this->setValue(
$this->sut,
'replaceChar',
$replacement
);
}
$this->callMethod(
$this->sut,
'createReplacements',
[$input]
);
$this->assertSame(
$expected,
$this->getValue(
$this->sut,
'anonymizations'
)
);
}
public static function createReplacementsDataProvider(): Generator
{
yield 'simple' => [['abc'], ['abc' => '***']];
yield 'multiple' => [['def', 'def'], ['def' => '***']];
yield 'urlencoded' => [['1&c'], ['1&c' => '***', '1%26c' => '***' ]];
yield 'different replace char' => [['abcd'], ['abcd' => '####'], '#'];
}
/**
* @test
* @throws ReflectionException
* @dataProvider formatDataProvider
* @covers \D3\SensitiveMessageFormatter\sensitiveMessageFormatter::format
*/
public function testFormat(array $replacements, $expected): void
{
$this->callMethod(
$this->sut,
'createReplacements',
[$replacements]
);
$request = new Request(
'POST',
'google.com',
['header1' => 'value1', 'header2' => 'val%26ue2'],
'Body value1 + value2'
);
$response = new Response(
200,
['header1' => 'value1', 'header2' => 'value2'],
'Body value1 + value2'
);
$this->assertEquals(
$expected,
preg_replace(
'@(\r\n|\r|\n)@',
'==',
$this->callMethod(
$this->sut,
'format',
[$request, $response]
)
)
);
}
public static function formatDataProvider(): Generator
{
yield [
['value1', 'val&ue2'],
'POST google.com HTTP/1.1 POST google.com HTTP/1.1=='.
'header1: ******==header2: ******* Body ****** + value2 -- RESPONSE: 200 - HTTP/1.1 200 OK=='.
'header1: ******==header2: value2 Body ****** + value2',
];
}
}