* @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, ?int $fixedReplacementLenth = 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' => '####'], '#']; yield 'fixed replacement lenght' => [['abcd'], ['abcd' => '*******'], '*', 7]; } /** * @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', ]; } }