|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WPEmergeTests\ServiceProviders; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use Pimple\Container; |
| 7 | +use WPEmerge\ServiceProviders\ExtendsConfigTrait; |
| 8 | +use WP_UnitTestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * @coversDefaultClass \WPEmerge\ServiceProviders\ExtendsConfigTrait |
| 12 | + */ |
| 13 | +class ExtendsConfigTraitTest extends WP_UnitTestCase { |
| 14 | + public function setUp() { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + $this->subject = $this->getMockForTrait( ExtendsConfigTrait::class ); |
| 18 | + } |
| 19 | + |
| 20 | + public function tearDown() { |
| 21 | + parent::tearDown(); |
| 22 | + |
| 23 | + unset( $this->subject ); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @covers ::extendConfig |
| 28 | + */ |
| 29 | + public function testExtendConfig_ConfigNotSet_Default() { |
| 30 | + $container = new Container( [ |
| 31 | + WPEMERGE_CONFIG_KEY => [], |
| 32 | + ] ); |
| 33 | + $key = 'foo'; |
| 34 | + $default = 'bar'; |
| 35 | + $expected = $default; |
| 36 | + |
| 37 | + $this->subject->extendConfig( $container, $key, $default ); |
| 38 | + |
| 39 | + $this->assertEquals( $expected, $container[ WPEMERGE_CONFIG_KEY ][ $key ] ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @covers ::extendConfig |
| 44 | + */ |
| 45 | + public function testExtendConfig_NotArrays_Replace() { |
| 46 | + $container = new Container( [ |
| 47 | + WPEMERGE_CONFIG_KEY => [ |
| 48 | + 'foo' => 'foo', |
| 49 | + ], |
| 50 | + ] ); |
| 51 | + $key = 'foo'; |
| 52 | + $default = 'bar'; |
| 53 | + $expected = 'foo'; |
| 54 | + |
| 55 | + $this->subject->extendConfig( $container, $key, $default ); |
| 56 | + |
| 57 | + $this->assertEquals( $expected, $container[ WPEMERGE_CONFIG_KEY ][ $key ] ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @covers ::extendConfig |
| 62 | + */ |
| 63 | + public function testExtendConfig_Arrays_RecursiveReplace() { |
| 64 | + $container = new Container( [ |
| 65 | + WPEMERGE_CONFIG_KEY => [ |
| 66 | + 'foo' => [ |
| 67 | + 'foo' => 'foo', |
| 68 | + 'bar' => 'bar', |
| 69 | + 'baz' => [ |
| 70 | + 'foo' => 'foo', |
| 71 | + ] |
| 72 | + ], |
| 73 | + ], |
| 74 | + ] ); |
| 75 | + $key = 'foo'; |
| 76 | + $default = [ |
| 77 | + 'bar' => 'foobarbaz', |
| 78 | + 'baz' => [ |
| 79 | + 'bar' => 'bar', |
| 80 | + ], |
| 81 | + 'foobarbaz' => 'foobarbaz', |
| 82 | + ]; |
| 83 | + $expected = [ |
| 84 | + // Value is NOT missing. |
| 85 | + 'foo' => 'foo', |
| 86 | + // Value is NOT replaced by default. |
| 87 | + 'bar' => 'bar', |
| 88 | + 'baz' => [ |
| 89 | + 'foo' => 'foo', |
| 90 | + // Key from default is added in nested array. |
| 91 | + 'bar' => 'bar', |
| 92 | + ], |
| 93 | + // Key from default is added. |
| 94 | + 'foobarbaz' => 'foobarbaz', |
| 95 | + ]; |
| 96 | + |
| 97 | + $this->subject->extendConfig( $container, $key, $default ); |
| 98 | + |
| 99 | + $this->assertEquals( $expected, $container[ WPEMERGE_CONFIG_KEY ][ $key ] ); |
| 100 | + } |
| 101 | +} |
0 commit comments