ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
IniFileReadRepositoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\TestCase;
23
24final class IniFileReadRepositoryTest extends TestCase
25{
26 private string $file;
27
28 protected function setUp(): void
29 {
30 $this->file = tempnam(sys_get_temp_dir(), 'ini_read_');
31 file_put_contents(
32 $this->file,
33 "; <?php exit; ?>\n"
34 . "[server]\n"
35 . "absolute_path = \"/var/www/ilias\"\n"
36 . "timezone = \"Europe/Zurich\"\n"
37 . "\n"
38 . "[clients]\n"
39 . "default = \"default\"\n"
40 );
41 }
42
43 protected function tearDown(): void
44 {
45 if (is_file($this->file)) {
46 unlink($this->file);
47 }
48 }
49
51 {
52 $repo = new IniFileReadRepository($this->file);
53 self::assertSame(['server', 'clients'], $repo->getSections());
54 }
55
56 public function testHasSection(): void
57 {
58 $repo = new IniFileReadRepository($this->file);
59 self::assertTrue($repo->hasSection('server'));
60 self::assertFalse($repo->hasSection('missing'));
61 }
62
63 public function testGetReturnsTheStoredValue(): void
64 {
65 $repo = new IniFileReadRepository($this->file);
66 self::assertSame('/var/www/ilias', $repo->get('server', 'absolute_path'));
67 self::assertSame('Europe/Zurich', $repo->get('server', 'timezone'));
68 }
69
70 public function testGetSectionReturnsAllPairs(): void
71 {
72 $repo = new IniFileReadRepository($this->file);
73 self::assertSame(
74 ['absolute_path' => '/var/www/ilias', 'timezone' => 'Europe/Zurich'],
75 $repo->getSection('server')
76 );
77 }
78
79 public function testHas(): void
80 {
81 $repo = new IniFileReadRepository($this->file);
82 self::assertTrue($repo->has('clients', 'default'));
83 self::assertFalse($repo->has('clients', 'missing'));
84 self::assertFalse($repo->has('missing', 'default'));
85 }
86
87 public function testGetOnUnknownKeyThrows(): void
88 {
89 $repo = new IniFileReadRepository($this->file);
90 $this->expectException(InvalidArgumentException::class);
91 $repo->get('server', 'does_not_exist');
92 }
93
95 {
96 $repo = new IniFileReadRepository($this->file);
97 $this->expectException(InvalidArgumentException::class);
98 $repo->getSection('does_not_exist');
99 }
100
102 {
103 $repo = new IniFileReadRepository('/this/path/does/not/exist.ini');
104 self::assertSame([], $repo->getSections());
105 }
106}