File: SourceCacheTest.php

package info (click to toggle)
php-di 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,932 kB
  • sloc: php: 10,572; makefile: 42; xml: 17; sh: 10; pascal: 5
file content (113 lines) | stat: -rw-r--r-- 3,268 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);

namespace DI\Test\UnitTest\Definition\Source;

use DI\Definition\ObjectDefinition;
use DI\Definition\Reference;
use DI\Definition\Source\DefinitionArray;
use DI\Definition\Source\DefinitionSource;
use DI\Definition\Source\SourceCache;
use PHPUnit\Framework\TestCase;

class SourceCacheTest extends TestCase
{
    public function setUp(): void
    {
        parent::setUp();

        if (! SourceCache::isSupported()) {
            $this->markTestSkipped('APCu extension is required');
        }
        apcu_clear_cache();
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_get_from_cache()
    {
        $definition = new ObjectDefinition('foo');

        $wrappedSource = $this->createMock(DefinitionSource::class);
        $wrappedSource
            ->expects($this->once())// The sub-source should be called ONLY ONCE
            ->method('getDefinition')
            ->willReturn($definition);

        $source = new SourceCache($wrappedSource);

        self::assertEquals($definition, $source->getDefinition('foo'));
        self::assertEquals($definition, $source->getDefinition('foo'));
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_save_to_cache_and_return()
    {
        $cachedSource = new DefinitionArray([
            'foo' => \DI\create(),
        ]);

        $source = new SourceCache($cachedSource);

        // Sanity check
        self::assertSavedInCache('foo', null);
        // Return the definition
        self::assertEquals(new ObjectDefinition('foo'), $source->getDefinition('foo'));
        // The definition is saved in the cache
        self::assertSavedInCache('foo', new ObjectDefinition('foo'));
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_save_null_to_cache_and_return_null()
    {
        $source = new SourceCache(new DefinitionArray);

        self::assertNull($source->getDefinition('foo'));
        self::assertSavedInCache('foo', null);
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_only_cache_object_and_autowire_definitions()
    {
        $definition = new Reference('foo');

        $wrappedSource = $this->createMock(DefinitionSource::class);
        $wrappedSource
            ->expects($this->exactly(2))
            ->method('getDefinition')
            ->willReturn($definition);

        $source = new SourceCache($wrappedSource);

        self::assertSame($definition, $source->getDefinition('foo'));
        self::assertSame($definition, $source->getDefinition('foo'));
    }

    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_use_namespaced_cache_keys()
    {
        $namespace = 'staging';
        $source = new SourceCache(new DefinitionArray, $namespace);
        self::assertSame($source->getCacheKey('foo'), SourceCache::CACHE_KEY . $namespace . 'foo');
    }

    private static function assertSavedInCache(string $definitionName, $expectedValue)
    {
        $definition = apcu_fetch(SourceCache::CACHE_KEY . $definitionName);
        self::assertEquals($expectedValue, $definition);
    }
}