NelmioApiDocBundle/Tests/Extractor/CachingApiDocExtractorTest.php

95 lines
3.1 KiB
PHP
Raw Normal View History

2015-07-21 17:13:40 +07:00
<?php
2015-11-10 01:27:09 +01:00
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2015-07-21 17:13:40 +07:00
namespace Nelmio\ApiDocBundle\Tests\Extractor;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\Extractor\CachingApiDocExtractor;
use Nelmio\ApiDocBundle\Tests\WebTestCase;
class CachingApiDocExtractorTest extends WebTestCase
{
/**
* @return array
*/
public static function viewsWithoutDefaultProvider()
{
$data = ApiDocExtractorTest::dataProviderForViews();
// remove default view data from provider
array_shift($data);
2015-10-22 14:42:59 +02:00
2015-07-21 17:13:40 +07:00
return $data;
}
/**
* Test that every view cache is saved in its own cache file
*
* @dataProvider viewsWithoutDefaultProvider
2024-10-01 15:54:04 +03:00
*
2015-07-21 17:13:40 +07:00
* @param string $view View name
*/
2024-10-01 15:54:04 +03:00
public function testDifferentCacheFilesAreCreatedForDifferentViews($view): void
2015-07-21 17:13:40 +07:00
{
$container = $this->getContainer();
/* @var CachingApiDocExtractor $extractor */
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$this->assertInstanceOf('\Nelmio\ApiDocBundle\Extractor\CachingApiDocExtractor', $extractor);
2024-10-01 15:54:04 +03:00
set_error_handler([$this, 'handleDeprecation']);
2015-07-21 17:13:40 +07:00
$defaultData = $extractor->all(ApiDoc::DEFAULT_VIEW);
$data = $extractor->all($view);
restore_error_handler();
$this->assertIsArray($data);
2015-07-21 17:13:40 +07:00
$this->assertNotSameSize($defaultData, $data);
$this->assertNotEquals($defaultData, $data);
2024-10-01 15:54:04 +03:00
$cacheFile = $container->getParameter('kernel.cache_dir') . '/api-doc.cache';
2015-07-21 17:13:40 +07:00
2024-10-01 15:54:04 +03:00
$expectedDefaultViewCacheFile = $cacheFile . '.' . ApiDoc::DEFAULT_VIEW;
$expectedViewCacheFile = $cacheFile . '.' . $view;
2015-07-21 17:13:40 +07:00
$this->assertFileExists($expectedDefaultViewCacheFile);
$this->assertFileExists($expectedViewCacheFile);
$this->assertFileNotEquals($expectedDefaultViewCacheFile, $expectedViewCacheFile);
}
/**
* @dataProvider \Nelmio\ApiDocBundle\Tests\Extractor\ApiDocExtractorTest::dataProviderForViews
2024-10-01 15:54:04 +03:00
*
2015-07-21 17:13:40 +07:00
* @param string $view View name to test
*/
2024-10-01 15:54:04 +03:00
public function testCachedResultSameAsGenerated($view): void
2015-07-21 17:13:40 +07:00
{
$container = $this->getContainer();
/* @var CachingApiDocExtractor $extractor */
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$this->assertInstanceOf('\Nelmio\ApiDocBundle\Extractor\CachingApiDocExtractor', $extractor);
2024-10-01 15:54:04 +03:00
$cacheFile = $container->getParameter('kernel.cache_dir') . '/api-doc.cache';
2015-07-21 17:13:40 +07:00
2024-10-01 15:54:04 +03:00
$expectedViewCacheFile = $cacheFile . '.' . $view;
2015-07-21 17:13:40 +07:00
2024-10-01 15:54:04 +03:00
set_error_handler([$this, 'handleDeprecation']);
2015-07-21 17:13:40 +07:00
$data = $extractor->all($view);
$this->assertFileExists($expectedViewCacheFile);
$cachedData = $extractor->all($view);
restore_error_handler();
$this->assertIsArray($data);
$this->assertIsArray($cachedData);
2015-07-21 17:13:40 +07:00
$this->assertSameSize($data, $cachedData);
$this->assertEquals($data, $cachedData);
}
}