BackupResourceCommandTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 98.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 112
ccs 58
cts 59
cp 0.9831
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A getCommand() 0 4 1
A getResourceDO() 0 4 1
A getResourceDOMock() 0 10 1
A testBackupEmptyResource() 0 6 1
A testBackupFirstVersionOfTheResource() 0 15 1
A testBackupResourceToTheSpecificVersion() 0 15 1
A testBackupResourceWhenAnotherCopyAlreadyExist() 0 20 1
1
<?php
2
namespace Staticus\Resources\Commands;
3
4
use League\Flysystem\Filesystem;
5
use League\Flysystem\Memory\MemoryAdapter;
6
use Staticus\Resources\File\ResourceDO;
7
use Staticus\Resources\ResourceDOInterface;
8
9
class BackupResourceCommandTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var ResourceDO
13
     */
14
    protected $resourceDO;
15
    /**
16
     * @var Filesystem
17
     */
18
    protected $filesystem;
19
20 4
    protected function setUp()
21
    {
22 4
        parent::setUp();
23 4
        $this->resourceDO = new ResourceDO();
24 4
        $this->filesystem = new Filesystem(new MemoryAdapter());
25 4
    }
26
27
    /**
28
     * @return BackupResourceCommand
29
     */
30 4
    public function getCommand(ResourceDO $resourceDO)
31
    {
32 4
        return new BackupResourceCommand($resourceDO, $this->filesystem);
33
    }
34
35
    /**
36
     * @return ResourceDO
37
     */
38 1
    public function getResourceDO()
39
    {
40 1
        return clone $this->resourceDO;
41
    }
42
43
    /**
44
     * @return ResourceDO
45
     */
46 3
    public function getResourceDOMock()
47
    {
48 3
        $resourceDO = clone $this->resourceDO;
49
50
        return $resourceDO
51 3
            ->setBaseDirectory('testBase')
52 3
            ->setName('testResource')
53 3
            ->setType('testType')
54 3
        ;
55
    }
56
57
    /**
58
     * @expectedException \Staticus\Resources\Exceptions\CommandErrorException
59
     * @expectedExceptionMessage Can not look for options: resource is empty
60
     */
61 1
    public function testBackupEmptyResource()
62
    {
63 1
        $resourceDO = $this->getResourceDO();
64 1
        $command = $this->getCommand($resourceDO);
65 1
        $command();
66
    }
67
68 1
    public function testBackupFirstVersionOfTheResource()
69
    {
70 1
        $resourceDO_v0 = $this->getResourceDOMock();
71 1
        $this->filesystem->put($resourceDO_v0->getFilePath(), '');
72
73 1
        $expectedVersion = '1';
74 1
        $command = $this->getCommand($resourceDO_v0);
75 1
        $result = $command();
76 1
        $this->assertInstanceOf(ResourceDOInterface::class, $result);
77 1
        $this->assertEquals($expectedVersion, $result->getVersion());
78
79 1
        $resourceDO_v1 = clone $resourceDO_v0;
80 1
        $resourceDO_v1->setVersion($expectedVersion);
81 1
        $this->assertTrue($this->filesystem->has($resourceDO_v1->getFilePath(), ''));
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::has() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
82 1
    }
83
84 2
    public function testBackupResourceToTheSpecificVersion()
85
    {
86 1
        $resourceDO_v0 = $this->getResourceDOMock();
87 1
        $this->filesystem->put($resourceDO_v0->getFilePath(), '');
88
89 1
        $expectedVersion = '8';
90 1
        $command = $this->getCommand($resourceDO_v0);
91 1
        $result = $command(7);
92 2
        $this->assertInstanceOf(ResourceDOInterface::class, $result);
93 1
        $this->assertEquals($expectedVersion, $result->getVersion());
94
95 1
        $resourceDO_v8 = clone $resourceDO_v0;
96 1
        $resourceDO_v8->setVersion($expectedVersion);
97 1
        $this->assertTrue($this->filesystem->has($resourceDO_v8->getFilePath(), ''));
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::has() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
98 1
    }
99
100 1
    public function testBackupResourceWhenAnotherCopyAlreadyExist()
101
    {
102 1
        $resourceDO_v0 = $this->getResourceDOMock();
103 1
        $this->filesystem->put($resourceDO_v0->getFilePath(), '');
104
105
        // Put one more version on disk
106 1
        $resourceDO_v1 = clone $resourceDO_v0;
107 1
        $resourceDO_v1->setVersion($resourceDO_v0->getVersion() + 1);
108 1
        $this->filesystem->put($resourceDO_v1->getFilePath(), '');
109
110 1
        $expectedVersion = (string)($resourceDO_v0->getVersion() + 2);
111 1
        $command = $this->getCommand($resourceDO_v0);
112 1
        $result = $command();
113 1
        $this->assertInstanceOf(ResourceDOInterface::class, $result);
114 1
        $this->assertEquals($expectedVersion, $result->getVersion());
115
116 1
        $resourceDO_v2 = clone $resourceDO_v0;
117 1
        $resourceDO_v2->setVersion($expectedVersion);
118 1
        $this->assertTrue($this->filesystem->has($resourceDO_v2->getFilePath(), ''));
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::has() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119 1
    }
120
}
121