-
Notifications
You must be signed in to change notification settings - Fork 1
Add tests for Config Store API function: loadConfigFromFilesystem
#21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
61a4820
37a6e38
b548e39
40a3a9a
c21a0de
a730119
2bdee04
d7c4908
4b6786d
1fd17fe
b3870b3
86028e6
78c6fd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <?php | ||
| /** | ||
| * Tests for external API function loadConfigFromFilesystem() | ||
| * | ||
| * @package spiralWebDb\centralHub\Tests\Integration\ConfigStore | ||
| * @since 1.3.0 | ||
| * @author Robert A. Gadon | ||
| * @link https://github.com/rgadon107/cornerstone | ||
| * @license GNU General Public License 2.0+ | ||
| */ | ||
|
|
||
| namespace spiralWebDb\centralHub\Tests\Integration\ConfigStore; | ||
|
|
||
| use Brain\Monkey; | ||
| use function KnowTheCode\ConfigStore\_the_store; | ||
| use function KnowTheCode\ConfigStore\loadConfigFromFilesystem; | ||
| use spiralWebDb\Cornerstone\Tests\Integration\Test_Case; | ||
|
|
||
|
|
||
| /** | ||
| * Class Tests_LoadConfigFromFilesystem | ||
| * | ||
| * @package spiralWebDb\centralHub\Tests\Integration\ConfigStore | ||
| * @group config-store | ||
| */ | ||
| class Tests_LoadConfigFromFilesystem extends Test_Case { | ||
|
|
||
| /** | ||
| * Test loadConfigFromFilesystem() should merge defaults with config and return a store key. | ||
| */ | ||
| public function test_should_merge_defaults_and_return_store_key() { | ||
| $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/test-cpt-config.php'; | ||
| $defaults = [ | ||
| 'aaa' => 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 ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rgadon107 Notice how this integration test is constructed. What is it doing?
Writing a test in this way makes it very clear as to what behavior we expect and don't expect to happen. |
||
| } | ||
|
|
||
| /** | ||
| * 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 ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?php | ||
| /** | ||
| * Tests for external API function loadConfigFromFilesystem() | ||
| * | ||
| * @package spiralWebDb\centralHub\Tests\Unit\ConfigStore | ||
| * @since 1.3.0 | ||
| * @author Robert A. Gadon | ||
| * @link https://github.com/rgadon107/cornerstone | ||
| * @license GNU General Public License 2.0+ | ||
| */ | ||
|
|
||
| namespace spiralWebDb\centralHub\Tests\Unit\ConfigStore; | ||
|
|
||
| use Brain\Monkey; | ||
| use function KnowTheCode\ConfigStore\loadConfigFromFilesystem; | ||
| use spiralWebDb\Cornerstone\Tests\Unit\Test_Case; | ||
|
|
||
| /** | ||
| * Class Tests_LoadConfigFromFilesystem | ||
| * | ||
| * @package spiralWebDb\centralHub\Tests\Unit\ConfigStore | ||
| * @group config-store | ||
| */ | ||
| class Tests_LoadConfigFromFilesystem extends Test_Case { | ||
|
|
||
| /** | ||
| * Prepares the test environment before each test. | ||
| */ | ||
| protected function setUp() { | ||
| parent::setUp(); | ||
|
|
||
| require_once CENTRAL_HUB_ROOT_DIR . '/src/config-store/api.php'; | ||
| } | ||
|
|
||
| /** | ||
| * Test loadConfigFromFilesystem() should merge defaults with config and return a store key. | ||
| */ | ||
| public function test_should_merge_defaults_and_return_store_key() { | ||
| $path_to_file = CENTRAL_HUB_ROOT_DIR . '/tests/phpunit/fixtures/test-cpt-config.php'; | ||
| $defaults = [ | ||
| 'aaa' => 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 ) ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rgadon107 Look at each of the mocked internal function dependencies. We are checking:
When we invoke |
||
| } | ||
|
|
||
| /** | ||
| * 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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rgadon107 Notice that we are checking that this function does not get invoked. |
||
| Monkey\Functions\expect( '\KnowTheCode\ConfigStore\_the_store' ) | ||
| ->twice() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rgadon107 Notice that we are invoking this 2x. |
||
| ->with( 'foo', $config ) | ||
| ->andReturn( true ); | ||
|
|
||
| $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); | ||
| $this->assertSame( 'foo', loadConfigFromFilesystem( $path_to_file ) ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.