summaryrefslogtreecommitdiff
path: root/vendor/guzzle/guzzle/tests/Guzzle/Tests/Cache/ClosureCacheAdapterTest.php
blob: 12de65b5aa56fb8474a28e1b1741a0cc4465699b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

namespace Guzzle\Tests\Cache;

use Guzzle\Cache\ClosureCacheAdapter;

/**
 * @covers Guzzle\Cache\ClosureCacheAdapter
 */
class ClosureCacheAdapterTest extends \Guzzle\Tests\GuzzleTestCase
{
    /** @var ClosureCacheAdapter */
    private $adapter;

    /** Array of callables to use for testing */
    private $callables;

    /** Cache data for testing */
    public $data = array();

    /**
     * Prepares the environment before running a test.
     */
    protected function setUp()
    {
        parent::setUp();

        $that = $this;
        $this->callables = array(
            'contains' => function($id, $options = array()) use ($that) {
                return array_key_exists($id, $that->data);
            },
            'delete' => function($id, $options = array()) use ($that) {
                unset($that->data[$id]);
                return true;
            },
            'fetch' => function($id, $options = array()) use ($that) {
                return array_key_exists($id, $that->data) ? $that->data[$id] : null;
            },
            'save' => function($id, $data, $lifeTime, $options = array()) use ($that) {
                $that->data[$id] = $data;
                return true;
            }
        );

        $this->adapter = new ClosureCacheAdapter($this->callables);
    }

    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown()
    {
        $this->cache = null;
        $this->callables = null;
        parent::tearDown();
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testEnsuresCallablesArePresent()
    {
        $callables = $this->callables;
        unset($callables['delete']);
        $cache = new ClosureCacheAdapter($callables);
    }

    public function testAllCallablesMustBePresent()
    {
        $cache = new ClosureCacheAdapter($this->callables);
    }

    public function testCachesDataUsingCallables()
    {
        $this->assertTrue($this->adapter->save('test', 'data', 1000));
        $this->assertEquals('data', $this->adapter->fetch('test'));
    }

    public function testChecksIfCacheContainsKeys()
    {
        $this->adapter->save('test', 'data', 1000);
        $this->assertTrue($this->adapter->contains('test'));
        $this->assertFalse($this->adapter->contains('foo'));
    }

    public function testDeletesFromCacheByKey()
    {
        $this->adapter->save('test', 'data', 1000);
        $this->assertTrue($this->adapter->contains('test'));
        $this->adapter->delete('test');
        $this->assertFalse($this->adapter->contains('test'));
    }
}