Skip to content

Commit a322d77

Browse files
committed
Added more unit tests, and fixed PropertyObfuscator's duplicate key check
1 parent f822dcd commit a322d77

File tree

5 files changed

+127
-17
lines changed

5 files changed

+127
-17
lines changed

src/Robtimus/Obfuscation/Obfuscate.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
final class Obfuscate
1414
{
15-
private static ?Obfuscator $_none = null;
16-
1715
private function __construct()
1816
{
1917
// private constructor to prevent initialization
@@ -55,22 +53,19 @@ public function obfuscateText(string $text): string
5553
*/
5654
public static function none(): Obfuscator
5755
{
58-
if (is_null(Obfuscate::$_none)) {
59-
Obfuscate::$_none = new class extends Obfuscator
56+
return new class extends Obfuscator
57+
{
58+
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
59+
public function __construct()
6060
{
61-
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
62-
public function __construct()
63-
{
64-
}
61+
}
6562

66-
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
67-
public function obfuscateText(string $text): string
68-
{
69-
return $text;
70-
}
71-
};
72-
}
73-
return Obfuscate::$_none;
63+
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
64+
public function obfuscateText(string $text): string
65+
{
66+
return $text;
67+
}
68+
};
7469
}
7570

7671
/**

src/Robtimus/Obfuscation/PropertyObfuscator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function withProperty(string $propertyName, Obfuscator $obfuscator, ?bool
103103
{
104104
$this->_addLastProperty();
105105

106-
$caseSensitive = $caseSensitive ?: $this->_caseSensitiveByDefault;
106+
$caseSensitive = is_null($caseSensitive) ? $this->_caseSensitiveByDefault : $caseSensitive;
107107
$this->_testProperty($propertyName, $caseSensitive);
108108

109109
$this->_propertyName = $propertyName;

tests/Robtimus/Obfuscation/ObfuscatePortionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ public static function obfuscationParameters(): array
114114
];
115115
}
116116

117+
public function testWithDefaults(): void
118+
{
119+
$obfuscator = Obfuscate::portion()
120+
->keepAtStart(1)
121+
->keepAtEnd(1)
122+
->atLeastFromStart(1)
123+
->atLeastFromEnd(1)
124+
->withFixedTotalLength(10)
125+
->withMask('x')
126+
->withDefaults()
127+
->build();
128+
129+
$obfuscated = $obfuscator->obfuscateText('hello world');
130+
131+
$this->assertEquals('***********', $obfuscated);
132+
}
133+
117134
public function testNegativeKeepAtStart(): void
118135
{
119136
$builder = Obfuscate::portion();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Robtimus\Obfuscation;
3+
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
use PHPUnit\Framework\TestCase;
6+
use ValueError;
7+
8+
class ObfuscatorTest extends TestCase
9+
{
10+
#[DataProvider('nonPositiveMinPrefixLengthParameters')]
11+
public function testNonPositiveMinPrefixLength(int $minPrefixLength): void
12+
{
13+
$this->expectException(ValueError::class);
14+
$this->expectExceptionMessage("$minPrefixLength < 1");
15+
16+
new class($minPrefixLength) extends Obfuscator
17+
{
18+
public function __construct(int $minPrefixLength)
19+
{
20+
parent::__construct($minPrefixLength);
21+
}
22+
23+
public function obfuscateText(string $text): string
24+
{
25+
return $text;
26+
}
27+
};
28+
}
29+
30+
/**
31+
* @return array<array<int>>
32+
*/
33+
public static function nonPositiveMinPrefixLengthParameters(): array
34+
{
35+
return [
36+
[-1],
37+
[0],
38+
];
39+
}
40+
}

tests/Robtimus/Obfuscation/PropertyObfuscatorTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPUnit\Framework\Attributes\DataProvider;
55
use PHPUnit\Framework\TestCase;
6+
use ValueError;
67

78
class PropertyObfuscatorTest extends TestCase
89
{
@@ -103,6 +104,33 @@ class PropertyObfuscatorTest extends TestCase
103104
}
104105
EOD;
105106

107+
public function testDuplicateCaseSensitivePropertyNames(): void
108+
{
109+
$obfuscator = Obfuscate::fixedLength(3);
110+
$builder = PropertyObfuscator::builder()
111+
->withProperty('test', $obfuscator)
112+
->withProperty('test', $obfuscator, false)
113+
->withProperty('TEST', $obfuscator);
114+
115+
$this->expectException(ValueError::class);
116+
$this->expectExceptionMessage('Duplicate key: test (case sensitive)');
117+
118+
$builder->withProperty('test', $obfuscator, true);
119+
}
120+
121+
public function testDuplicateCaseInsensitivePropertyNames(): void
122+
{
123+
$obfuscator = Obfuscate::fixedLength(3);
124+
$builder = PropertyObfuscator::builder()
125+
->withProperty('test', $obfuscator)
126+
->withProperty('test', $obfuscator, false);
127+
128+
$this->expectException(ValueError::class);
129+
$this->expectExceptionMessage('Duplicate key: TEST (case insensitive)');
130+
131+
$builder->withProperty('TEST', $obfuscator, false);
132+
}
133+
106134
#[DataProvider('obfuscatePropertyGloballyCaseSensitiveParameters')]
107135
public function testObfuscatePropertyGloballyCaseSensitive(string $propertyName, string $value, string $expected): void
108136
{
@@ -1984,4 +2012,34 @@ public function testObfuscateArrayOverridableInheritedObfuscators(): void
19842012
EOD;
19852013
$this->assertEquals($expectedJson, $obfuscatedJson);
19862014
}
2015+
2016+
#[DataProvider('obfuscateScalarsParameters')]
2017+
public function testObfuscateScalars(mixed $value): void
2018+
{
2019+
$obfuscator = Obfuscate::fixedLength(3);
2020+
$propertyObfuscator = PropertyObfuscator::builder()
2021+
->caseSensitiveByDefault()
2022+
->withProperty('string', $obfuscator)
2023+
->withProperty('notObfuscated', Obfuscate::none())
2024+
->forObjects(PropertyObfuscationMode::SKIP)
2025+
->forArrays(PropertyObfuscationMode::SKIP)
2026+
->build();
2027+
2028+
$obfuscated = $propertyObfuscator->obfuscateProperties($value);
2029+
2030+
$this->assertEquals($value, $obfuscated);
2031+
}
2032+
2033+
/**
2034+
* @return array<array<mixed>>
2035+
*/
2036+
public static function obfuscateScalarsParameters(): array
2037+
{
2038+
return [
2039+
['foo'],
2040+
[1],
2041+
[true],
2042+
[false],
2043+
];
2044+
}
19872045
}

0 commit comments

Comments
 (0)