/
WriteHandleTest.php
71 lines (52 loc) 路 2.06 KB
/
WriteHandleTest.php
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
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Psl\Tests\Unit\File;
use PHPUnit\Framework\TestCase;
use Psl\Exception\InvariantViolationException;
use Psl\File;
use Psl\Filesystem;
final class WriteHandleTest extends TestCase
{
public function testMustCreateExistingFile(): void
{
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('already exists.');
new File\WriteHandle(__FILE__, File\WriteMode::MUST_CREATE);
}
public function testAppendToNonExistingFile(): void
{
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('does not exist.');
$f = new File\WriteHandle(__FILE__ . '.fake', File\WriteMode::APPEND);
$f->write('g');
}
public function testAppendToANonWritableFile(): void
{
$temporary_file = Filesystem\create_temporary_file();
Filesystem\change_permissions($temporary_file, 0555);
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('is not writable.');
new File\WriteHandle($temporary_file, File\WriteMode::APPEND);
}
public function testWriting(): void
{
$temporary_file = Filesystem\create_temporary_file();
$handle = File\open_write_only($temporary_file);
$handle->writeAll('hello');
static::assertSame(2, $handle->write(', '));
static::assertSame(6, $handle->writeImmediately('world!'));
$handle->close();
$handle = File\open_read_only($temporary_file);
$content = $handle->readAll();
static::assertSame('hello, world!', $content);
}
public function testCreateNonExisting(): void
{
$temporary_file = Filesystem\create_temporary_file();
Filesystem\delete_file($temporary_file);
static::assertFalse(Filesystem\is_file($temporary_file));
$handle = new File\WriteHandle($temporary_file, File\WriteMode::MUST_CREATE);
$handle->close();
static::assertTrue(Filesystem\is_file($temporary_file));
}
}