Passed
Push — master ( 2a3d19...5e9e4e )
by Caen
05:36 queued 13s
created

CreateActionTest::testOutputPathHelpers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Facades\Filesystem;
8
use Hyde\Framework\Exceptions\FileConflictException;
9
use Hyde\Hyde;
10
use Hyde\Publications\Actions\CreateAction;
11
use Hyde\Testing\TestCase;
12
13
/**
14
 * @covers \Hyde\Publications\Actions\CreateAction
15
 */
16
class CreateActionTest extends TestCase
17
{
18
    public function testCreate()
19
    {
20
        $action = new CreateActionTestClass;
21
        $action->create();
22
23
        $this->assertTrue(file_exists(Hyde::path('foo')));
24
        $this->assertSame('bar', file_get_contents(Hyde::path('foo')));
25
26
        Filesystem::unlink('foo');
27
    }
28
29
    public function testWithConflict()
30
    {
31
        file_put_contents(Hyde::path('foo'), 'keep');
32
        $this->expectException(FileConflictException::class);
33
34
        $action = new CreateActionTestClass;
35
        $action->create();
36
37
        $this->assertSame('keep', file_get_contents(Hyde::path('foo')));
38
39
        Filesystem::unlink('foo');
40
    }
41
42
    public function testWithConflictForce()
43
    {
44
        file_put_contents(Hyde::path('foo'), 'keep');
45
        $action = new CreateActionTestClass;
46
        $action->force()->create();
47
48
        $this->assertSame('bar', file_get_contents(Hyde::path('foo')));
49
50
        Filesystem::unlink('foo');
51
    }
52
53
    public function testOutputPathHelpers()
54
    {
55
        $action = new CreateActionTestClass;
56
        $action->setOutputPath('bar');
57
58
        $this->assertSame('bar', $action->getOutputPath());
59
        $this->assertSame(Hyde::path('bar'), $action->getAbsoluteOutputPath());
60
    }
61
62
    public function testConflictPredictionHelpers()
63
    {
64
        $action = new CreateActionTestClass;
65
66
        $this->assertFalse($action->fileExists());
67
        $this->assertFalse($action->hasFileConflict());
68
69
        file_put_contents(Hyde::path('foo'), 'keep');
70
        $this->assertTrue($action->fileExists());
71
        $this->assertTrue($action->hasFileConflict());
72
73
        $action->force();
74
        $this->assertFalse($action->hasFileConflict());
75
76
        $action->force(false);
77
        $this->assertTrue($action->hasFileConflict());
78
79
        Filesystem::unlink('foo');
80
    }
81
82
    public function testCanSaveToSubdirectory()
83
    {
84
        $action = new CreateActionTestClass;
85
        $action->setOutputPath('foo/bar');
86
        $action->create();
87
88
        $this->assertTrue(file_exists(Hyde::path('foo/bar')));
89
        $this->assertSame('bar', file_get_contents(Hyde::path('foo/bar')));
90
        unlink(Hyde::path('foo/bar'));
91
        rmdir(Hyde::path('foo'));
92
    }
93
94
    public function testFormatStringForStorage()
95
    {
96
        $action = new CreateActionTestClass;
97
        $this->assertSame('hello-world', $action->getFormattedNameForStorage());
98
    }
99
}
100
101
class CreateActionTestClass extends CreateAction
102
{
103
    protected string $outputPath = 'foo';
104
105
    protected function handleCreate(): void
106
    {
107
        $this->save('bar');
108
    }
109
110
    public function getFormattedNameForStorage(): string
111
    {
112
        return $this->formatStringForStorage('Hello World!');
113
    }
114
}
115