final class SwaggerUiLibraryDiscoveryTest in Swagger UI Field Formatter 8.3
Tests the Swagger UI library discovery service.
@covers \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscovery
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\swagger_ui_formatter\Unit\SwaggerUiLibraryDiscoveryTest
Expanded class hierarchy of SwaggerUiLibraryDiscoveryTest
File
- tests/
src/ Unit/ SwaggerUiLibraryDiscoveryTest.php, line 39
Namespace
Drupal\Tests\swagger_ui_formatter\UnitView source
final class SwaggerUiLibraryDiscoveryTest extends UnitTestCase {
/**
* The default Swagger UI library directory path.
*/
private const DEFAULT_LIBRARY_DIR = 'libraries/swagger-ui';
/**
* The mocked default cache bin.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $cache;
/**
* The mocked theme handler service.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $themeHandler;
/**
* The mocked theme manager service.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $themeManager;
/**
* The mocked theme initialization service.
*
* @var \Drupal\Core\Theme\ThemeInitializationInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $themeInitialization;
/**
* The Swagger UI library discovery service.
*
* @var \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscovery
*/
private $swaggerUiLibraryDiscovery;
/**
* {@inheritdoc}
*/
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);
}
/**
* Tests valid library directory with cold cache.
*/
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);
// Here, we don't care about whether the data has been cached or not.
$this->cache
->expects($this
->once())
->method('set')
->willReturn(NULL);
self::assertEquals(self::DEFAULT_LIBRARY_DIR, $this->swaggerUiLibraryDiscovery
->libraryDirectory());
}
/**
* Tests library directory with warm cache.
*/
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());
}
/**
* Tests with invalid library directory.
*/
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();
}
/**
* Tests with missing required library file.
*/
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();
}
/**
* Tests with valid library version.
*/
public function testWithValidLibraryVersion() : void {
$this
->setUpLibraryVersionTest();
// Imitate that SwaggerUiLibraryDiscovery::libraryDirectory() responds
// from cache to avoid mocking or different services.
$this->cache
->method('get')
->willReturn((object) [
'data' => self::DEFAULT_LIBRARY_DIR,
]);
self::assertEquals(SWAGGER_UI_FORMATTER_TEST_VALID_LIBRARY_VERSION, $this->swaggerUiLibraryDiscovery
->libraryVersion());
}
/**
* Tests with missing package.json.
*/
public function testWithMissingPackageJson() : void {
$this
->setUpLibraryVersionTest();
// Imitate a wrong package.json path with a specific library directory.
$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();
}
/**
* Tests with malformed package.json.
*/
public function testWithMalformedPackageJson() : void {
$this
->setUpLibraryVersionTest();
// Imitate an invalid package.json with a specific library directory.
$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();
}
/**
* Tests with missing version in package.json.
*/
public function testWithMissingVersionInPackageJson() : void {
$this
->setUpLibraryVersionTest();
// Imitate that the "version" attribute is not found in package.json with
// a specific library directory.
$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();
}
/**
* Tests with unsupported package.json.
*/
public function testWithUnsupportedPackageJson() : void {
$this
->setUpLibraryVersionTest();
// Imitate that the Swagger UI library "version" from package.json is not
// supported.
$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();
}
/**
* Setup method for SwaggerUiLibraryDiscovery::libraryDirectory() tests.
*/
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);
}
/**
* Setup method for SwaggerUiLibraryDiscovery::libraryVersion() tests.
*/
private function setUpLibraryVersionTest() : void {
$this->themeHandler
->expects($this
->never())
->method('getDefault');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SwaggerUiLibraryDiscoveryTest:: |
private | property | The mocked default cache bin. | |
SwaggerUiLibraryDiscoveryTest:: |
private | property | The Swagger UI library discovery service. | |
SwaggerUiLibraryDiscoveryTest:: |
private | property | The mocked theme handler service. | |
SwaggerUiLibraryDiscoveryTest:: |
private | property | The mocked theme initialization service. | |
SwaggerUiLibraryDiscoveryTest:: |
private | property | The mocked theme manager service. | |
SwaggerUiLibraryDiscoveryTest:: |
private | constant | The default Swagger UI library directory path. | |
SwaggerUiLibraryDiscoveryTest:: |
protected | function |
Overrides UnitTestCase:: |
|
SwaggerUiLibraryDiscoveryTest:: |
private | function | Setup method for SwaggerUiLibraryDiscovery::libraryDirectory() tests. | |
SwaggerUiLibraryDiscoveryTest:: |
private | function | Setup method for SwaggerUiLibraryDiscovery::libraryVersion() tests. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests library directory with warm cache. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with invalid library directory. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with malformed package.json. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with missing package.json. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with missing required library file. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with missing version in package.json. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with unsupported package.json. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests valid library directory with cold cache. | |
SwaggerUiLibraryDiscoveryTest:: |
public | function | Tests with valid library version. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |