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


Signed-off-by: default avatarBart Jaskulski <bjaskulski@protonmail.com>
parent b3f53d42
No related branches found
No related tags found
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 );
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())
);
}
}
......@@ -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)
);
}
}
......@@ -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
}
......@@ -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 {
}
};
}
}
......@@ -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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment