Skip to content

Commit

Permalink
Added unique key formatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EXayer committed Mar 1, 2024
1 parent d496ec7 commit 61f7598
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# VDF Converter

![Tests](https://github.com/exayer/vdf-converter/workflows/Tests/badge.svg)
[![Total Downloads](https://img.shields.io/packagist/dt/EXayer/vdf-converter)](https://packagist.org/packages/exayer/vdf-converter)
![Latest Stable Version](https://img.shields.io/packagist/v/exayer/vdf-converter)

A memory efficient parser for the Valve Data Format (*.vdf) written in PHP.
Expand Down Expand Up @@ -144,7 +143,7 @@ After creating your formatter, you can specify its class name in the `uniqueKeyF
You can also specify a signer for a specific webhook call:

```php
$config = (new VdfConverterConfig())
$config = VdfConverterConfig::create()
->uniqueKeyFormatter(YourCustomFormatter::class);

$iterator = VdfConverter::fromString($vdf, $config);
Expand Down
41 changes: 21 additions & 20 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EXayer\VdfConverter\Exception\CouldNotParseException;
use EXayer\VdfConverter\Lexer;
use EXayer\VdfConverter\Parser;
use EXayer\VdfConverter\VdfConverterConfig;
use PHPUnit\Framework\TestCase;

class ParserTest extends TestCase
Expand All @@ -16,7 +17,7 @@ class ParserTest extends TestCase
*/
public function testSyntax($vdf, $expectedResult)
{
$parser = new Parser(new Lexer(new \ArrayIterator([$vdf])));
$parser = new Parser(new Lexer(new \ArrayIterator([$vdf])), new VdfConverterConfig());
$result = iterator_to_array($parser);

$this->assertEquals($expectedResult, $result);
Expand Down Expand Up @@ -87,13 +88,13 @@ public function syntaxDataProvider()
[
'a' => [
'b' => 1,
'x' => ['x' => 1, 'x__2' => 2],
'x' => ['x' => 1, 'x__[2]' => 2],
'e' => [
'f' => ['x' => 1, '' => 1, '__2' => 2, 'x__2' => 2],
'f__2' => [],
'f' => ['x' => 1, '' => 1, '__[2]' => 2, 'x__[2]' => 2],
'f__[2]' => [],
],
'x__2' => [],
'e__2' => [],
'x__[2]' => [],
'e__[2]' => [],
],
],
],
Expand All @@ -103,28 +104,28 @@ public function syntaxDataProvider()

[
'a' => 1,
'b' => ['c' => 1, 'c__2' => 2],
'a__2' => [
'b' => ['c' => 1, 'c__2' => 2],
'b__2' => [],
'b' => ['c' => 1, 'c__[2]' => 2],
'a__[2]' => [
'b' => ['c' => 1, 'c__[2]' => 2],
'b__[2]' => [],
],
'a__3' => [],
'a__[3]' => [],
'c' => [],
'c__2' => [],
'c__[2]' => [],
],
],
// all duplicates
[
'{"x" "1" "x" {"x" "1" "x" "2"} "x" {"x" {"x" "1" "x" "2"} "x" {}} "x" {} "x" {}}',
[
'x' => 1,
'x__2' => ['x' => 1, 'x__2' => 2],
'x__3' => [
'x' => ['x' => 1, 'x__2' => 2],
'x__2' => [],
'x__[2]' => ['x' => 1, 'x__[2]' => 2],
'x__[3]' => [
'x' => ['x' => 1, 'x__[2]' => 2],
'x__[2]' => [],
],
'x__4' => [],
'x__5' => [],
'x__[4]' => [],
'x__[5]' => [],
],
],
];
Expand All @@ -138,7 +139,7 @@ public function testSyntaxError(string $brokenVdf)
{
$this->expectException(CouldNotParseException::class);

iterator_to_array(new Parser(new Lexer(new \ArrayIterator([$brokenVdf]))));
iterator_to_array(new Parser(new Lexer(new \ArrayIterator([$brokenVdf])), new VdfConverterConfig()));
}

public function syntaxErrorDataProvider()
Expand All @@ -162,7 +163,7 @@ public function testUnexpectedEndError(string $vdf)
{
$this->expectExceptionMessage(CouldNotParseException::unexpectedEnding()->getMessage());

iterator_to_array(new Parser(new Lexer(new \ArrayIterator([$vdf]))));
iterator_to_array(new Parser(new Lexer(new \ArrayIterator([$vdf])), new VdfConverterConfig()));
}

public function unexpectedEndExceptionDataProvider()
Expand Down
17 changes: 17 additions & 0 deletions tests/UniqueKey/DefaultFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EXayer\VdfConverter\Tests\UniqueKey;

use EXayer\VdfConverter\UniqueKey\DefaultFormatter;
use PHPUnit\Framework\TestCase;

class DefaultFormatterTest extends TestCase
{
public function testBuildName()
{
$formatter = new DefaultFormatter();
$formattedKey = $formatter->buildKeyName('sample_key', 9);

$this->assertEquals($formattedKey, 'sample_key__[9]');
}
}
11 changes: 6 additions & 5 deletions tests/UniqueKeyTest.php → tests/UniqueKey/UniqueKeyTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace EXayer\VdfConverter\Tests;
namespace EXayer\VdfConverter\Tests\UniqueKey;

use EXayer\VdfConverter\UniqueKeyHandler;
use EXayer\VdfConverter\UniqueKey\DefaultFormatter;
use EXayer\VdfConverter\UniqueKey\UniqueKeyHandler;
use PHPUnit\Framework\TestCase;

class UniqueKeyTest extends TestCase
Expand All @@ -15,7 +16,7 @@ class UniqueKeyTest extends TestCase
*/
public function testStorage(array $initStorage, array $levelsToClear, array $expected)
{
$uniqueKey = new UniqueKeyHandler();
$uniqueKey = new UniqueKeyHandler(new DefaultFormatter());

foreach ($initStorage as $pairs) {
foreach ($pairs as $level => $key) {
Expand Down Expand Up @@ -44,7 +45,7 @@ public function storageDataProvider()
[
[[1 => 'key'], [1 => 'key']],
[],
[1, 'key', 'key__3']
[1, 'key', 'key__[3]']
],
[
[[1 => 'key'], [1 => 'key'], [2 => 'key'], [3 => 'key']],
Expand All @@ -54,7 +55,7 @@ public function storageDataProvider()
[
[[1 => 'key'], [2 => 'key'], [3 => 'key']],
[2, 3],
[1, 'key', 'key__2']
[1, 'key', 'key__[2]']
],
];
}
Expand Down
16 changes: 16 additions & 0 deletions tests/VdfConverterConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use EXayer\VdfConverter\UniqueKey\DefaultFormatter;
use EXayer\VdfConverter\VdfConverterConfig;
use PHPUnit\Framework\TestCase;

class VdfConverterConfigTest extends TestCase
{
public function testUniqueKeyFormatterCreation()
{
$config = VdfConverterConfig::create()
->uniqueKeyFormatter(DefaultFormatter::class);

$this->assertEquals(new DefaultFormatter(), $config->getUniqueKeyFormatter());
}
}

0 comments on commit 61f7598

Please sign in to comment.