HandlerTraitTest::testComputeExpireTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Cache;
3
4
use Fwlib\Cache\HandlerTrait as CacheHandlerTrait;
5
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
6
use PHPUnit_Framework_MockObject_MockObject as MockObject;
7
8
/**
9
 * @copyright   Copyright 2015 Fwolf
10
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
11
 */
12
class HandlerTraitTest extends PHPUnitTestCase
13
{
14
    /**
15
     * @return MockObject | CacheHandlerTrait
16
     */
17
    protected function buildMock()
18
    {
19
        $mock = $this->getMockBuilder(CacheHandlerTrait::class)
20
            ->setMethods(null)
21
            ->getMockForTrait();
22
23
        return $mock;
24
    }
25
26
27
    public function testComputeExpireTime()
28
    {
29
        $handler = $this->buildMock();
30
31
        $expireTime = $this->reflectionCall($handler, 'computeExpireTime');
32
        $this->assertEquals(0, $expireTime);
33
34
        $expireTime =
35
            $this->reflectionCall($handler, 'computeExpireTime', [0]);
36
        $this->assertEquals(0, $expireTime);
37
38
        $expireTime =
39
            $this->reflectionCall($handler, 'computeExpireTime', [10, 300]);
40
        $this->assertEquals(310, $expireTime);
41
42
        $now = time();
43
        $expireTime =
44
            $this->reflectionCall($handler, 'computeExpireTime', [30, 0]);
45
        $this->assertGreaterThan($now + 20, $expireTime);
46
    }
47
48
49
    public function testHashKey()
50
    {
51
        $handler = $this->buildMock();
52
53
        $handler->emptyKeyReplacement = '[emptyKey]';
0 ignored issues
show
Bug introduced by
Accessing emptyKeyReplacement on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
55
        $this->assertEquals(
56
            '[emptyKey]',
57
            $this->reflectionCall($handler, 'hashKey', [''])
58
        );
59
60
        $handler->hashAlgorithm = '';
0 ignored issues
show
Bug introduced by
Accessing hashAlgorithm on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
61
        $hashedKey = $this->reflectionCall($handler, 'hashKey', ['foo']);
62
        $this->assertEquals('foo', $hashedKey);
63
64
        $handler->hashAlgorithm = 'crc32b';
0 ignored issues
show
Bug introduced by
Accessing hashAlgorithm on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
65
        $hashedKey = $this->reflectionCall($handler, 'hashKey', ['foo']);
66
        $this->assertNotEquals('foo', $hashedKey);
67
        $this->assertNotEmpty($hashedKey);
68
    }
69
}
70