You are here

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

Expanded class hierarchy of SwaggerUiLibraryDiscoveryTest

File

tests/src/Unit/SwaggerUiLibraryDiscoveryTest.php, line 39

Namespace

Drupal\Tests\swagger_ui_formatter\Unit
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SwaggerUiLibraryDiscoveryTest::$cache private property The mocked default cache bin.
SwaggerUiLibraryDiscoveryTest::$swaggerUiLibraryDiscovery private property The Swagger UI library discovery service.
SwaggerUiLibraryDiscoveryTest::$themeHandler private property The mocked theme handler service.
SwaggerUiLibraryDiscoveryTest::$themeInitialization private property The mocked theme initialization service.
SwaggerUiLibraryDiscoveryTest::$themeManager private property The mocked theme manager service.
SwaggerUiLibraryDiscoveryTest::DEFAULT_LIBRARY_DIR private constant The default Swagger UI library directory path.
SwaggerUiLibraryDiscoveryTest::setUp protected function Overrides UnitTestCase::setUp
SwaggerUiLibraryDiscoveryTest::setUpLibraryDirectoryTest private function Setup method for SwaggerUiLibraryDiscovery::libraryDirectory() tests.
SwaggerUiLibraryDiscoveryTest::setUpLibraryVersionTest private function Setup method for SwaggerUiLibraryDiscovery::libraryVersion() tests.
SwaggerUiLibraryDiscoveryTest::testLibraryDirectoryWithWarmCache public function Tests library directory with warm cache.
SwaggerUiLibraryDiscoveryTest::testWithInvalidLibraryDirectory public function Tests with invalid library directory.
SwaggerUiLibraryDiscoveryTest::testWithMalformedPackageJson public function Tests with malformed package.json.
SwaggerUiLibraryDiscoveryTest::testWithMissingPackageJson public function Tests with missing package.json.
SwaggerUiLibraryDiscoveryTest::testWithMissingRequiredLibraryFile public function Tests with missing required library file.
SwaggerUiLibraryDiscoveryTest::testWithMissingVersionInPackageJson public function Tests with missing version in package.json.
SwaggerUiLibraryDiscoveryTest::testWithUnsupportedPackageJson public function Tests with unsupported package.json.
SwaggerUiLibraryDiscoveryTest::testWithValidLibraryDirectoryColdCache public function Tests valid library directory with cold cache.
SwaggerUiLibraryDiscoveryTest::testWithValidLibraryVersion public function Tests with valid library version.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.