From 641729aafa14e4c60c0ff5742b5b9d26b9221bc1 Mon Sep 17 00:00:00 2001 From: Iman Ghafoori Date: Tue, 12 Dec 2023 16:55:32 +0330 Subject: [PATCH] fix tests --- composer.json | 4 +- phpunit.xml.dist | 59 ++++------- src/Utils/WidgetJsonifier.php | 4 +- tests/SlotWidgetTest.php | 30 +++++- tests/TestCase.php | 4 +- tests/WidgetCacheTest.php | 87 +++++++++++++++- tests/WidgetMinificationTest.php | 15 ++- tests/WidgetTest.php | 102 +++++++++++++++++-- tests/test_stubs.php | 170 ------------------------------- 9 files changed, 249 insertions(+), 226 deletions(-) delete mode 100644 tests/test_stubs.php diff --git a/composer.json b/composer.json index 0f15dda..5fb269b 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ "laravel/framework":"~5.1|6.*|7.*|8.*|9.*|10.*" }, "autoload-dev": { - "classmap": ["tests/TestCase.php"] + "psr-4": { + "Tests\\": "tests" + } }, "require-dev": { "orchestra/testbench": "~3.0|~4.0|~5.0|~6.0|~7.0|~8.0", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8369c88..9aac3a2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,40 +1,25 @@ - - - - ./tests/ - - - - - - - - - - - - - - - - ./src/Utils - - - - ./src/vendor - - + + + ./tests/ + + + + + + + diff --git a/src/Utils/WidgetJsonifier.php b/src/Utils/WidgetJsonifier.php index e7638ef..23a4e0b 100644 --- a/src/Utils/WidgetJsonifier.php +++ b/src/Utils/WidgetJsonifier.php @@ -34,7 +34,9 @@ public function jsonResponse($widget, ...$args) */ private function makeWidgetObj(string $widget) { - $widget = app()->getNamespace().'Widgets\\'.$widget; + if ($widget[0] !== '\\') { + $widget = app()->getNamespace().'Widgets\\'.$widget; + } return app($widget); } diff --git a/tests/SlotWidgetTest.php b/tests/SlotWidgetTest.php index f72554e..b770306 100644 --- a/tests/SlotWidgetTest.php +++ b/tests/SlotWidgetTest.php @@ -1,9 +1,10 @@ controller = 'Widget4Ctrl@meta'; + $widget = new Widget4Slot(); + $widget->controller = 'Tests\Widget764Ctrl@meta'; $renderer->renderWidget($widget, ['arg1' => 'a', 'arg2' => 'bb']); } @@ -103,8 +104,27 @@ public function test_widget_with_many_slots() } //act - $widget = new Widget4(); - $widget->controller = 'Widget4Ctrl@meta'; + $widget = new Widget4Slot(); + $widget->controller = 'Tests\Widget764Ctrl@meta'; $renderer->renderWidget($widget, ['arg1' => 'a', 'arg2' => 'bb']); } } + +class Widget4Slot +{ + public $template = 'hello'; + public $controller = 'Widget764Ctrl'; +} + +class Widget764Ctrl +{ + public function data($arg1, $arg2) + { + return $arg1.$arg2; + } + + public function meta($arg1, $arg2) + { + return $arg2.$arg1.$arg1; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 9b0d287..699ace4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,6 +1,8 @@ once()->andReturn('foo'); //act - $widget = new Widget1(); + $widget = new Widget123(); $result1 = render_widget($widget); $result2 = render_widget($widget); $result3 = render_widget($widget); @@ -67,7 +71,7 @@ public function test_caches_the_result_of_controller_method_and_views() View::shouldReceive('exists')->once()->andReturn(true); //act - $widget = new Widget1(); + $widget = new Widget123(); $result1 = render_widget($widget); $result2 = render_widget($widget); $result3 = render_widget($widget); @@ -196,7 +200,7 @@ public function test_the_view_and_controller_are_rendered_only_once_when_cache_i \App::shouldReceive('call')->times(5)->andReturn('foo'); //act - $widget = new Widget1(); + $widget = new Widget123(); $result1 = render_widget($widget); $result2 = render_widget($widget); $widget->cacheView = false; @@ -210,3 +214,78 @@ public function test_the_view_and_controller_are_rendered_only_once_when_cache_i $this->assertEquals($widget->cacheLifeTime->s, 1); } } + +class ForeverWidget +{ + public $cacheLifeTime = -1; + public $template = 'hello'; + + public function data() + { + } +} + +class ForeverWidget2 +{ + public $cacheLifeTime = 'forever'; + public $template = 'hello'; + + public function data() + { + } +} + +class CustomCacheKeyWidget +{ + public $cacheLifeTime = -1; + public $template = 'hello'; + + public function data() + { + } + + public function cacheKey() + { + return 'abcde'; + } +} + +class TaggedWidget +{ + public $template = 'hello'; + public $cacheTags = ['t1', 't2']; + + public function data() + { + } +} + +class Widget123 +{ + public $template = 'hello'; + + public function data() + { + } +} + +class Widget8 +{ + public $template = 'hello'; + + public $cacheView = false; + + public function data() + { + } +} + +class ZeroLifeTimeWidget +{ + public $cacheLifeTime = 0; + public $template = 'hello'; + + public function data() + { + } +} \ No newline at end of file diff --git a/tests/WidgetMinificationTest.php b/tests/WidgetMinificationTest.php index 7669d20..48510be 100644 --- a/tests/WidgetMinificationTest.php +++ b/tests/WidgetMinificationTest.php @@ -1,6 +1,9 @@ assertEquals($minified, $html); } } + + +class Widget7 +{ + public $template = 'hello'; + + public function data() + { + } +} \ No newline at end of file diff --git a/tests/WidgetTest.php b/tests/WidgetTest.php index ac3c130..db692df 100644 --- a/tests/WidgetTest.php +++ b/tests/WidgetTest.php @@ -1,6 +1,9 @@ once()->andReturn(true); - View::shouldReceive('make')->once()->with('Widgets::Foo.Widget1View', ['data' => 'foo', 'params' => null], [])->andReturn(app('view')); + View::shouldReceive('make')->once()->with('Widgets::Tests.Widget1View', ['data' => 'foo', 'params' => null], [])->andReturn(app('view')); View::shouldReceive('render')->once()->andReturn('
'); \App::shouldReceive('call')->once()->andReturn('foo'); //act - $widget = new \App\Widgets\Foo\Widget1(); + $widget = new Widget1(); render_widget($widget); } @@ -58,7 +61,7 @@ public function test_controller_method_is_called_on_some_other_class_2() //act $widget = new Widget4(); - $widget->controller = 'Widget4Ctrl@meta'; + $widget->controller = 'Tests\Widget4Ctrl@meta'; render_widget($widget, ['arg1' => 'a', 'arg2' => 'bb']); } @@ -90,7 +93,7 @@ public function test_data_is_passed_to_data_method_from_view_as_string() View::shouldReceive('make')->once()->with('hello', ['data' => '222111', 'params' => ['foo' => '111', 'bar' => '222']], [])->andReturn(app('view')); View::shouldReceive('render')->once()->andReturn('
'); //act - render_widget('Foo\Widget6', ['foo' => '111', 'bar' => '222']); + render_widget('\Tests\Widget6', ['foo' => '111', 'bar' => '222']); } public function test_json_widgets() @@ -108,7 +111,94 @@ public function test_json_widgets_as_string() Response::shouldReceive('json')->once()->with('222111', 200)->andReturn('123'); View::shouldReceive('exists')->once()->andReturn(true); //act - $a = json_widget('Foo\Widget6', ['foo' => '111', 'bar' => '222']); + $a = json_widget('\Tests\Widget6', ['foo' => '111', 'bar' => '222']); $this->assertEquals('123', $a); } } + + +class Widget6 +{ + public $template = 'hello'; + + public function data($foo, $bar) + { + return $bar.$foo; + } +} + +class Widget5 +{ + public $template = 'hello'; + public $presenter = 'Tests\Widget5Presenter'; + + public function data() + { + return 'foo'; + } +} + +class Widget4 +{ + public $template = 'hello'; + public $controller = 'Tests\Widget4Ctrl'; +} + +class Widget13 +{ + public $template = 'hello'; + + public function data() + { + } +} + + +class Widget66 +{ + public $template = 'hello'; + + public function data($foo, $bar) + { + return $bar.$foo; + } +} + +class Widget4Ctrl +{ + public function data($arg1, $arg2) + { + return $arg1.$arg2; + } + + public function meta($arg1, $arg2) + { + return $arg2.$arg1.$arg1; + } +} + +class Widget3 +{ + public $template = 'hello'; + public $contextAs = '$myData'; + + public function data() + { + } +} + +class Widget1 +{ + public function data() + { + } +} + + +class Widget5Presenter +{ + public function present($data) + { + return 'bar'.$data; + } +} \ No newline at end of file diff --git a/tests/test_stubs.php b/tests/test_stubs.php deleted file mode 100644 index 39c359d..0000000 --- a/tests/test_stubs.php +++ /dev/null @@ -1,170 +0,0 @@ -