Completed
Push — master ( a37b73...8ce218 )
by Chris
02:15
created

CachableLoaderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 35.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 20
loc 57
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadWithoutCache() 10 10 1
A testLoadWithCache() 10 10 1
A testLoadCacheWithDefaultTtlOption() 0 9 1
A prophesizeCachePool() 0 22 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <http://chrisyue.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Chrisyue\PhpM3u8\Loader;
13
14
function file_get_contents($uri)
0 ignored issues
show
Unused Code introduced by
The parameter $uri is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
15
{
16
    return 'hello world';
17
}
18
19
namespace Chrisyue\PhpM3u8\Tests\Loader;
20
21
use Chrisyue\PhpM3u8\Loader\CachableLoader;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
22
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
23
24
class CachableLoaderTest extends TestCase
25
{
26 View Code Duplication
    public function testLoadWithoutCache()
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...
27
    {
28
        $uri = 'http://example.com';
29
        $expiresAfter = 1800;
30
        $content = \Chrisyue\PhpM3u8\Loader\file_get_contents($uri);
31
        $cachePool = $this->prophesizeCachePool($uri, false, $content, $expiresAfter);
32
33
        $loader = new CachableLoader($cachePool->reveal(), array('ttl' => $expiresAfter));
34
        $this->assertEquals($loader->load($uri), $content);
35
    }
36
37 View Code Duplication
    public function testLoadWithCache()
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...
38
    {
39
        $uri = 'http://site.com';
40
        $expiresAfter = 1800;
41
        $content = \Chrisyue\PhpM3u8\Loader\file_get_contents($uri);
42
        $cachePool = $this->prophesizeCachePool($uri, true, $content, $expiresAfter);
43
44
        $loader = new CachableLoader($cachePool->reveal(), array('ttl' => $expiresAfter));
45
        $this->assertEquals($loader->load($uri), $content);
46
    }
47
48
    public function testLoadCacheWithDefaultTtlOption()
49
    {
50
        $uri = 'http://another.site.com';
51
        $content = \Chrisyue\PhpM3u8\Loader\file_get_contents($uri);
52
        $cachePool = $this->prophesizeCachePool($uri, false, $content, 3600);
53
54
        $loader = new CachableLoader($cachePool->reveal());
55
        $this->assertEquals($loader->load($uri), $content);
56
    }
57
58
    private function prophesizeCachePool($uri, $isHit, $content, $expiresAfter)
59
    {
60
        $cachePool = $this->prophesize('Psr\Cache\CacheItemPoolInterface');
61
        $cacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
62
63
        $key = md5($uri);
64
        $cachePool->getItem($key)->shouldBeCalledTimes(1)->willReturn($cacheItem->reveal());
65
        $cacheItem->isHit()->shouldBeCalledTimes(1)->willReturn($isHit);
66
67
        if ($isHit) {
68
            $cacheItem->get()->shouldBeCalledTimes(1)->willReturn($content);
69
70
            return $cachePool;
71
        }
72
73
        $cacheItem->set($content)->shouldBeCalledTimes(1);
74
        $cacheItem->expiresAfter($expiresAfter)->shouldBeCalledTimes(1);
75
76
        $cachePool->save($cacheItem->reveal())->shouldBeCalledTimes(1);
77
78
        return $cachePool;
79
    }
80
}
81