Completed
Push — master ( 8cb42d...e9bfab )
by
unknown
19:56
created

CollectionCacheTest::testbuildCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * CollectionCache class file
4
 */
5
6
namespace Graviton\DocumentBundle\Tests\Service;
7
8
use Graviton\CoreBundle\Repository\AppRepository;
9
use Graviton\DocumentBundle\Service\CollectionCache;
10
use Graviton\TestBundle\Test\RestTestCase;
11
use GravitonDyn\EventStatusBundle\Document\EventStatus;
12
use GravitonDyn\EventStatusBundle\Document\EventStatusStatus;
13
14
/**
15
 * ExtReferenceConverter test
16
 *
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/GPL GPL
19
 * @link     http://swisscom.ch
20
 */
21
class CollectionCacheTest extends RestTestCase
22
{
23
    /** @var  CollectionCache */
24
    private $cache;
25
26
    /**
27
     * setup type we want to test
28
     *
29
     * @return void
30
     */
31
    public function setUp()
32
    {
33
        $this->cache = $this->getContainer()->get('graviton.document.service.collectioncache');
34
        $config = $this->getContainer()->getParameter('graviton.cache.collections');
35
        if (!array_key_exists('EventStatus', $config)) {
36
            $config['EventStatus'] = 10;
37
            $this->cache->setConfiguration($config);
38
        }
39
    }
40
41
    /**
42
     * private build key test Makes an id
43
     *
44
     * @return void
45
     */
46
    public function testbuildCacheKey()
47
    {
48
        $collection = 'App';
49
        $id = 'kjeGd-213-csd';
50
        $key = CollectionCache::BASE_KEY.'-'.preg_replace("/[^a-zA-Z0-9_-]+/", "-", $collection.'-'.$id);
51
52
        $method = $this->getPrivateClassMethod(get_class($this->cache), 'buildCacheKey');
53
        $result = $method->invokeArgs($this->cache, [$collection, $id]);
54
55
        $this->assertEquals($key, $result);
56
    }
57
58
    /**
59
     * Time it should be in cache and if so should happen
60
     *
61
     * @return void
62
     */
63
    public function testgetCollectionCacheTime()
64
    {
65
        $collection = 'EventStatus';
66
67
        $config = $this->getContainer()->getParameter('graviton.cache.collections');
68
        $method = $this->getPrivateClassMethod(get_class($this->cache), 'getCollectionCacheTime');
69
        $result = $method->invokeArgs($this->cache, [$collection]);
70
71
        $this->assertEquals($config['EventStatus'], $result);
72
    }
73
74
    /**
75
     * Cache testing
76
     *
77
     * @return void
78
     */
79
    public function testsetAndgetByRepository()
80
    {
81
        /** @var AppRepository $repository */
82
        $repository = $this->getContainer()->get('gravitondyn.eventstatus.repository.eventstatus');
83
        $document = new EventStatus();
84
        $document->setId('testing-cache');
85
        $document->setCreatedate(new \DateTime());
86
        $this->cache->setByRepository($repository, serialize($document), 'testing-cache');
87
88
        $stringCache = $this->cache->getByRepository($repository, 'testing-cache');
89
        $object = unserialize($stringCache);
90
        $this->assertEquals($document->getId(), $object->getId());
91
        $this->assertEquals($document->getCreatedate(), $object->getCreatedate());
92
    }
93
94
    /**
95
     * Cache testing
96
     *
97
     * @return void
98
     */
99
    public function testsetAndgetByRepositoryFalse()
100
    {
101
        /** @var AppRepository $repository */
102
        $repository = $this->getContainer()->get('gravitondyn.eventstatus.repository.eventstatusstatus');
103
        $document = new EventStatusStatus();
104
        $document->setId('testing-cache-2');
105
        $shouldNotBeSet = $this->cache->setByRepository($repository, serialize($document), $document->getId());
106
107
        $this->assertFalse($shouldNotBeSet, '');
108
    }
109
110
    /**
111
     * Update lock tests
112
     *
113
     * @return void
114
     */
115
    public function testLocks()
116
    {
117
        /** @var AppRepository $repository */
118
        $repository = $this->getContainer()->get('gravitondyn.eventstatus.repository.eventstatus');
119
120
        $id = 'ocack-test';
121
        $this->cache->addUpdateLock($repository, $id, 1);
122
        $start = microtime(true);
123
        $shouldHaveBeenReleased = $start + 3;
124
        $this->cache->updateOperationCheck($repository, $id);
125
        $end = microtime(true);
126
        $this->assertTrue(($start+0.15) < $end);
0 ignored issues
show
introduced by
Instead of assertTrue() use $this->assertLessThan($start + 0.15, $end). This will lead to a better error message when the test fails.
Loading history...
127
128
        $this->assertTrue($end < $shouldHaveBeenReleased);
0 ignored issues
show
introduced by
Instead of assertTrue() use $this->assertLessThan($e...shouldHaveBeenReleased). This will lead to a better error message when the test fails.
Loading history...
129
    }
130
}
131