Skip to content

Commit 9a26209

Browse files
committed
Added new tests
1 parent 87b3df8 commit 9a26209

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/Utility/Strings.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ final class Strings
1414
*/
1515
public static function splitAtIndex(string $string, int $index): array
1616
{
17+
if ($index < 0) {
18+
throw new \Exception('Can not spilt a string at a negative index.');
19+
}
20+
1721
return [
1822
substr($string, 0, $index),
19-
$part2 = substr($string, $index),
23+
$index < strlen($string)
24+
? substr($string, $index)
25+
: ''
26+
,
2027
];
2128
}
2229

tests/Utility/SignaturesTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace le0daniel\Tests\Laravel\ImageEngine\Utility;
4+
5+
use le0daniel\Laravel\ImageEngine\Utility\SignatureException;
6+
use le0daniel\Laravel\ImageEngine\Utility\Signatures;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class SignaturesTest extends TestCase
10+
{
11+
private const SECRET = 'asdfasdf!_';
12+
13+
public function testSign()
14+
{
15+
$this->assertSame(
16+
'the-string::PT7sqFr2CdpljWjc5HCbLevrooH_xT1D-WDnLBEBqzI',
17+
Signatures::sign(self::SECRET, 'the-string')
18+
);
19+
}
20+
21+
public function testVerifyAndReturnPayloadString()
22+
{
23+
$this->assertEquals(
24+
'the-string',
25+
Signatures::verifyAndReturnPayloadString(
26+
self::SECRET,
27+
'the-string::PT7sqFr2CdpljWjc5HCbLevrooH_xT1D-WDnLBEBqzI'
28+
)
29+
);
30+
}
31+
32+
public function testVerifyAndReturnPayloadStringWithInvalidStructure()
33+
{
34+
$this->expectException(SignatureException::class);
35+
$this->expectExceptionMessage('The signed string is of invalid structure');
36+
Signatures::verifyAndReturnPayloadString(self::SECRET, 'asdadasd');
37+
38+
$this->fail('Should not reach this.');
39+
}
40+
41+
public function testVerifyAndReturnPayloadStringWithTamperedString()
42+
{
43+
$this->expectException(SignatureException::class);
44+
$this->expectExceptionMessage('Signature mismatches.');
45+
Signatures::verifyAndReturnPayloadString(self::SECRET, 'asdadasd::ajbflenfiwenfiwef');
46+
47+
$this->fail('Should not reach this.');
48+
}
49+
}

tests/Utility/StringsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace le0daniel\Tests\Laravel\ImageEngine\Utility;
4+
5+
use le0daniel\Laravel\ImageEngine\Utility\Strings;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class StringsTest extends TestCase
9+
{
10+
11+
public function testSplitAtIndex()
12+
{
13+
$this->assertEquals(['test', '-string'], Strings::splitAtIndex('test-string', 4));
14+
$this->assertEquals(['', 'test-string'], Strings::splitAtIndex('test-string', 0));
15+
$this->assertEquals(['test-string', ''], Strings::splitAtIndex('test-string', 100));
16+
17+
$this->assertEquals(['test-strin', 'g'], Strings::splitAtIndex('test-string', 10));
18+
$this->assertEquals(['test-string', ''], Strings::splitAtIndex('test-string', 11));
19+
$this->assertEquals(['test-string', ''], Strings::splitAtIndex('test-string', 12));
20+
21+
$this->expectException(\Exception::class);
22+
$this->expectExceptionMessage('Can not spilt a string at a negative index.');
23+
Strings::splitAtIndex('test-string', -1);
24+
25+
$this->fail('Should not reach this.');
26+
}
27+
}

0 commit comments

Comments
 (0)