• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Metadata/Tests/Property/Factory/CachedPropertyNameCollectionFactoryTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Metadata\Tests\Property\Factory;
15

16
use ApiPlatform\Metadata\Property\Factory\CachedPropertyNameCollectionFactory;
17
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
18
use ApiPlatform\Metadata\Property\PropertyNameCollection;
19
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Dummy;
20
use PHPUnit\Framework\TestCase;
21
use Prophecy\PhpUnit\ProphecyTrait;
22
use Psr\Cache\CacheException;
23
use Psr\Cache\CacheItemInterface;
24
use Psr\Cache\CacheItemPoolInterface;
25

26
/**
27
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
28
 */
29
class CachedPropertyNameCollectionFactoryTest extends TestCase
30
{
31
    use ProphecyTrait;
32

33
    public function testCreateWithItemHit(): void
34
    {
35
        $cacheItem = $this->prophesize(CacheItemInterface::class);
×
36
        $cacheItem->isHit()->willReturn(true)->shouldBeCalled();
×
37
        $cacheItem->get()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummy']))->shouldBeCalled();
×
38

39
        $cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
×
40
        $cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
×
41

42
        $decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
43

44
        $cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
×
45
        $resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
×
46

47
        $expectedResult = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
×
48
        $this->assertEquals($expectedResult, $resultedPropertyNameCollection);
×
49
        $this->assertEquals($expectedResult, $cachedPropertyNameCollectionFactory->create(Dummy::class), 'Trigger the local cache');
×
50
    }
51

52
    public function testCreateWithItemNotHit(): void
53
    {
54
        $resourceNameCollection = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
×
55

56
        $decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
57
        $decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn($resourceNameCollection)->shouldBeCalled();
×
58

59
        $cacheItem = $this->prophesize(CacheItemInterface::class);
×
60
        $cacheItem->isHit()->willReturn(false)->shouldBeCalled();
×
61
        $cacheItem->set($resourceNameCollection)->willReturn($cacheItem->reveal())->shouldBeCalled();
×
62

63
        $cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
×
64
        $cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
×
65
        $cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalled();
×
66

67
        $cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
×
68
        $resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
×
69

70
        $expectedResult = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
×
71
        $this->assertEquals($expectedResult, $resultedPropertyNameCollection);
×
72
        $this->assertEquals($expectedResult, $cachedPropertyNameCollectionFactory->create(Dummy::class), 'Trigger the local cache');
×
73
    }
74

75
    public function testCreateWithGetCacheItemThrowsCacheException(): void
76
    {
77
        $decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
78
        $decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummy']))->shouldBeCalled();
×
79

NEW
80
        $cacheException = new class extends \Exception implements CacheException {};
×
81

82
        $cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
×
83
        $cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException)->shouldBeCalled();
×
84

85
        $cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
×
86
        $resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
×
87

88
        $expectedResult = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
×
89
        $this->assertEquals($expectedResult, $resultedPropertyNameCollection);
×
90
        $this->assertEquals($expectedResult, $cachedPropertyNameCollectionFactory->create(Dummy::class), 'Trigger the local cache');
×
91
    }
92

93
    private function generateCacheKey(string $resourceClass = Dummy::class, array $options = []): string
94
    {
95
        return CachedPropertyNameCollectionFactory::CACHE_KEY_PREFIX.hash('xxh3', serialize([$resourceClass, $options]));
×
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc