diff --git a/mu-plugins/central-hub/tests/phpunit/integration/config-store/loadConfigFromFilesystem.php b/mu-plugins/central-hub/tests/phpunit/integration/config-store/loadConfigFromFilesystem.php new file mode 100644 index 00000000..905fb559 --- /dev/null +++ b/mu-plugins/central-hub/tests/phpunit/integration/config-store/loadConfigFromFilesystem.php @@ -0,0 +1,110 @@ + 37, + 'eee' => 'Coding is fun!', + ]; + $merged_config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + 'eee' => 'Coding is fun!', + ]; + + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file, $defaults ) ); + $this->assertEquals( $merged_config, _the_store( 'foo' ) ); + } + + /** + * Test loadConfigFromFilesystem() should store a config and return the store key. + */ + public function test_should_store_a_config_and_return_store_key() { + $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/test-cpt-config.php'; + $config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + ]; + + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); + $this->assertSame( $config, _the_store( 'foo' ) ); + } + + /** + * Test loadConfigFromFilesystem() should overwrite stored configuration and return store key from + * file configuration. + */ + public function test_should_overwrite_store_and_return_store_key_from_file_config() { + $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/test-cpt-config.php'; + $config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + ]; + + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); + $this->assertSame( $config, _the_store( 'foo' ) ); + } + + /** + * Test loadConfigFromFilesystem() should overwrite stored configuration and return store key from + * file configuration. + */ + public function test_should_throw_exception_when_no_store_key() { + $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/config-with-params-only.php'; + $this->expectException( \Exception::class ); + $this->expectExceptionMessage( + sprintf( 'No store key exists in the %s configuration file.', $path_to_file ) + ); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_merge_with_defaults' )->never(); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_the_store' )->never(); + + loadConfigFromFilesystem( $path_to_file ); + } + + /** + * Test loadConfigFromFilesystem() should throw an Exception when the configuration parameters are empty. + */ + public function test_should_throw_exception_when_config_params_empty() { + $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/config-with-store-key-only.php'; + $this->expectException( \Exception::class ); + $this->expectExceptionMessage( + sprintf( + 'No configuration parameters exist for store key [foo] in the %s configuration file.', + $path_to_file + ) + ); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_merge_with_defaults' )->never(); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_the_store' )->never(); + + loadConfigFromFilesystem( $path_to_file ); + } +} diff --git a/mu-plugins/central-hub/tests/phpunit/unit/config-store/_loadConfigFromFilesystem.php b/mu-plugins/central-hub/tests/phpunit/unit/config-store/_loadConfigFromFilesystem.php index cd1b8acd..681055ba 100644 --- a/mu-plugins/central-hub/tests/phpunit/unit/config-store/_loadConfigFromFilesystem.php +++ b/mu-plugins/central-hub/tests/phpunit/unit/config-store/_loadConfigFromFilesystem.php @@ -16,12 +16,12 @@ use spiralWebDb\Cornerstone\Tests\Unit\Test_Case; /** - * Class Tests_LoadConfigFromFilesystem + * Class Tests__LoadConfigFromFilesystem * * @package spiralWebDb\centralHub\Tests\Unit\ConfigStore * @group config-store */ -class Tests_LoadConfigFromFilesystem extends Test_Case { +class Tests__LoadConfigFromFilesystem extends Test_Case { /** * Prepares the test environment before each test. diff --git a/mu-plugins/central-hub/tests/phpunit/unit/config-store/loadConfigFromFilesystem.php b/mu-plugins/central-hub/tests/phpunit/unit/config-store/loadConfigFromFilesystem.php new file mode 100644 index 00000000..32507fa1 --- /dev/null +++ b/mu-plugins/central-hub/tests/phpunit/unit/config-store/loadConfigFromFilesystem.php @@ -0,0 +1,114 @@ + 37, + 'eee' => 'Coding is fun!', + ]; + $config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + ]; + $merged_config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + 'eee' => 'Coding is fun!', + ]; + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_load_config_from_filesystem' ) + ->once() + ->with( $path_to_file ) + ->andReturn( [ 'foo', $config ] ); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_merge_with_defaults' ) + ->once() + ->with( $config, $defaults ) + ->andReturn( $merged_config ); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_the_store' ) + ->once() + ->with( 'foo', $merged_config ) + ->andReturn( true ); + + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file, $defaults ) ); + } + + /** + * Test loadConfigFromFilesystem() should store a config and return the store key. + */ + public function test_should_store_a_config_and_return_store_key() { + $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/test-cpt-config.php'; + $config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + ]; + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_load_config_from_filesystem' ) + ->once() + ->with( $path_to_file ) + ->andReturn( [ 'foo', $config ] ); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_merge_with_defaults' )->never(); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_the_store' ) + ->once() + ->with( 'foo', $config ) + ->andReturn( true ); + + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); + } + + /** + * Test loadConfigFromFilesystem() should overwrite stored configuration and return store key from + * file configuration. + */ + public function test_should_overwrite_store_and_return_store_key_from_file_config() { + $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/test-cpt-config.php'; + $config = [ + 'aaa' => 'bbb', + 'ccc' => 'ddd', + ]; + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_load_config_from_filesystem' ) + ->twice() + ->with( $path_to_file ) + ->andReturn( [ 'foo', $config ] ); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_merge_with_defaults' )->never(); + Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_the_store' ) + ->twice() + ->with( 'foo', $config ) + ->andReturn( true ); + + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); + $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); + } +}