|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 | +namespace Symfony\Component\Cache\Tests\Adapter; |
| 13 | + |
| 14 | +use Cache\IntegrationTests\CachePoolTest; |
| 15 | +use Psr\Cache\CacheItemInterface; |
| 16 | +use Symfony\Component\Cache\Adapter\OpCacheAdapter; |
| 17 | + |
| 18 | +/** |
| 19 | + * @group time-sensitive |
| 20 | + */ |
| 21 | +class OpCacheNotSerializedAdapterTest extends CachePoolTest |
| 22 | +{ |
| 23 | + private $filename; |
| 24 | + |
| 25 | + protected $skippedTests = array( |
| 26 | + 'testBasicUsage' => 'OpCacheAdpater is read-only.', |
| 27 | + 'testClear' => 'OpCacheAdpater is read-only.', |
| 28 | + 'testClearWithDeferredItems' => 'OpCacheAdpater is read-only.', |
| 29 | + 'testDeleteItem' => 'OpCacheAdpater is read-only.', |
| 30 | + 'testSave' => 'OpCacheAdpater is read-only.', |
| 31 | + 'testSaveExpired' => 'OpCacheAdpater is read-only.', |
| 32 | + 'testSaveWithoutExpire' => 'OpCacheAdpater is read-only.', |
| 33 | + 'testDeferredSave' => 'OpCacheAdpater is read-only.', |
| 34 | + 'testDeferredSaveWithoutCommit' => 'OpCacheAdpater is read-only.', |
| 35 | + 'testDeleteItems' => 'OpCacheAdpater is read-only.', |
| 36 | + 'testDeleteDeferredItem' => 'OpCacheAdpater is read-only.', |
| 37 | + 'testCommit' => 'OpCacheAdpater is read-only.', |
| 38 | + 'testSaveDeferredWhenChangingValues' => 'OpCacheAdpater is read-only.', |
| 39 | + 'testSaveDeferredOverwrite' => 'OpCacheAdpater is read-only.', |
| 40 | + 'testSavingObject' => 'OpCacheAdpater is read-only.', |
| 41 | + |
| 42 | + 'testDataTypeObject' => 'OpCacheAdpater without serialization does not support objects.', |
| 43 | + |
| 44 | + 'testExpiresAt' => 'OpCacheAdpater does not support expiration.', |
| 45 | + 'testExpiresAtWithNull' => 'OpCacheAdpater does not support expiration.', |
| 46 | + 'testExpiresAfterWithNull' => 'OpCacheAdpater does not support expiration.', |
| 47 | + 'testDeferredExpired' => 'OpCacheAdpater does not support expiration.', |
| 48 | + 'testExpiration' => 'OpCacheAdpater does not support expiration.', |
| 49 | + |
| 50 | + // todo |
| 51 | + 'testIsHit' => 'OpCacheAdpater is read-only.', |
| 52 | + 'testIsHitDeferred' => 'OpCacheAdpater is read-only.', |
| 53 | + 'testDataTypeString' => 'OpCacheAdpater is read-only.', |
| 54 | + 'testDataTypeInteger' => 'OpCacheAdpater is read-only.', |
| 55 | + 'testDataTypeNull' => 'OpCacheAdpater is read-only.', |
| 56 | + 'testDataTypeFloat' => 'OpCacheAdpater is read-only.', |
| 57 | + 'testDataTypeBoolean' => 'OpCacheAdpater is read-only.', |
| 58 | + 'testDataTypeArray' => 'OpCacheAdpater is read-only.', |
| 59 | + ); |
| 60 | + |
| 61 | + public function createCachePool() |
| 62 | + { |
| 63 | + $this->filename = sys_get_temp_dir() . '/symfony-cache/OpCache/not_serialized.php'; |
| 64 | + |
| 65 | + return new OpCacheAdapter($this->filename, '', 0, false); |
| 66 | + } |
| 67 | + |
| 68 | + public function testWarmUp() |
| 69 | + { |
| 70 | + $values = [ |
| 71 | + 'string' => 'string', |
| 72 | + 'integer' => 42, |
| 73 | + 'null' => null, |
| 74 | + 'float' => 42.42, |
| 75 | + 'boolean' => true, |
| 76 | + 'array_simple' => [ 'foo', 'bar' ], |
| 77 | + 'array_associative' => [ 'foo' => 'bar', 'foo2' => 'bar2' ], |
| 78 | + ]; |
| 79 | + |
| 80 | + $adapter = $this->createCachePool(); |
| 81 | + $adapter->warmUp($values); |
| 82 | + |
| 83 | + $this->assertEquals( |
| 84 | + file_get_contents(__DIR__ . '/../Fixtures/OpCache/not_serialized.php'), |
| 85 | + file_get_contents($this->filename), |
| 86 | + 'Warm up should create a PHP file that OPCache can load in memory' |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public function testGetItem() |
| 91 | + { |
| 92 | + $adapter = $this->createCachePool(); |
| 93 | + $adapter->warmUp([ 'key' => 'value' ]); |
| 94 | + |
| 95 | + // get existing item |
| 96 | + $item = $adapter->getItem('key'); |
| 97 | + $this->assertEquals('value', $item->get(), 'A stored item must be returned from cached.'); |
| 98 | + $this->assertEquals('key', $item->getKey(), 'Cache key can not change.'); |
| 99 | + |
| 100 | + // get non-existent item |
| 101 | + $item = $adapter->getItem('key2'); |
| 102 | + $this->assertFalse($item->isHit()); |
| 103 | + $this->assertNull($item->get(), "Item's value must be null when isHit is false."); |
| 104 | + } |
| 105 | + |
| 106 | + public function testGetItems() |
| 107 | + { |
| 108 | + $adapter = $this->createCachePool(); |
| 109 | + $adapter->warmUp([ 'foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz' ]); |
| 110 | + |
| 111 | + $keys = ['foo', 'bar', 'baz', 'biz']; |
| 112 | + |
| 113 | + /** @type CacheItemInterface[] $items */ |
| 114 | + $items = $adapter->getItems($keys); |
| 115 | + $count = 0; |
| 116 | + |
| 117 | + foreach ($items as $key => $item) { |
| 118 | + $itemKey = $item->getKey(); |
| 119 | + |
| 120 | + $this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items'); |
| 121 | + $this->assertEquals($key !== 'biz', $item->isHit()); |
| 122 | + $this->assertTrue(in_array($key, $keys), 'Cache key can not change.'); |
| 123 | + |
| 124 | + // Remove $key for $keys |
| 125 | + foreach ($keys as $k => $v) { |
| 126 | + if ($v === $key) { |
| 127 | + unset($keys[$k]); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + $count++; |
| 132 | + } |
| 133 | + |
| 134 | + $this->assertSame(4, $count); |
| 135 | + } |
| 136 | + |
| 137 | + public function testHasItem() |
| 138 | + { |
| 139 | + $adapter = $this->createCachePool(); |
| 140 | + $adapter->warmUp([ 'key' => 'foo' ]); |
| 141 | + |
| 142 | + // has existing item |
| 143 | + $this->assertTrue($adapter->hasItem('key')); |
| 144 | + |
| 145 | + // has non-existent item |
| 146 | + $this->assertFalse($adapter->hasItem('key2')); |
| 147 | + } |
| 148 | + |
| 149 | + public function testKeyLength() |
| 150 | + { |
| 151 | + $key = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.'; |
| 152 | + |
| 153 | + $adapter = $this->createCachePool(); |
| 154 | + $adapter->warmUp([ $key => 'value' ]); |
| 155 | + |
| 156 | + $this->assertTrue($adapter->hasItem($key), 'The implementation does not support a valid cache key'); |
| 157 | + } |
| 158 | +} |
0 commit comments