View source
<?php
declare (strict_types=1);
namespace {
const SWAGGER_UI_FORMATTER_TEST_INVALID_LIBRARY_DIR = 'libraries/invalid';
const SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_DIR_WITH_MISSING_FILES = 'libraries/valid-with-missing-files';
const SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_VERSION = '3.35.2';
const SWAGGER_UI_FORMATTER_TEST_MISSING_PACKAGE_JSON_DIR = 'libraries/missing-package-json';
const SWAGGER_UI_FORMATTER_TEST_INVALID_PACKAGE_JSON_DIR = 'libraries/invalid-package-json';
const SWAGGER_UI_FORMATTER_TEST_VERSION_NOT_FOUND_IN_PACKAGE_JSON_DIR = 'libraries/version-not-found-in-package-json';
const SWAGGER_UI_FORMATTER_TEST_VERSION_IS_NOT_SUPPORTED_IN_PACKAGE_JSON_DIR = 'libraries/version-is-not-supported-in-package-json';
}
namespace Drupal\Tests\swagger_ui_formatter\Unit {
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\ThemeInitializationInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryException;
use Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscovery;
final class SwaggerUiLibraryDiscoveryTest extends UnitTestCase {
private const DEFAULT_LIBRARY_DIR = 'libraries/swagger-ui';
private $cache;
private $themeHandler;
private $themeManager;
private $themeInitialization;
private $swaggerUiLibraryDiscovery;
protected function setUp() : void {
parent::setUp();
$this->cache = $this
->createMock(CacheBackendInterface::class);
$this->themeHandler = $this
->createMock(ThemeHandlerInterface::class);
$this->themeManager = $this
->createMock(ThemeManagerInterface::class);
$this->themeInitialization = $this
->createMock(ThemeInitializationInterface::class);
$this->swaggerUiLibraryDiscovery = new SwaggerUiLibraryDiscovery($this->cache, $this->themeHandler, $this->themeManager, $this->themeInitialization);
}
public function testWithValidLibraryDirectoryColdCache() : void {
$this
->setUpLibraryDirectoryTest();
$default_theme = new ActiveTheme([
'name' => 'bartik',
]);
$this->themeManager
->expects($this
->once())
->method('alterForTheme')
->with($default_theme, 'swagger_ui_library_directory', self::DEFAULT_LIBRARY_DIR)
->willReturn(NULL);
$this->cache
->expects($this
->once())
->method('set')
->willReturn(NULL);
self::assertEquals(self::DEFAULT_LIBRARY_DIR, $this->swaggerUiLibraryDiscovery
->libraryDirectory());
}
public function testLibraryDirectoryWithWarmCache() : void {
$this->cache
->method('get')
->willReturn((object) [
'data' => self::DEFAULT_LIBRARY_DIR,
]);
$this->themeHandler
->expects($this
->never())
->method('getDefault');
self::assertEquals(self::DEFAULT_LIBRARY_DIR, $this->swaggerUiLibraryDiscovery
->libraryDirectory());
}
public function testWithInvalidLibraryDirectory() : void {
$this
->setUpLibraryDirectoryTest();
$default_theme = new ActiveTheme([
'name' => 'bartik',
]);
$this->themeManager
->expects($this
->once())
->method('alterForTheme')
->with($default_theme, 'swagger_ui_library_directory', self::DEFAULT_LIBRARY_DIR)
->willReturnCallback(static function ($default_theme, $hook, &$library_dir) {
$library_dir = SWAGGER_UI_FORMATTER_TEST_INVALID_LIBRARY_DIR;
return NULL;
});
$this
->expectException(SwaggerUiLibraryDiscoveryException::class);
$this
->expectExceptionCode(SwaggerUiLibraryDiscoveryException::CODE_INVALID_DIR);
$this->swaggerUiLibraryDiscovery
->libraryDirectory();
}
public function testWithMissingRequiredLibraryFile() : void {
$this
->setUpLibraryDirectoryTest();
$default_theme = new ActiveTheme([
'name' => 'bartik',
]);
$this->themeManager
->expects($this
->once())
->method('alterForTheme')
->with($default_theme, 'swagger_ui_library_directory', self::DEFAULT_LIBRARY_DIR)
->willReturnCallback(static function ($default_theme, $hook, &$library_dir) {
$library_dir = SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_DIR_WITH_MISSING_FILES;
return NULL;
});
$this
->expectException(SwaggerUiLibraryDiscoveryException::class);
$this
->expectExceptionCode(SwaggerUiLibraryDiscoveryException::CODE_REQUIRED_FILE_IS_NOT_FOUND);
$this->swaggerUiLibraryDiscovery
->libraryDirectory();
}
public function testWithValidLibraryVersion() : void {
$this
->setUpLibraryVersionTest();
$this->cache
->method('get')
->willReturn((object) [
'data' => self::DEFAULT_LIBRARY_DIR,
]);
self::assertEquals(SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_VERSION, $this->swaggerUiLibraryDiscovery
->libraryVersion());
}
public function testWithMissingPackageJson() : void {
$this
->setUpLibraryVersionTest();
$this->cache
->method('get')
->willReturn((object) [
'data' => SWAGGER_UI_FORMATTER_TEST_MISSING_PACKAGE_JSON_DIR,
]);
$this
->expectException(SwaggerUiLibraryDiscoveryException::class);
$this
->expectExceptionCode(SwaggerUiLibraryDiscoveryException::CODE_CANNOT_READ_PACKAGE_JSON_CONTENT);
$this->swaggerUiLibraryDiscovery
->libraryVersion();
}
public function testWithMalformedPackageJson() : void {
$this
->setUpLibraryVersionTest();
$this->cache
->method('get')
->willReturn((object) [
'data' => SWAGGER_UI_FORMATTER_TEST_INVALID_PACKAGE_JSON_DIR,
]);
$this
->expectException(SwaggerUiLibraryDiscoveryException::class);
$this
->expectExceptionCode(SwaggerUiLibraryDiscoveryException::CODE_CANNOT_DECODE_PACKAGE_JSON);
$this->swaggerUiLibraryDiscovery
->libraryVersion();
}
public function testWithMissingVersionInPackageJson() : void {
$this
->setUpLibraryVersionTest();
$this->cache
->method('get')
->willReturn((object) [
'data' => SWAGGER_UI_FORMATTER_TEST_VERSION_NOT_FOUND_IN_PACKAGE_JSON_DIR,
]);
$this
->expectException(SwaggerUiLibraryDiscoveryException::class);
$this
->expectExceptionCode(SwaggerUiLibraryDiscoveryException::CODE_UNABLE_TO_IDENTIFY_LIBRARY_VERSION);
$this->swaggerUiLibraryDiscovery
->libraryVersion();
}
public function testWithUnsupportedPackageJson() : void {
$this
->setUpLibraryVersionTest();
$this->cache
->method('get')
->willReturn((object) [
'data' => SWAGGER_UI_FORMATTER_TEST_VERSION_IS_NOT_SUPPORTED_IN_PACKAGE_JSON_DIR,
]);
$this
->expectException(SwaggerUiLibraryDiscoveryException::class);
$this
->expectExceptionCode(SwaggerUiLibraryDiscoveryException::CODE_LIBRARY_VERSION_IS_NOT_SUPPORTED);
$this->swaggerUiLibraryDiscovery
->libraryVersion();
}
private function setUpLibraryDirectoryTest() : void {
$this->cache
->method('get')
->willReturn(NULL);
$this->themeHandler
->method('getDefault')
->willReturn('bartik');
$default_theme = new ActiveTheme([
'name' => 'bartik',
]);
$this->themeInitialization
->method('getActiveThemeByName')
->with($this->themeHandler
->getDefault())
->willReturn($default_theme);
$this->themeInitialization
->method('loadActiveTheme')
->with($default_theme)
->willReturn(NULL);
}
private function setUpLibraryVersionTest() : void {
$this->themeHandler
->expects($this
->never())
->method('getDefault');
}
}
}
namespace Drupal\swagger_ui_formatter\Service {
const DRUPAL_ROOT = '';
function file_exists(string $filename) : bool {
if ($filename === '/' . SWAGGER_UI_FORMATTER_TEST_INVALID_LIBRARY_DIR) {
return FALSE;
}
if ($filename === '/' . SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_DIR_WITH_MISSING_FILES) {
return TRUE;
}
return !preg_match('#^/' . SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_DIR_WITH_MISSING_FILES . '#', $filename);
}
function file_get_contents($filename) {
if ($filename === '/' . SWAGGER_UI_FORMATTER_TEST_MISSING_PACKAGE_JSON_DIR . '/package.json') {
return FALSE;
}
if ($filename === '/' . SWAGGER_UI_FORMATTER_TEST_INVALID_PACKAGE_JSON_DIR . '/package.json') {
return '{ invalid json }';
}
if ($filename === '/' . SWAGGER_UI_FORMATTER_TEST_VERSION_NOT_FOUND_IN_PACKAGE_JSON_DIR . '/package.json') {
return '{ "foo": "bar" }';
}
if ($filename === '/' . SWAGGER_UI_FORMATTER_TEST_VERSION_IS_NOT_SUPPORTED_IN_PACKAGE_JSON_DIR . '/package.json') {
return '{ "version": "3.32.1" }';
}
return '{ "version": "' . SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_VERSION . '" }';
}
}