Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ) );
Comment thread
rgadon107 marked this conversation as resolved.
}

/**
* 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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgadon107 Notice how this integration test is constructed. What is it doing?

  1. It's validating that _load_config_from_filesystem() does do what we expect:
    • Loads the config from file.
    • throws the error we expect
  2. _merge_with_defaults does get called.
  3. _the_store does get called.

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
Expand Up @@ -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.
Expand Down
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 ) );

@hellofromtonya hellofromtonya Jun 28, 2019

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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:

  1. The number of times each is invoked, i.e. once().
  2. The arguments we expect each to received, i.e. with.
  3. The value that is returned, i.e. andReturn.

When we invoke loadConfigFromFilesystem(), each of these internal functions will be exercised. BrainMonkey is checking the number of times and arguments for us. It then mocks the function out and returns the value we coded.

}

/**
* 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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 ) );
}
}