View C comes from child theme.
\ No newline at end of file diff --git a/tests/php/Fixture/views/dynamic-view.php b/tests/php/Fixture/views/dynamic-view.php new file mode 100644 index 0000000..abc8359 --- /dev/null +++ b/tests/php/Fixture/views/dynamic-view.php @@ -0,0 +1 @@ +Rendering works with context: = $this->some_value ?>.
\ No newline at end of file diff --git a/tests/php/Fixture/views/parent_theme/partial-b.php b/tests/php/Fixture/views/parent_theme/partial-b.php new file mode 100644 index 0000000..15b407e --- /dev/null +++ b/tests/php/Fixture/views/parent_theme/partial-b.php @@ -0,0 +1 @@ +partial B from parent theme - = $this->render_partial( 'partial-c' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/parent_theme/partial-c.php b/tests/php/Fixture/views/parent_theme/partial-c.php new file mode 100644 index 0000000..eef22e7 --- /dev/null +++ b/tests/php/Fixture/views/parent_theme/partial-c.php @@ -0,0 +1 @@ +partial C from parent theme - = $this->render_partial( 'partial-d' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/parent_theme/partial-d.php b/tests/php/Fixture/views/parent_theme/partial-d.php new file mode 100644 index 0000000..7d2283a --- /dev/null +++ b/tests/php/Fixture/views/parent_theme/partial-d.php @@ -0,0 +1 @@ +partial D from parent theme - = $this->render_partial( 'partial-e' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/parent_theme/view-b.php b/tests/php/Fixture/views/parent_theme/view-b.php new file mode 100644 index 0000000..f68ff30 --- /dev/null +++ b/tests/php/Fixture/views/parent_theme/view-b.php @@ -0,0 +1 @@ +View B comes from parent theme.
\ No newline at end of file diff --git a/tests/php/Fixture/views/parent_theme/view-c.php b/tests/php/Fixture/views/parent_theme/view-c.php new file mode 100644 index 0000000..746216c --- /dev/null +++ b/tests/php/Fixture/views/parent_theme/view-c.php @@ -0,0 +1 @@ +View C comes from parent theme.
\ No newline at end of file diff --git a/tests/php/Fixture/views/partial.php b/tests/php/Fixture/views/partial.php new file mode 100644 index 0000000..f0ffa7a --- /dev/null +++ b/tests/php/Fixture/views/partial.php @@ -0,0 +1 @@ += $this->some_value ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/dynamic-view.php b/tests/php/Fixture/views/plugin/dynamic-view.php new file mode 100644 index 0000000..abc8359 --- /dev/null +++ b/tests/php/Fixture/views/plugin/dynamic-view.php @@ -0,0 +1 @@ +Rendering works with context: = $this->some_value ?>.
\ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/partial-a.php b/tests/php/Fixture/views/plugin/partial-a.php new file mode 100644 index 0000000..7737513 --- /dev/null +++ b/tests/php/Fixture/views/plugin/partial-a.php @@ -0,0 +1 @@ +partial A from plugin - = $this->render_partial( 'partial-b' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/partial-b.php b/tests/php/Fixture/views/plugin/partial-b.php new file mode 100644 index 0000000..ab64dac --- /dev/null +++ b/tests/php/Fixture/views/plugin/partial-b.php @@ -0,0 +1 @@ +partial B from plugin - = $this->render_partial( 'partial-c' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/partial-c.php b/tests/php/Fixture/views/plugin/partial-c.php new file mode 100644 index 0000000..488957b --- /dev/null +++ b/tests/php/Fixture/views/plugin/partial-c.php @@ -0,0 +1 @@ +partial C from plugin - = $this->render_partial( 'partial-d' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/partial-d.php b/tests/php/Fixture/views/plugin/partial-d.php new file mode 100644 index 0000000..9207827 --- /dev/null +++ b/tests/php/Fixture/views/plugin/partial-d.php @@ -0,0 +1 @@ +partial D from plugin - = $this->render_partial( 'partial-e' ) ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/partial-e.php b/tests/php/Fixture/views/plugin/partial-e.php new file mode 100644 index 0000000..74cd26a --- /dev/null +++ b/tests/php/Fixture/views/plugin/partial-e.php @@ -0,0 +1 @@ +partial E from plugin \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/partial.php b/tests/php/Fixture/views/plugin/partial.php new file mode 100644 index 0000000..f0ffa7a --- /dev/null +++ b/tests/php/Fixture/views/plugin/partial.php @@ -0,0 +1 @@ += $this->some_value ?> \ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/static-view.php b/tests/php/Fixture/views/plugin/static-view.php new file mode 100644 index 0000000..942f557 --- /dev/null +++ b/tests/php/Fixture/views/plugin/static-view.php @@ -0,0 +1 @@ +Rendering works.
diff --git a/tests/php/Fixture/views/plugin/view-a.php b/tests/php/Fixture/views/plugin/view-a.php new file mode 100644 index 0000000..0fdd1f0 --- /dev/null +++ b/tests/php/Fixture/views/plugin/view-a.php @@ -0,0 +1 @@ +View A comes from plugin.
\ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/view-b.php b/tests/php/Fixture/views/plugin/view-b.php new file mode 100644 index 0000000..fed079b --- /dev/null +++ b/tests/php/Fixture/views/plugin/view-b.php @@ -0,0 +1 @@ +View B comes from plugin.
\ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/view-c.php b/tests/php/Fixture/views/plugin/view-c.php new file mode 100644 index 0000000..5b3f140 --- /dev/null +++ b/tests/php/Fixture/views/plugin/view-c.php @@ -0,0 +1 @@ +View C comes from plugin.
\ No newline at end of file diff --git a/tests/php/Fixture/views/plugin/view-with-partial.php b/tests/php/Fixture/views/plugin/view-with-partial.php new file mode 100644 index 0000000..9c16fc2 --- /dev/null +++ b/tests/php/Fixture/views/plugin/view-with-partial.php @@ -0,0 +1 @@ +Rendering works with partials: = $this->render_partial( 'partial' ) ?>.
\ No newline at end of file diff --git a/tests/php/Fixture/views/static-view.php b/tests/php/Fixture/views/static-view.php new file mode 100644 index 0000000..942f557 --- /dev/null +++ b/tests/php/Fixture/views/static-view.php @@ -0,0 +1 @@ +Rendering works.
diff --git a/tests/php/Fixture/views/view-with-partial.php b/tests/php/Fixture/views/view-with-partial.php new file mode 100644 index 0000000..ee7a8cb --- /dev/null +++ b/tests/php/Fixture/views/view-with-partial.php @@ -0,0 +1 @@ +Rendering works with partials: = $this->render_partial( 'tests/php/Fixture/views/partial' ) ?>.
\ No newline at end of file diff --git a/tests/php/Integration/SimpleViewFactoryTest.php b/tests/php/Integration/SimpleViewFactoryTest.php new file mode 100644 index 0000000..a1dbc32 --- /dev/null +++ b/tests/php/Integration/SimpleViewFactoryTest.php @@ -0,0 +1,25 @@ +create( ViewHelper::VIEWS_FOLDER . 'static-view' ); + $this->assertInstanceOf( SimpleView::class, $view ); + } + + public function test_created_views_implement_the_interface(): void { + $factory = new SimpleViewFactory(); + + $view = $factory->create( ViewHelper::VIEWS_FOLDER . 'static-view' ); + $this->assertInstanceOf( View::class, $view ); + } +} diff --git a/tests/php/Integration/SimpleViewTest.php b/tests/php/Integration/SimpleViewTest.php new file mode 100644 index 0000000..29e4ecc --- /dev/null +++ b/tests/php/Integration/SimpleViewTest.php @@ -0,0 +1,22 @@ +assertStringStartsWith( + 'Rendering works.
', + $view->render() + ); + } +} diff --git a/tests/php/Integration/TemplatedViewFactoryTest.php b/tests/php/Integration/TemplatedViewFactoryTest.php new file mode 100644 index 0000000..e95d985 --- /dev/null +++ b/tests/php/Integration/TemplatedViewFactoryTest.php @@ -0,0 +1,25 @@ +create( 'static-view' ); + $this->assertInstanceOf( TemplatedView::class, $view ); + } + + public function test_created_views_implement_the_interface(): void { + $factory = new TemplatedViewFactory( ViewHelper::LOCATIONS ); + + $view = $factory->create( 'static-view' ); + $this->assertInstanceOf( View::class, $view ); + } +} diff --git a/tests/php/Integration/TemplatedViewTest.php b/tests/php/Integration/TemplatedViewTest.php new file mode 100644 index 0000000..c02105b --- /dev/null +++ b/tests/php/Integration/TemplatedViewTest.php @@ -0,0 +1,23 @@ +assertStringStartsWith( + 'partial A from plugin - partial B from parent theme - partial C from child theme - partial D from parent theme - partial E from plugin', + $partials->render() + ); + } +} diff --git a/tests/php/Integration/TestCase.php b/tests/php/Integration/TestCase.php new file mode 100644 index 0000000..e9c96cf --- /dev/null +++ b/tests/php/Integration/TestCase.php @@ -0,0 +1,17 @@ +assertInstanceOf( InjectionChain::class, $chain ); + } + + public function test_it_accepts_new_resolutions(): void { + $chain = ( new InjectionChain() ) + ->add_resolution( 'something' ); + + $this->assertTrue( $chain->has_resolution( 'something' ) ); + $this->assertFalse( $chain->has_resolution( 'something_else' ) ); + } + + public function test_it_accepts_new_chain_entries(): void { + $chain = ( new InjectionChain() ) + ->add_to_chain( 'something' ); + + $this->assertEquals( 'something', $chain->get_class() ); + } + + public function test_it_returns_the_last_class_in_the_chain(): void { + $chain = ( new InjectionChain() ) + ->add_to_chain( 'first' ) + ->add_to_chain( 'second' ) + ->add_to_chain( 'third' ); + + $this->assertEquals( 'third', $chain->get_class() ); + } + + public function test_it_retains_all_elements_in_the_chain(): void { + $chain = ( new InjectionChain() ) + ->add_to_chain( 'first' ) + ->add_to_chain( 'second' ) + ->add_to_chain( 'third' ); + + $this->assertEquals( [ 'third', 'second', 'first' ], $chain->get_chain() ); + } +} diff --git a/tests/php/Unit/SimpleInjectorTest.php b/tests/php/Unit/SimpleInjectorTest.php new file mode 100644 index 0000000..5679755 --- /dev/null +++ b/tests/php/Unit/SimpleInjectorTest.php @@ -0,0 +1,111 @@ +assertInstanceOf( SimpleInjector::class, $injector ); + } + + public function test_it_implements_the_interface(): void { + $injector = new SimpleInjector(); + + $this->assertInstanceOf( Injector::class, $injector ); + } + + public function test_it_can_instantiate_a_concrete_class(): void { + $object = ( new SimpleInjector() ) + ->make( Fixture\DummyClass::class ); + + $this->assertInstanceOf( Fixture\DummyClass::class, $object ); + } + + public function test_it_can_autowire_a_class_with_a_dependency(): void { + $object = ( new SimpleInjector() ) + ->make( Fixture\DummyClassWithDependency::class ); + + $this->assertInstanceOf( Fixture\DummyClassWithDependency::class, $object ); + $this->assertInstanceOf( Fixture\DummyClass::class, $object->get_dummy() ); + } + + public function test_it_can_instantiate_a_bound_interface(): void { + $injector = ( new SimpleInjector() ) + ->bind( + Fixture\DummyInterface::class, + Fixture\DummyClassWithDependency::class + ); + $object = $injector->make( Fixture\DummyInterface::class ); + + $this->assertInstanceOf( Fixture\DummyInterface::class, $object ); + $this->assertInstanceOf( Fixture\DummyClassWithDependency::class, $object ); + $this->assertInstanceOf( Fixture\DummyClass::class, $object->get_dummy() ); + } + + public function test_it_returns_separate_instances_by_default(): void { + $injector = new SimpleInjector(); + $object_a = $injector->make( Fixture\DummyClass::class ); + $object_b = $injector->make( Fixture\DummyClass::class ); + + $this->assertNotSame( $object_a, $object_b ); + } + + public function test_it_returns_same_instances_if_shared(): void { + $injector = ( new SimpleInjector() ) + ->share( Fixture\DummyClass::class ); + $object_a = $injector->make( Fixture\DummyClass::class ); + $object_b = $injector->make( Fixture\DummyClass::class ); + + $this->assertSame( $object_a, $object_b ); + } + + public function test_it_can_instantiate_a_class_with_named_arguments(): void { + $object = ( new SimpleInjector() ) + ->make( + Fixture\DummyClassWithNamedArguments::class, + [ 'argument_a' => 42, 'argument_b' => 'Mr Alderson' ] + ); + + $this->assertInstanceOf( Fixture\DummyClassWithNamedArguments::class, $object ); + $this->assertEquals( 42, $object->get_argument_a() ); + $this->assertEquals( 'Mr Alderson', $object->get_argument_b() ); + } + + public function test_it_allows_for_skipping_named_arguments_with_default_values(): void { + $object = ( new SimpleInjector() ) + ->make( + Fixture\DummyClassWithNamedArguments::class, + [ 'argument_a' => 42 ] + ); + + $this->assertInstanceOf( Fixture\DummyClassWithNamedArguments::class, $object ); + $this->assertEquals( 42, $object->get_argument_a() ); + $this->assertEquals( 'Mr Meeseeks', $object->get_argument_b() ); + } + + public function test_it_throws_if_a_required_named_arguments_is_missing(): void { + $this->expectException( FailedToMakeInstance::class ); + + ( new SimpleInjector() ) + ->make( Fixture\DummyClassWithNamedArguments::class ); + } + + public function test_it_throws_if_a_circular_reference_is_detected(): void { + $this->expectException( FailedToMakeInstance::class ); + $this->expectExceptionCode( FailedToMakeInstance::CIRCULAR_REFERENCE ); + + ( new SimpleInjector() ) + ->bind( + Fixture\DummyClass::class, + Fixture\DummyClassWithDependency::class + ) + ->make( Fixture\DummyClassWithDependency::class ); + } +} diff --git a/tests/php/Unit/SimpleViewFactoryTest.php b/tests/php/Unit/SimpleViewFactoryTest.php new file mode 100644 index 0000000..8dd98f7 --- /dev/null +++ b/tests/php/Unit/SimpleViewFactoryTest.php @@ -0,0 +1,21 @@ +assertInstanceOf( SimpleViewFactory::class, $factory ); + } + + public function test_it_implements_the_interface(): void { + $factory = new SimpleViewFactory(); + + $this->assertInstanceOf( ViewFactory::class, $factory ); + } +} diff --git a/tests/php/Unit/SimpleViewTest.php b/tests/php/Unit/SimpleViewTest.php new file mode 100644 index 0000000..d5327cd --- /dev/null +++ b/tests/php/Unit/SimpleViewTest.php @@ -0,0 +1,68 @@ +createMock( ViewFactory::class ); + $view = new SimpleView( + ViewHelper::VIEWS_FOLDER . 'static-view', + $view_factory_mock + ); + + $this->assertInstanceOf( SimpleView::class, $view ); + } + + public function test_it_can_be_rendered(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view = new SimpleView( + ViewHelper::VIEWS_FOLDER . 'static-view', + $view_factory_mock + ); + + $this->assertStringStartsWith( + 'Rendering works.
', + $view->render() + ); + } + + public function test_it_can_provide_rendering_context(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view = new SimpleView( + ViewHelper::VIEWS_FOLDER . 'dynamic-view', + $view_factory_mock + ); + + $this->assertStringStartsWith( + 'Rendering works with context: 42.
', + $view->render( [ 'some_value' => 42 ] ) + ); + } + + public function test_it_can_render_partials(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view_factory_mock + ->expects( $this->once() ) + ->method( 'create' ) + ->with( ViewHelper::VIEWS_FOLDER . 'partial' ) + ->willReturn( new SimpleView( + ViewHelper::VIEWS_FOLDER . 'partial', + $view_factory_mock + ) ); + + $view = new SimpleView( + ViewHelper::VIEWS_FOLDER . 'view-with-partial', + $view_factory_mock + ); + + $this->assertStringStartsWith( + 'Rendering works with partials: 42.
', + $view->render( [ 'some_value' => 42 ] ) + ); + } +} diff --git a/tests/php/Unit/TemplatedViewFactoryTest.php b/tests/php/Unit/TemplatedViewFactoryTest.php new file mode 100644 index 0000000..59f2a72 --- /dev/null +++ b/tests/php/Unit/TemplatedViewFactoryTest.php @@ -0,0 +1,21 @@ +assertInstanceOf( TemplatedViewFactory::class, $factory ); + } + + public function test_it_implements_the_interface(): void { + $factory = new TemplatedViewFactory(); + + $this->assertInstanceOf( ViewFactory::class, $factory ); + } +} diff --git a/tests/php/Unit/TemplatedViewTest.php b/tests/php/Unit/TemplatedViewTest.php new file mode 100644 index 0000000..20f01d2 --- /dev/null +++ b/tests/php/Unit/TemplatedViewTest.php @@ -0,0 +1,106 @@ +createMock( ViewFactory::class ); + $view = new TemplatedView( + 'static-view', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + + $this->assertInstanceOf( TemplatedView::class, $view ); + } + + public function test_it_can_be_rendered(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view = new TemplatedView( + 'static-view', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + + $this->assertStringStartsWith( + 'Rendering works.
', + $view->render() + ); + } + + public function test_it_can_provide_rendering_context(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view = new TemplatedView( + 'dynamic-view', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + + $this->assertStringStartsWith( + 'Rendering works with context: 42.
', + $view->render( [ 'some_value' => 42 ] ) + ); + } + + public function test_it_can_render_partials(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view_factory_mock + ->expects( $this->once() ) + ->method( 'create' ) + ->with( 'partial' ) + ->willReturn( new TemplatedView( + 'partial', + $view_factory_mock, + ViewHelper::LOCATIONS + ) ); + + $view = new TemplatedView( + 'view-with-partial', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + + $this->assertStringStartsWith( + 'Rendering works with partials: 42.
', + $view->render( [ 'some_value' => 42 ] ) + ); + } + + public function test_it_can_be_overridden_in_themes(): void { + $view_factory_mock = $this->createMock( ViewFactory::class ); + $view_a = new TemplatedView( + 'view-a', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + $view_b = new TemplatedView( + 'view-b', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + $view_c = new TemplatedView( + 'view-c', + $view_factory_mock, + ViewHelper::LOCATIONS + ); + + $this->assertStringStartsWith( + 'View A comes from plugin.
', + $view_a->render() + ); + $this->assertStringStartsWith( + 'View B comes from parent theme.
', + $view_b->render() + ); + $this->assertStringStartsWith( + 'View C comes from child theme.
', + $view_c->render() + ); + } +} diff --git a/tests/php/Unit/TestCase.php b/tests/php/Unit/TestCase.php new file mode 100644 index 0000000..a139bbf --- /dev/null +++ b/tests/php/Unit/TestCase.php @@ -0,0 +1,17 @@ +Hello World!