-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoderTest.php
More file actions
61 lines (51 loc) · 2.2 KB
/
Copy pathEncoderTest.php
File metadata and controls
61 lines (51 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Podoko\Bencode\Tests;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Podoko\Bencode\Encoder;
use Podoko\Bencode\Exception\EncoderException;
use stdClass;
#[CoversClass(Encoder::class)]
class EncoderTest extends TestCase
{
protected function encoder(): Encoder
{
return new Encoder();
}
/** @throws \InvalidArgumentException */
#[DataProviderExternal(DataEncodeDecodeProvider::class, 'data_list')]
public function test_encode(mixed $data, string $expected): void
{
self::assertSame($expected, $this->encoder()->encode($data));
}
public function test_encode_sorts_dictionary(): void
{
$data = ['foo' => 'bar', 'baz' => 897, 'nested' => [['hello' => 'there']]];
$expected = 'd3:bazi897e3:foo3:bar6:nestedld5:hello5:thereeee';
self::assertSame($expected, $this->encoder()->encode($data));
}
/** @throws \InvalidArgumentException */
#[DataProviderExternal(DataEncodeDecodeProvider::class, 'data_from_files')]
public function test_encode_from_files(string $source, string $expected): void
{
$data = json_decode(file_get_contents($source), true);
self::assertStringEqualsFile($expected, $this->encoder()->encode($data));
}
#[DataProvider('data_encode_exceptions')]
public function test_encode_exceptions(mixed $data, EncoderException $exception): void
{
$this->expectExceptionObject($exception);
$this->encoder()->encode($data);
}
public static function data_encode_exceptions(): iterable
{
yield 'float' => [[1.2], new EncoderException('Unsupported type: double')];
yield 'true' => [[true], new EncoderException('Unsupported type: boolean')];
yield 'false' => [[false], new EncoderException('Unsupported type: boolean')];
yield 'null' => [[null], new EncoderException('Unsupported type: NULL')];
yield 'resource' => [[fopen(__FILE__, 'r')], new EncoderException('Unsupported type: resource')];
yield 'object' => [new stdClass(), new EncoderException('Unsupported type: object')];
}
}