Completed
Push — feature/EVO-8323-configurable-... ( 36aaf7 )
by
unknown
15:55
created

CollectionCacheTest::testgetCollectionCacheTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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, $document);
87
88
        $object = $this->cache->getByRepository($repository, 'testing-cache');
89
        $this->assertEquals($document->getId(), $object->getId());
90
        $this->assertEquals($document->getCreatedate(), $object->getCreatedate());
91
    }
92
93
    /**
94
     * Cache testing
95
     *
96
     * @return void
97
     */
98
    public function testsetAndgetByRepositoryFalse()
99
    {
100
        /** @var AppRepository $repository */
101
        $repository = $this->getContainer()->get('gravitondyn.eventstatus.repository.eventstatusstatus');
102
        $document = new EventStatusStatus();
103
        $document->setId('testing-cache-2');
104
        $shouldNotBeSet = $this->cache->setByRepository($repository, $document);
105
106
        $this->assertFalse($shouldNotBeSet, '');
107
    }
108
109
    public function testLocks()
110
    {
111
        /** @var AppRepository $repository */
112
        $repository = $this->getContainer()->get('gravitondyn.eventstatus.repository.eventstatus');
113
114
        $id = 'ocack-test';
115
        $this->cache->addUpdateLock($repository, $id, 0.4);
116
        $start = microtime(true);
117
        $shouldHaveBeenReleased = $start + 500;
118
        $this->cache->updateOperationCheck($repository, $id);
119
        $end = microtime(true);
120
        $this->assertTrue(($start < $end) && ($end < $shouldHaveBeenReleased));
121
    }
122
}
123