diff --git a/Tests/Fixtures/VirtualFilesystemTestTrait/getDefaultVfs.php b/Tests/Fixtures/VirtualFilesystemTestTrait/getDefaultVfs.php new file mode 100644 index 0000000..8b5b4c7 --- /dev/null +++ b/Tests/Fixtures/VirtualFilesystemTestTrait/getDefaultVfs.php @@ -0,0 +1,23 @@ + [ + [ + 'wp-admin' => [], + 'wp-content' => [ + 'mu-plugins' => [], + 'plugins' => [ + 'wp-rocket' => [], + ], + 'themes' => [ + 'twentytwenty' => [], + ], + 'uploads' => [], + ], + 'wp-includes' => [], + 'wp-config.php' => '', + ], + ], +]; diff --git a/Tests/Unit/VirtualFilesystemDirect/TestCase.php b/Tests/Unit/VirtualFilesystemDirect/TestCase.php index c7ba32d..2d2da60 100644 --- a/Tests/Unit/VirtualFilesystemDirect/TestCase.php +++ b/Tests/Unit/VirtualFilesystemDirect/TestCase.php @@ -1,23 +1,65 @@ init(); } + /** + * Gets the path to the Fixtures directory. + * + * @return string + */ public function getPathToFixturesDir() { return WPMEDIA_PHPUNIT_ROOT_DIR . '/Tests/Fixtures/'; } + /** + * Loads the test data matching the given file from the Fixtures directory. + * + * @param string $file Path or filename of the test class requesting the data. + * + * @return array test data. + */ protected function loadTestData( $file ) { return $this->getTestData( WPMEDIA_PHPUNIT_ROOT_DIR . '/Tests/Fixtures/', basename( $file, '.php' ) ); } + + /** + * Overrides the package's WP-like default with the `Tests/{Integration,Unit}` structure + * that the fixtures in this suite (see {@see Test_GetListing}, {@see Test_GetDirsListing}, + * {@see Test_GetFilesListing}) assert against as the full, exact listing of the virtual + * filesystem root. + * + * @return array default structure. + */ + public function getDefaultVfs() { + return [ + 'Tests' => [ + 'Integration' => [], + 'Unit' => [], + ], + ]; + } } diff --git a/Tests/Unit/VirtualFilesystemTestTrait/TestCase.php b/Tests/Unit/VirtualFilesystemTestTrait/TestCase.php index f2c56fd..bb7e6bc 100644 --- a/Tests/Unit/VirtualFilesystemTestTrait/TestCase.php +++ b/Tests/Unit/VirtualFilesystemTestTrait/TestCase.php @@ -34,18 +34,4 @@ abstract class TestCase extends BaseTestCase { public function getPathToFixturesDir() { return WPMEDIA_PHPUNIT_ROOT_DIR . '/Tests/Fixtures/'; } - - /** - * Gets the default virtual directory filesystem structure. - * - * @return array default structure. - */ - public function getDefaultVfs() { - return [ - 'Tests' => [ - 'Integration' => [], - 'Unit' => [], - ], - ]; - } } diff --git a/Tests/Unit/VirtualFilesystemTestTrait/getDefaultVfs.php b/Tests/Unit/VirtualFilesystemTestTrait/getDefaultVfs.php new file mode 100644 index 0000000..81b6bd5 --- /dev/null +++ b/Tests/Unit/VirtualFilesystemTestTrait/getDefaultVfs.php @@ -0,0 +1,36 @@ +assertSame( $expected, $this->getDefaultVfs() ); + } + + /** + * Provides the expected default structure from the Fixtures directory. + * + * @return array test data. + */ + public function getDefaultVfsDataProvider() { + return $this->getTestData( __DIR__, 'getDefaultVfs' ); + } +} diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 9cc0e68..26c2a16 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -12,6 +12,10 @@ codebase is fully compliant. --> src/Integration/HttpRequestTrait.php + src/VirtualFilesystemTestTrait.php + Tests/Unit/VirtualFilesystemDirect/TestCase.php + Tests/Unit/VirtualFilesystemTestTrait/getDefaultVfs.php + Tests/Fixtures/VirtualFilesystemTestTrait/getDefaultVfs.php vendor/* diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 53298e5..2a37334 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -178,13 +178,13 @@ parameters: rawMessage: 'Call to function is_array() with array will always evaluate to true.' identifier: function.alreadyNarrowedType count: 1 - path: src/Unit/VirtualFilesystemTestCase.php + path: src/ArrayTrait.php - rawMessage: 'Call to function is_null() with string will always evaluate to false.' identifier: function.impossibleType count: 2 - path: src/Unit/VirtualFilesystemTestCase.php + path: src/ArrayTrait.php - rawMessage: Constant WPMEDIA_PHPUNIT_ROOT_DIR not found. diff --git a/src/Integration/VirtualFilesystemTestCase.php b/src/Integration/VirtualFilesystemTestCase.php index 508d8b2..679a04f 100644 --- a/src/Integration/VirtualFilesystemTestCase.php +++ b/src/Integration/VirtualFilesystemTestCase.php @@ -36,18 +36,4 @@ abstract class VirtualFilesystemTestCase extends TestCase { * @var int */ protected $permissions = 0777; - - /** - * Gets the default virtual directory filesystem structure. - * - * @return array default structure. - */ - public function getDefaultVfs() { - return [ - 'Tests' => [ - 'Integration' => [], - 'Unit' => [], - ], - ]; - } } diff --git a/src/Unit/VirtualFilesystemTestCase.php b/src/Unit/VirtualFilesystemTestCase.php index 6906cdd..dcc8160 100644 --- a/src/Unit/VirtualFilesystemTestCase.php +++ b/src/Unit/VirtualFilesystemTestCase.php @@ -28,18 +28,4 @@ abstract class VirtualFilesystemTestCase extends TestCase { * @var int */ protected $permissions = 0777; - - /** - * Gets the default virtual directory filesystem structure. - * - * @return array default structure. - */ - public function getDefaultVfs() { - return [ - 'Tests' => [ - 'Integration' => [], - 'Unit' => [], - ], - ]; - } } diff --git a/src/VirtualFilesystemTestTrait.php b/src/VirtualFilesystemTestTrait.php index 10a2fea..1fd864b 100644 --- a/src/VirtualFilesystemTestTrait.php +++ b/src/VirtualFilesystemTestTrait.php @@ -1,5 +1,7 @@ initOriginals(); + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Public API property; renaming would be a breaking change for consumers. $this->filesystem = new VirtualFilesystemDirect( $this->rootVirtualDir, $this->mergeStructure(), $this->permissions ); - $this->rootVirtualUrl = $this->filesystem->getUrl( '/' ); + $this->rootVirtualUrl = $this->filesystem->getUrl( '/' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Public API property; renaming would be a breaking change for consumers. } /** @@ -80,7 +83,7 @@ public function init() { * * @return mixed */ - public function providerTestData() { + public function providerTestData() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Public API method; renaming would be a breaking change for consumers. $this->loadConfig(); return $this->config['test_data']; @@ -89,7 +92,7 @@ public function providerTestData() { /** * Loads the configuration for the vfs structure and test data. */ - protected function loadConfig() { + protected function loadConfig() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Public API method; renaming would be a breaking change for consumers. $this->config = array_merge( [ 'vfs_dir' => '', @@ -105,7 +108,7 @@ protected function loadConfig() { * * @return string */ - public function getPathToFixturesDir() { + public function getPathToFixturesDir() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Public API method; renaming would be a breaking change for consumers. return ''; } @@ -114,7 +117,7 @@ public function getPathToFixturesDir() { * * @return array merged structure */ - protected function mergeStructure() { + protected function mergeStructure() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Public API method; renaming would be a breaking change for consumers. // If already merged, return it. if ( ! empty( $this->merged_structure ) ) { return $this->merged_structure; @@ -129,9 +132,14 @@ protected function mergeStructure() { /** * Gets the default virtual directory filesystem structure. * + * This is the single source of truth for the default structure used by + * {@see mergeStructure()}. Consumers that need a different default structure + * should override this method rather than defining a competing default + * elsewhere in the class hierarchy. + * * @return array default structure. */ - public function getDefaultVfs() { + public function getDefaultVfs() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Public API method and the package's documented single override point (issue #36); renaming would be a breaking change for consumers. return [ 'wp-admin' => [], 'wp-content' => [ @@ -152,16 +160,16 @@ public function getDefaultVfs() { /** * Initializes the original files and directories properties for use in the tests. */ - protected function initOriginals() { + protected function initOriginals() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Public API method; renaming would be a breaking change for consumers. // Bail out when "skip_initOriginals" is set to true. - if ( $this->skip_initOriginals ) { + if ( $this->skip_initOriginals ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Public API property; renaming would be a breaking change for consumers. return; } if ( ! empty( $this->config['vfs_dir'] ) && '/' !== $this->config['vfs_dir'] ) { - $vfs_dir = rtrim( $this->config['vfs_dir'], '/\\' ); // Remove trailing slash for the get. - $structure = $this->get( $this->config['structure'], $vfs_dir, [], '/' ); - $vfs_dir .= '/'; // Add the trailing slash for the flattening. + $vfs_dir = rtrim( $this->config['vfs_dir'], '/\\' ); // Remove trailing slash for the get. + $structure = $this->get( $this->config['structure'], $vfs_dir, [], '/' ); + $vfs_dir .= '/'; // Add the trailing slash for the flattening. } else { $vfs_dir = ''; $structure = $this->config['structure'];