From 02007e957d5ae77f3dcd4456332718d389d62d77 Mon Sep 17 00:00:00 2001 From: Mikhail Shamin Date: Tue, 21 Jul 2015 17:13:40 +0700 Subject: [PATCH] Fix extractor view data caching --- Extractor/CachingApiDocExtractor.php | 44 +++++++++- Tests/Extractor/ApiDocExtractorTest.php | 4 +- .../Extractor/CachingApiDocExtractorTest.php | 84 +++++++++++++++++++ 3 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 Tests/Extractor/CachingApiDocExtractorTest.php diff --git a/Extractor/CachingApiDocExtractor.php b/Extractor/CachingApiDocExtractor.php index a87d38e..d3ecae2 100644 --- a/Extractor/CachingApiDocExtractor.php +++ b/Extractor/CachingApiDocExtractor.php @@ -32,8 +32,27 @@ class CachingApiDocExtractor extends ApiDocExtractor */ protected $cache; + /** + * @var string + */ protected $cacheFile; + /** + * @var bool + */ + protected $debug; + + /** + * @param ContainerInterface $container + * @param RouterInterface $router + * @param Reader $reader + * @param DocCommentExtractor $commentExtractor + * @param ControllerNameParser $controllerNameParser + * @param array $handlers + * @param array $annotationsProviders + * @param string $cacheFile + * @param bool|false $debug + */ public function __construct( ContainerInterface $container, RouterInterface $router, @@ -47,12 +66,18 @@ class CachingApiDocExtractor extends ApiDocExtractor ) { parent::__construct($container, $router, $reader, $commentExtractor, $controllerNameParser, $handlers, $annotationsProviders); $this->cacheFile = $cacheFile; - $this->cache = new ConfigCache($this->cacheFile, $debug); + $this->debug = $debug; } + /** + * @param string $view View name + * @return array|mixed + */ public function all($view = ApiDoc::DEFAULT_VIEW) { - if ($this->cache->isFresh() === false) { + $cache = $this->getViewCache($view); + + if ($cache->isFresh() === false) { $resources = array(); @@ -67,12 +92,23 @@ class CachingApiDocExtractor extends ApiDocExtractor $resources = array_merge($resources, $this->router->getRouteCollection()->getResources()); $data = parent::all($view); - $this->cache->write(serialize($data), $resources); + + $cache->write(serialize($data), $resources); return $data; } - return unserialize(file_get_contents($this->cacheFile)); + return unserialize(file_get_contents($cache)); } + + /** + * @param string $view + * @return ConfigCache + */ + protected function getViewCache($view) + { + return new ConfigCache($this->cacheFile.'.'.$view, $this->debug); + } + } diff --git a/Tests/Extractor/ApiDocExtractorTest.php b/Tests/Extractor/ApiDocExtractorTest.php index 7be64ad..3f80e45 100644 --- a/Tests/Extractor/ApiDocExtractorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -47,9 +47,9 @@ class ApiDocExtractorTest extends WebTestCase $this->assertTrue(is_array($data)); $this->assertCount(self::$ROUTES_QUANTITY_DEFAULT, $data); - $cacheFile = $container->getParameter('kernel.cache_dir') . '/api-doc.cache'; + $cacheFile = $container->getParameter('kernel.cache_dir') . '/api-doc.cache.' . ApiDoc::DEFAULT_VIEW; $this->assertFileExists($cacheFile); - $this->assertEquals(file_get_contents($cacheFile), serialize($data)); + $this->assertStringEqualsFile($cacheFile, serialize($data)); foreach ($data as $key => $d) { $this->assertTrue(is_array($d)); diff --git a/Tests/Extractor/CachingApiDocExtractorTest.php b/Tests/Extractor/CachingApiDocExtractorTest.php new file mode 100644 index 0000000..4a918b8 --- /dev/null +++ b/Tests/Extractor/CachingApiDocExtractorTest.php @@ -0,0 +1,84 @@ +getContainer(); + /* @var CachingApiDocExtractor $extractor */ + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $this->assertInstanceOf('\Nelmio\ApiDocBundle\Extractor\CachingApiDocExtractor', $extractor); + + set_error_handler(array($this, 'handleDeprecation')); + $defaultData = $extractor->all(ApiDoc::DEFAULT_VIEW); + $data = $extractor->all($view); + restore_error_handler(); + + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $data); + $this->assertNotSameSize($defaultData, $data); + $this->assertNotEquals($defaultData, $data); + + $cacheFile = $container->getParameter('kernel.cache_dir').'/api-doc.cache'; + + $expectedDefaultViewCacheFile = $cacheFile.'.'.ApiDoc::DEFAULT_VIEW; + $expectedViewCacheFile = $cacheFile.'.'.$view; + + $this->assertFileExists($expectedDefaultViewCacheFile); + $this->assertFileExists($expectedViewCacheFile); + $this->assertFileNotEquals($expectedDefaultViewCacheFile, $expectedViewCacheFile); + } + + /** + * @dataProvider \Nelmio\ApiDocBundle\Tests\Extractor\ApiDocExtractorTest::dataProviderForViews + * @param string $view View name to test + */ + public function testCachedResultSameAsGenerated($view) + { + $container = $this->getContainer(); + /* @var CachingApiDocExtractor $extractor */ + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $this->assertInstanceOf('\Nelmio\ApiDocBundle\Extractor\CachingApiDocExtractor', $extractor); + + $cacheFile = $container->getParameter('kernel.cache_dir').'/api-doc.cache'; + + $expectedViewCacheFile = $cacheFile.'.'.$view; + + $this->assertFileNotExists($expectedViewCacheFile); + + set_error_handler(array($this, 'handleDeprecation')); + $data = $extractor->all($view); + + $this->assertFileExists($expectedViewCacheFile); + + $cachedData = $extractor->all($view); + restore_error_handler(); + + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $data); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $cachedData); + $this->assertSameSize($data, $cachedData); + $this->assertEquals($data, $cachedData); + } +}