Skip to content
Snippets Groups Projects
Verified Commit dba7a0e1 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

tests: update tests with current implementation

parent b3f53d42
Branches
Tags
2 merge requests!3improve into wp-hook and some additional unfinished things,!21.x
<?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;
}
}
...@@ -3,10 +3,11 @@ declare( strict_types=1 ); ...@@ -3,10 +3,11 @@ declare( strict_types=1 );
namespace WPDesk\Init\Tests\Binding; namespace WPDesk\Init\Tests\Binding;
use WPDesk\Init\Binding\Definition\UnknownDefinition;
use WPDesk\Init\Binding\Loader\ArrayDefinitions; use WPDesk\Init\Binding\Loader\ArrayDefinitions;
use WPDesk\Init\Tests\TestCase; use WPDesk\Init\Tests\TestCase;
class ArrayBindingLoaderTest extends TestCase { class ArrayDefinitionsTest extends TestCase {
public function test_loading_empty_bindings(): void { public function test_loading_empty_bindings(): void {
$a = new ArrayDefinitions([]); $a = new ArrayDefinitions([]);
...@@ -25,13 +26,9 @@ class ArrayBindingLoaderTest extends TestCase { ...@@ -25,13 +26,9 @@ class ArrayBindingLoaderTest extends TestCase {
]); ]);
$this->assertEquals( $this->assertEquals(
[ [
'hook' => [ new UnknownDefinition('bind1', 'hook'),
'bind1', new UnknownDefinition('bind2', 'hook'),
'bind2', new UnknownDefinition('bind3', 'hook2'),
],
'hook2' => [
'bind3',
]
], ],
iterator_to_array($a->load()) iterator_to_array($a->load())
); );
...@@ -45,8 +42,9 @@ class ArrayBindingLoaderTest extends TestCase { ...@@ -45,8 +42,9 @@ class ArrayBindingLoaderTest extends TestCase {
]); ]);
$this->assertEquals( $this->assertEquals(
[ [
'' => ['bind1', 'bind2'], new UnknownDefinition('bind1', null),
'hook' => ['bind3'], new UnknownDefinition('bind2', null),
new UnknownDefinition('bind3', 'hook'),
], ],
iterator_to_array($a->load()) iterator_to_array($a->load())
); );
...@@ -58,11 +56,12 @@ class ArrayBindingLoaderTest extends TestCase { ...@@ -58,11 +56,12 @@ class ArrayBindingLoaderTest extends TestCase {
]); ]);
$this->assertEquals( $this->assertEquals(
[ [
'' => ['bind1'], new UnknownDefinition('bind1', null),
'not_a_hook' => ['bind2'], new UnknownDefinition('bind2', 'not_a_hook'),
'hook' => ['bind3'], new UnknownDefinition('bind3', 'hook'),
], ],
iterator_to_array($a->load()) iterator_to_array($a->load())
); );
} }
} }
...@@ -3,6 +3,7 @@ declare( strict_types=1 ); ...@@ -3,6 +3,7 @@ declare( strict_types=1 );
namespace WPDesk\Init\Tests\Binding; namespace WPDesk\Init\Tests\Binding;
use WPDesk\Init\Binding\Definition\UnknownDefinition;
use WPDesk\Init\Binding\Loader\ArrayDefinitions; use WPDesk\Init\Binding\Loader\ArrayDefinitions;
use WPDesk\Init\Binding\Loader\CompositeBindingLoader; use WPDesk\Init\Binding\Loader\CompositeBindingLoader;
use WPDesk\Init\Tests\TestCase; use WPDesk\Init\Tests\TestCase;
...@@ -34,15 +35,11 @@ class CompositeBindingLoaderTest extends TestCase { ...@@ -34,15 +35,11 @@ class CompositeBindingLoaderTest extends TestCase {
); );
$this->assertEquals( $this->assertEquals(
[ [
'hook' => [ new UnknownDefinition('bind1', 'hook'),
'bind1', new UnknownDefinition('bind2', 'hook'),
'bind2', new UnknownDefinition('bind3', 'hook2'),
], ],
'hook2' => [ iterator_to_array($a->load(), false)
'bind3',
]
],
iterator_to_array($a->load())
); );
} }
...@@ -58,16 +55,13 @@ class CompositeBindingLoaderTest extends TestCase { ...@@ -58,16 +55,13 @@ class CompositeBindingLoaderTest extends TestCase {
'hook' => 'bind3', 'hook' => 'bind3',
]) ])
); );
$actual = [];
foreach ($a->load() as $k => $v) {
$actual[$k] = array_merge( $actual[$k] ?? [], (array) $v );
}
$this->assertEquals( $this->assertEquals(
[ [
'' => ['bind1', 'bind2'], new UnknownDefinition('bind1', null),
'hook' => ['bind3'], new UnknownDefinition('bind2', null),
new UnknownDefinition('bind3', 'hook'),
], ],
$actual iterator_to_array($a->load(), false)
); );
$a = new CompositeBindingLoader( $a = new CompositeBindingLoader(
...@@ -81,17 +75,13 @@ class CompositeBindingLoaderTest extends TestCase { ...@@ -81,17 +75,13 @@ class CompositeBindingLoaderTest extends TestCase {
'hook' => ['bind3'], 'hook' => ['bind3'],
]), ]),
); );
$actual = [];
foreach ($a->load() as $k => $v) {
$actual[$k] = array_merge( $actual[$k] ?? [], (array) $v );
}
$this->assertEquals( $this->assertEquals(
[ [
'' => ['bind1'], new UnknownDefinition('bind1', null),
'not_a_hook' => ['bind2'], new UnknownDefinition('bind2', 'not_a_hook'),
'hook' => ['bind3'], new UnknownDefinition('bind3', 'hook'),
], ],
$actual iterator_to_array($a->load(), false)
); );
} }
} }
...@@ -3,11 +3,13 @@ declare( strict_types=1 ); ...@@ -3,11 +3,13 @@ declare( strict_types=1 );
namespace WPDesk\Init\Tests\Dumper; namespace WPDesk\Init\Tests\Dumper;
use WPDesk\Init\Util\PhpFileDumper;
class PhpFileDumperTest extends \WPDesk\Init\Tests\TestCase { class PhpFileDumperTest extends \WPDesk\Init\Tests\TestCase {
public function test_dump_php_file() { public function test_dump_php_file() {
$dir = $this->initTempPlugin(); $dir = $this->initTempPlugin();
$dumper = new \WPDesk\Init\Dumper\PhpFileDumper(); $dumper = new PhpFileDumper();
$dumper->dump( [ 'foo' => 'bar' ], $dir . '/dump.php' ); $dumper->dump( [ 'foo' => 'bar' ], $dir . '/dump.php' );
$this->assertFileExists( $dir . '/dump.php' ); $this->assertFileExists( $dir . '/dump.php' );
$content = include $dir . '/dump.php'; $content = include $dir . '/dump.php';
......
...@@ -3,26 +3,25 @@ declare( strict_types=1 ); ...@@ -3,26 +3,25 @@ declare( strict_types=1 );
namespace WPDesk\Init\Tests\HookDriver; namespace WPDesk\Init\Tests\HookDriver;
use WPDesk\Init\Binding\HookBinder; use WPDesk\Init\Binding\Binder;
use WPDesk\Init\Binding\ObservableBinder; 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\HookDriver\GenericDriver;
use WPDesk\Init\Configuration\Configuration; use WPDesk\Init\Configuration\Configuration;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use WPDesk\Init\Binding\Loader\ArrayDefinitions; use WPDesk\Init\Binding\Loader\ArrayDefinitions;
use WPDesk\Init\Binding\StoppableBinder;
use WPDesk\Init\Tests\TestCase; use WPDesk\Init\Tests\TestCase;
class GenericDriverTest extends TestCase { class GenericDriverTest extends TestCase {
public function provider(): iterable { public function provider(): iterable {
yield [ yield [
[ 'fake_binder' => new ObservableBinder(new class implements Binder {
'fake_binder' => new ObservableBinder(new class implements HookBinder {
public function bind(): void { public function bind( Definition $def ): void {
} }
}), }),
],
function ( $binder ) { function ( $binder ) {
$this->assertTrue( $binder->is_bound() ); $this->assertTrue( $binder->is_bound() );
} }
...@@ -45,7 +44,7 @@ class GenericDriverTest extends TestCase { ...@@ -45,7 +44,7 @@ class GenericDriverTest extends TestCase {
// return $this->is_bound; // return $this->is_bound;
// } // }
// }, // },
// 'fake_binder' => new ObservableBinder(new class implements HookBinder { // 'fake_binder' => new ObservableBinder(new class implements Binder {
// //
// public function bind(): void { // public function bind(): void {
// } // }
...@@ -57,15 +56,28 @@ class GenericDriverTest extends TestCase { ...@@ -57,15 +56,28 @@ class GenericDriverTest extends TestCase {
// ]; // ];
} }
/** @dataProvider provider */ public function test_register_no_hooks(): void {
public function test_register_hooks( array $hook_bindings, callable $assertion ): void { $binder = new ObservableBinder($this->getBinder());
$driver = new GenericDriver( new ArrayDefinitions(array_keys($hook_bindings)) ); $driver = new GenericDriver(
$driver->register_hooks( new Configuration([]), $this->getContainer($hook_bindings) ); new ArrayDefinitions([]),
$binder
);
$driver->register_hooks();
foreach ( $hook_bindings as $hook => $binder ) { $this->assertEquals(0, $binder->binds_count());
$assertion( $binder );
} }
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 { private function getContainer( array $services ): ContainerInterface {
...@@ -84,4 +96,12 @@ class GenericDriverTest extends TestCase { ...@@ -84,4 +96,12 @@ class GenericDriverTest extends TestCase {
} }
}; };
} }
private function getBinder(): Binder {
return new class implements Binder {
public function bind( Definition $def ): void {
}
};
}
} }
...@@ -3,10 +3,12 @@ declare( strict_types=1 ); ...@@ -3,10 +3,12 @@ declare( strict_types=1 );
namespace WPDesk\Init\Tests\Loader; namespace WPDesk\Init\Tests\Loader;
use WPDesk\Init\Util\PhpFileLoader;
class PhpFileLoaderTest extends \WPDesk\Init\Tests\TestCase { class PhpFileLoaderTest extends \WPDesk\Init\Tests\TestCase {
public function test_load_php_file() { public function test_load_php_file() {
$loader = new \WPDesk\Init\Loader\PhpFileLoader(); $loader = new PhpFileLoader();
$resource = __DIR__ . '/../Fixtures/load.php'; $resource = __DIR__ . '/../Fixtures/load.php';
$data = $loader->load( $resource ); $data = $loader->load( $resource );
$this->assertEquals( [ 'foo' => 'bar' ], $data ); $this->assertEquals( [ 'foo' => 'bar' ], $data );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment