Passed
Branch kernel-refactor-experiment (6c84fd)
by Caen
03:50
created

FilesystemSafeCopyHelperTest::testDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Testing\Unit\Foundation;
4
5
use Hyde\Framework\Foundation\Filesystem;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\HydeKernel;
8
use Hyde\Testing\TestCase;
9
10
/**
11
 * @covers \Hyde\Framework\Foundation\Filesystem::copy
12
 * @covers \Hyde\Framework\HydeKernel::copy
13
 */
14
class FilesystemSafeCopyHelperTest extends TestCase
15
{
16
    protected static function testDir(string $path = ''): string
17
    {
18
        return __DIR__.'HydeSafeCopyHelperTestTemp'.$path;
19
    }
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
25
        mkdir(static::testDir());
26
    }
27
28
    public function test_copy_method_exists()
29
    {
30
        $this->assertTrue(method_exists(HydeKernel::class, 'copy'));
31
        $this->assertTrue(method_exists(Filesystem::class, 'copy'));
32
    }
33
34
    public function test_copy_method_returns404_if_source_file_does_not_exist()
35
    {
36
        $this->assertEquals(
37
            404,
38
            Hyde::copy(static::testDir('/does/not/exist.txt'), static::testDir('/test.txt'))
39
        );
40
    }
41
42
    public function test_copy_method_returns409_if_destination_file_exists_and_force_flag_is_not_set()
43
    {
44
        file_put_contents(static::testDir('/test.txt'), 'test');
45
46
        $this->assertEquals(
47
            409,
48
            Hyde::copy(static::testDir('/test.txt'), static::testDir('/test.txt'))
49
        );
50
51
        unlink(static::testDir('/test.txt'));
52
    }
53
54
    public function test_copy_method_returns_true_if_destination_file_exists_and_force_flag_is_set()
55
    {
56
        file_put_contents(static::testDir('/foo.txt'), 'foo');
57
58
        $this->assertTrue(
59
            Hyde::copy(static::testDir('/foo.txt'), static::testDir('/bar.txt'), true)
60
        );
61
62
        unlink(static::testDir('/foo.txt'));
63
        unlink(static::testDir('/bar.txt'));
64
    }
65
66
    public function test_file_with_no_conflicts_is_copied()
67
    {
68
        file_put_contents(static::testDir('/foo.txt'), 'foo');
69
70
        $this->assertTrue(
71
            Hyde::copy(static::testDir('/foo.txt'), static::testDir('/bar.txt'))
72
        );
73
74
        $this->assertFileExists(static::testDir('/bar.txt'));
75
    }
76
77
    public function test_file_with_conflicts_is_not_copied_when_force_flag_is_not_set()
78
    {
79
        file_put_contents(static::testDir('/foo.txt'), 'foo');
80
        file_put_contents(static::testDir('/bar.txt'), 'bar');
81
82
        $this->assertEquals(
83
            409,
84
            Hyde::copy(static::testDir('/foo.txt'), static::testDir('/bar.txt'))
85
        );
86
87
        $this->assertStringEqualsFile(static::testDir('/bar.txt'), 'bar');
88
    }
89
90
    public function test_file_with_conflicts_is_copied_when_force_flag_is_set()
91
    {
92
        file_put_contents(static::testDir('/foo.txt'), 'foo');
93
        file_put_contents(static::testDir('/bar.txt'), 'bar');
94
95
        $this->assertTrue(
96
            Hyde::copy(static::testDir('/foo.txt'), static::testDir('/bar.txt'), true)
97
        );
98
99
        $this->assertStringEqualsFile(static::testDir('/bar.txt'), 'foo');
100
    }
101
102
    public function tearDown(): void
103
    {
104
        deleteDirectory(static::testDir());
105
106
        parent::tearDown();
107
    }
108
}
109