Completed
Push — master ( ea9d41...184c59 )
by Sergii
08:21
created

SyncTest::testDirSync()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 26
nc 2
nop 0
1
<?php
2
namespace Tests\AppBundle\Sync;
3
4
use AppBundle\Sync\Entity\Filter\Path;
5
use AppBundle\Sync\Storage\Local;
6
use AppBundle\Sync\Sync;
7
use org\bovigo\vfs\vfsStream;
8
use org\bovigo\vfs\vfsStreamDirectory;
9
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
10
11
/**
12
 * Sync tests
13
 *
14
 * @author Sergey Sadovoi <[email protected]>
15
 */
16
class SyncTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * Root directory of virtual FS
20
     *
21
     * @var vfsStreamDirectory
22
     */
23
    protected $root;
24
25 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $tree = [
28
            'source' => [
29
                'TEST' => [
30
                    'stream' => [
31
                        'TEST00213.mov' => 'test stream content',
32
                        'TEST00313.mov' => 'test stream content',
33
                    ],
34
                    'source' => [
35
                        'TEST00213.mov' => 'test source content',
36
                    ],
37
                ],
38
                'ZZZZ' => [
39
                    'stream' => [
40
                        'ZZZZ00113.mov' => 'test stream content',
41
                    ],
42
                    'source' => [
43
                        'ZZZZ00313.mov' => 'test source content',
44
                    ],
45
                ]
46
            ],
47
            'dest' => [
48
                'TEST' => [
49
                    'TEST00213.mov' => 'test stream content',
50
                    'TEST00313.mov' => 'test stream content 2',
51
                    'TEST00413.mov' => 'test stream content 2',
52
                ],
53
            ]
54
        ];
55
        // Init virtual FS
56
        $this->root = vfsStream::setup('root', null, $tree);
57
    }
58
59
    public function testDirSync()
60
    {
61
        $sync = new Sync();
62
63
        // Set up Master
64
        $pathFilter = new Path();
65
        $pathFilter->setPattern('~[A-Z]{4}/stream/.*\.mov~');
66
67
        $masterStorage = new Local();
68
        $masterPath    = 'root/source';
69
        $masterFilters = [$pathFilter];
70
71
        $sync->setMasterStorage($masterStorage);
72
        $sync->setMasterPath(vfsStream::url($masterPath));
73
        $sync->setMasterFilters($masterFilters);
74
75
        // Set up Slave
76
        $slaveStorage = new Local();
77
        $slavePath    = 'root/dest';
78
        $slavePathTpl = $slavePath . '/__program__/__uid__';
79
80
        $sync->setSlaveStorage($slaveStorage);
81
        $sync->setSlavePath(vfsStream::url($slavePath));
82
        $sync->setSlavePathTpl(vfsStream::url($slavePathTpl));
83
84
        // Run
85
        $sync->run();
86
87
        // Test
88
        $files = [
89
            'root/dest/TEST/TEST00213.mov' => 'test stream content',
90
            'root/dest/TEST/TEST00313.mov' => 'test stream content',
91
            'root/dest/ZZZZ/ZZZZ00113.mov' => 'test stream content',
92
        ];
93
        $removed = 'root/dest/TEST/TEST00413.mov';
94
95
        foreach ($files as $path => $content) {
96
            $this->assertTrue($this->root->hasChild($path));
97
            $this->assertEquals($content, $this->root->getChild($path)->getContent());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method getContent() does only exist in the following implementations of said interface: org\bovigo\vfs\vfsStreamBlock, org\bovigo\vfs\vfsStreamFile.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
98
        }
99
100
        $this->assertFalse($this->root->hasChild($removed));
101
    }
102
}
103