|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TraderInteractive\Api; |
| 4 | + |
| 5 | +use MongoDB; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use SubjectivePHP\Psr\SimpleCache\InMemoryCache; |
| 8 | +use SubjectivePHP\Psr\SimpleCache\MongoCache; |
| 9 | +use SubjectivePHP\Psr\SimpleCache\NullCache; |
| 10 | +use SubjectivePHP\Psr\SimpleCache\RedisCache; |
| 11 | + |
| 12 | +/** |
| 13 | + * @coversDefaultClass \TraderInteractive\Api\CacheFactory |
| 14 | + * @covers ::<private> |
| 15 | + */ |
| 16 | +final class CacheFactoryTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param string $name The name of the cache to create. |
| 20 | + * @param array $config Config data to pass to the factory. |
| 21 | + * |
| 22 | + * @test |
| 23 | + * @covers ::make |
| 24 | + * @dataProvider provideMakeData |
| 25 | + */ |
| 26 | + public function makeCache(string $name, array $config) |
| 27 | + { |
| 28 | + $this->assertInstanceOf($name, CacheFactory::make($name, $config)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @return array |
| 33 | + */ |
| 34 | + public function provideMakeData() : array |
| 35 | + { |
| 36 | + return [ |
| 37 | + 'null cache' => [ |
| 38 | + 'name' => NullCache::class, |
| 39 | + 'config' => [], |
| 40 | + ], |
| 41 | + 'in-memory cache' => [ |
| 42 | + 'name' => InMemoryCache::class, |
| 43 | + 'config' => [], |
| 44 | + ], |
| 45 | + 'mongo cache with collection' => [ |
| 46 | + 'name' => MongoCache::class, |
| 47 | + 'config' => [ |
| 48 | + 'collection' => $this->getMockBuilder(\MongoDB\Collection::class) |
| 49 | + ->disableOriginalConstructor() |
| 50 | + ->getMock(), |
| 51 | + ], |
| 52 | + ], |
| 53 | + 'mongo cache' => [ |
| 54 | + 'name' => MongoCache::class, |
| 55 | + 'config' => [ |
| 56 | + 'uri' => 'mongodb://localhost:27017', |
| 57 | + 'database' => 'testing', |
| 58 | + 'collection' => 'cache', |
| 59 | + ], |
| 60 | + ], |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @test |
| 66 | + * @covers ::make |
| 67 | + * @expectedException \RuntimeException |
| 68 | + * @expectedExceptionMessage Cannot create cache instance of 'Invalid' |
| 69 | + */ |
| 70 | + public function cannotMakeUnsupportedCacheInstance() |
| 71 | + { |
| 72 | + CacheFactory::make('Invalid', []); |
| 73 | + } |
| 74 | +} |
0 commit comments