diff --git a/src/Binding/Binder/ObservableBinder.php b/src/Binding/Binder/ObservableBinder.php new file mode 100644 index 0000000000000000000000000000000000000000..8012f7ba35179c8e53ca70da1347be5cad746438 --- /dev/null +++ b/src/Binding/Binder/ObservableBinder.php @@ -0,0 +1,41 @@ +<?php + +declare(strict_types=1); + +namespace WPDesk\Init\Binding\Binder; + +use WPDesk\Init\Binding\Binder; +use WPDesk\Init\Binding\ComposableBinder; +use WPDesk\Init\Binding\Definition; + +/** + * Binder decorator, specifically built for testing purposes. Can naively investigate other binders. + */ +final class ObservableBinder implements ComposableBinder { + + /** @var Binder */ + private $binder; + + private $binds_count = 0; + + public function __construct( Binder $b ) { + $this->binder = $b; + } + + public function bind( Definition $def ): void { + $this->binder->bind( $def ); + $this->binds_count++; + } + + public function can_bind( Definition $def ): bool { + if ( $this->binder instanceof ComposableBinder ) { + return $this->binder->can_bind( $def ); + } + + return true; + } + + public function binds_count(): int { + return $this->binds_count; + } +} diff --git a/tests/Binding/ArrayBindingLoaderTest.php b/tests/Binding/ArrayDefinitionsTest.php similarity index 65% rename from tests/Binding/ArrayBindingLoaderTest.php rename to tests/Binding/ArrayDefinitionsTest.php index a7e5a20192e09150a6a9ef5076f86e2414769d3f..c6f965d17e432691a0e6405f015f165dd18a9fbe 100644 --- a/tests/Binding/ArrayBindingLoaderTest.php +++ b/tests/Binding/ArrayDefinitionsTest.php @@ -3,10 +3,11 @@ declare( strict_types=1 ); namespace WPDesk\Init\Tests\Binding; +use WPDesk\Init\Binding\Definition\UnknownDefinition; use WPDesk\Init\Binding\Loader\ArrayDefinitions; use WPDesk\Init\Tests\TestCase; -class ArrayBindingLoaderTest extends TestCase { +class ArrayDefinitionsTest extends TestCase { public function test_loading_empty_bindings(): void { $a = new ArrayDefinitions([]); @@ -25,13 +26,9 @@ class ArrayBindingLoaderTest extends TestCase { ]); $this->assertEquals( [ - 'hook' => [ - 'bind1', - 'bind2', - ], - 'hook2' => [ - 'bind3', - ] + new UnknownDefinition('bind1', 'hook'), + new UnknownDefinition('bind2', 'hook'), + new UnknownDefinition('bind3', 'hook2'), ], iterator_to_array($a->load()) ); @@ -45,8 +42,9 @@ class ArrayBindingLoaderTest extends TestCase { ]); $this->assertEquals( [ - '' => ['bind1', 'bind2'], - 'hook' => ['bind3'], + new UnknownDefinition('bind1', null), + new UnknownDefinition('bind2', null), + new UnknownDefinition('bind3', 'hook'), ], iterator_to_array($a->load()) ); @@ -58,11 +56,12 @@ class ArrayBindingLoaderTest extends TestCase { ]); $this->assertEquals( [ - '' => ['bind1'], - 'not_a_hook' => ['bind2'], - 'hook' => ['bind3'], + new UnknownDefinition('bind1', null), + new UnknownDefinition('bind2', 'not_a_hook'), + new UnknownDefinition('bind3', 'hook'), ], iterator_to_array($a->load()) ); } + } diff --git a/tests/Binding/CompositeBindingLoaderTest.php b/tests/Binding/CompositeBindingLoaderTest.php index 9cfb1186f1bbbb348bd27beef459d085f6b6b685..b8e2a992fd3e0f786c6f6f27eb5caed3f877d7dc 100644 --- a/tests/Binding/CompositeBindingLoaderTest.php +++ b/tests/Binding/CompositeBindingLoaderTest.php @@ -3,6 +3,7 @@ declare( strict_types=1 ); namespace WPDesk\Init\Tests\Binding; +use WPDesk\Init\Binding\Definition\UnknownDefinition; use WPDesk\Init\Binding\Loader\ArrayDefinitions; use WPDesk\Init\Binding\Loader\CompositeBindingLoader; use WPDesk\Init\Tests\TestCase; @@ -34,21 +35,17 @@ class CompositeBindingLoaderTest extends TestCase { ); $this->assertEquals( [ - 'hook' => [ - 'bind1', - 'bind2', - ], - 'hook2' => [ - 'bind3', - ] + new UnknownDefinition('bind1', 'hook'), + new UnknownDefinition('bind2', 'hook'), + new UnknownDefinition('bind3', 'hook2'), ], - iterator_to_array($a->load()) + iterator_to_array($a->load(), false) ); } public function test_loading_unstructured_bindings(): void { $a = new CompositeBindingLoader( - new ArrayDefinitions( [ + new ArrayDefinitions([ 'bind1', ]), new ArrayDefinitions([ @@ -58,16 +55,13 @@ class CompositeBindingLoaderTest extends TestCase { 'hook' => 'bind3', ]) ); - $actual = []; - foreach ($a->load() as $k => $v) { - $actual[$k] = array_merge( $actual[$k] ?? [], (array) $v ); - } $this->assertEquals( [ - '' => ['bind1', 'bind2'], - 'hook' => ['bind3'], + new UnknownDefinition('bind1', null), + new UnknownDefinition('bind2', null), + new UnknownDefinition('bind3', 'hook'), ], - $actual + iterator_to_array($a->load(), false) ); $a = new CompositeBindingLoader( @@ -81,17 +75,13 @@ class CompositeBindingLoaderTest extends TestCase { 'hook' => ['bind3'], ]), ); - $actual = []; - foreach ($a->load() as $k => $v) { - $actual[$k] = array_merge( $actual[$k] ?? [], (array) $v ); - } $this->assertEquals( [ - '' => ['bind1'], - 'not_a_hook' => ['bind2'], - 'hook' => ['bind3'], + new UnknownDefinition('bind1', null), + new UnknownDefinition('bind2', 'not_a_hook'), + new UnknownDefinition('bind3', 'hook'), ], - $actual + iterator_to_array($a->load(), false) ); } } diff --git a/tests/Dumper/PhpFileDumperTest.php b/tests/Dumper/PhpFileDumperTest.php index 71fcab288f4aa99d01bb11e6a8ffd556aca13b99..84460038121f0a2b3a734cdb440529b2e54f7454 100644 --- a/tests/Dumper/PhpFileDumperTest.php +++ b/tests/Dumper/PhpFileDumperTest.php @@ -3,11 +3,13 @@ declare( strict_types=1 ); namespace WPDesk\Init\Tests\Dumper; +use WPDesk\Init\Util\PhpFileDumper; + class PhpFileDumperTest extends \WPDesk\Init\Tests\TestCase { public function test_dump_php_file() { $dir = $this->initTempPlugin(); - $dumper = new \WPDesk\Init\Dumper\PhpFileDumper(); + $dumper = new PhpFileDumper(); $dumper->dump( [ 'foo' => 'bar' ], $dir . '/dump.php' ); $this->assertFileExists( $dir . '/dump.php' ); $content = include $dir . '/dump.php'; @@ -15,4 +17,4 @@ class PhpFileDumperTest extends \WPDesk\Init\Tests\TestCase { $this->assertEquals( [ 'foo' => 'bar' ], $content ); } -} \ No newline at end of file +} diff --git a/tests/HookDriver/GenericDriverTest.php b/tests/HookDriver/GenericDriverTest.php index 991b0e1c708ed04abb128b868cb143fbdae03b82..ce92478304e5937f25ca752d80a7191be955d31d 100644 --- a/tests/HookDriver/GenericDriverTest.php +++ b/tests/HookDriver/GenericDriverTest.php @@ -3,26 +3,25 @@ declare( strict_types=1 ); namespace WPDesk\Init\Tests\HookDriver; -use WPDesk\Init\Binding\HookBinder; -use WPDesk\Init\Binding\ObservableBinder; +use WPDesk\Init\Binding\Binder; +use WPDesk\Init\Binding\Binder\HookableBinder; +use WPDesk\Init\Binding\Binder\ObservableBinder; +use WPDesk\Init\Binding\Definition; use WPDesk\Init\HookDriver\GenericDriver; use WPDesk\Init\Configuration\Configuration; use Psr\Container\ContainerInterface; use WPDesk\Init\Binding\Loader\ArrayDefinitions; -use WPDesk\Init\Binding\StoppableBinder; use WPDesk\Init\Tests\TestCase; class GenericDriverTest extends TestCase { public function provider(): iterable { yield [ - [ - 'fake_binder' => new ObservableBinder(new class implements HookBinder { + 'fake_binder' => new ObservableBinder(new class implements Binder { - public function bind(): void { + public function bind( Definition $def ): void { } }), - ], function ( $binder ) { $this->assertTrue( $binder->is_bound() ); } @@ -45,7 +44,7 @@ class GenericDriverTest extends TestCase { // return $this->is_bound; // } // }, -// 'fake_binder' => new ObservableBinder(new class implements HookBinder { +// 'fake_binder' => new ObservableBinder(new class implements Binder { // // public function bind(): void { // } @@ -57,15 +56,28 @@ class GenericDriverTest extends TestCase { // ]; } - /** @dataProvider provider */ - public function test_register_hooks( array $hook_bindings, callable $assertion ): void { - $driver = new GenericDriver( new ArrayDefinitions(array_keys($hook_bindings)) ); - $driver->register_hooks( new Configuration([]), $this->getContainer($hook_bindings) ); + public function test_register_no_hooks(): void { + $binder = new ObservableBinder($this->getBinder()); + $driver = new GenericDriver( + new ArrayDefinitions([]), + $binder + ); + $driver->register_hooks(); - foreach ( $hook_bindings as $hook => $binder ) { - $assertion( $binder ); - } + $this->assertEquals(0, $binder->binds_count()); + } + + public function test_register_hooks(): void { + $binder = new ObservableBinder($this->getBinder()); + $driver = new GenericDriver( + new ArrayDefinitions(['' => ['hook1', 'hook2']]), + $binder + ); + + $driver->register_hooks(); + + $this->assertEquals(2, $binder->binds_count()); } private function getContainer( array $services ): ContainerInterface { @@ -84,4 +96,12 @@ class GenericDriverTest extends TestCase { } }; } + + private function getBinder(): Binder { + return new class implements Binder { + + public function bind( Definition $def ): void { + } + }; + } } diff --git a/tests/Loader/PhpFileLoaderTest.php b/tests/Loader/PhpFileLoaderTest.php index b2892183077409dac3c6a4d77906f4f47a75b438..b996b7e9d634d4e460b582ed44d3b72d17d2a2fc 100644 --- a/tests/Loader/PhpFileLoaderTest.php +++ b/tests/Loader/PhpFileLoaderTest.php @@ -3,13 +3,15 @@ declare( strict_types=1 ); namespace WPDesk\Init\Tests\Loader; +use WPDesk\Init\Util\PhpFileLoader; + class PhpFileLoaderTest extends \WPDesk\Init\Tests\TestCase { public function test_load_php_file() { - $loader = new \WPDesk\Init\Loader\PhpFileLoader(); + $loader = new PhpFileLoader(); $resource = __DIR__ . '/../Fixtures/load.php'; $data = $loader->load( $resource ); $this->assertEquals( [ 'foo' => 'bar' ], $data ); } -} \ No newline at end of file +}