From dba7a0e1a6761916877fa9c2e64d7d9874b19e41 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Mon, 8 Jul 2024 08:24:11 +0200 Subject: [PATCH] tests: update tests with current implementation Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- src/Binding/Binder/ObservableBinder.php | 41 +++++++++++++++ ...oaderTest.php => ArrayDefinitionsTest.php} | 25 +++++----- tests/Binding/CompositeBindingLoaderTest.php | 38 ++++++-------- tests/Dumper/PhpFileDumperTest.php | 6 ++- tests/HookDriver/GenericDriverTest.php | 50 +++++++++++++------ tests/Loader/PhpFileLoaderTest.php | 6 ++- 6 files changed, 110 insertions(+), 56 deletions(-) create mode 100644 src/Binding/Binder/ObservableBinder.php rename tests/Binding/{ArrayBindingLoaderTest.php => ArrayDefinitionsTest.php} (65%) diff --git a/src/Binding/Binder/ObservableBinder.php b/src/Binding/Binder/ObservableBinder.php new file mode 100644 index 0000000..8012f7b --- /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 a7e5a20..c6f965d 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 9cfb118..b8e2a99 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 71fcab2..8446003 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 991b0e1..ce92478 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 b289218..b996b7e 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 +} -- GitLab