From c6079909911c83a335610fc33cb9e8fa709614df Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 22 Aug 2019 17:59:49 +0800 Subject: [PATCH] Add tests --- app/Services/OptionForm.php | 10 +-- tests/ServicesTest/OptionFormTest.php | 94 +++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 tests/ServicesTest/OptionFormTest.php diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index 1b5cc02c..02b43cd4 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -34,7 +34,7 @@ class OptionForm protected $renderWithOutTable = false; protected $renderInputTagsOnly = false; - protected $renderWithOutSubmitButton = false; + protected $renderWithoutSubmitButton = false; /** * Create a new option form instance. @@ -244,7 +244,7 @@ class OptionForm */ public function handle(callable $callback = null) { - $request = app('request'); + $request = request(); $allPostData = $request->all(); if ($request->isMethod('POST') && Arr::get($allPostData, 'option') == $this->id) { @@ -368,9 +368,9 @@ class OptionForm return $this; } - public function renderWithOutSubmitButton() + public function renderWithoutSubmitButton() { - $this->renderWithOutSubmitButton = true; + $this->renderWithoutSubmitButton = true; return $this; } @@ -387,7 +387,7 @@ class OptionForm } // attach submit button to the form - if (! $this->renderWithOutSubmitButton) { + if (! $this->renderWithoutSubmitButton) { $this->addButton([ 'style' => 'primary', 'text' => trans('general.submit'), diff --git a/tests/ServicesTest/OptionFormTest.php b/tests/ServicesTest/OptionFormTest.php new file mode 100644 index 00000000..2defba29 --- /dev/null +++ b/tests/ServicesTest/OptionFormTest.php @@ -0,0 +1,94 @@ +before(function () use (&$called) { + $called = true; + }); + + $request = request(); + $request->setMethod('POST'); + $request->merge(['option' => 'test']); + + $form->handle(); + $this->assertTrue($called); + } + + public function testHookAfter() + { + $called = false; + $form = new OptionForm('test', 'test'); + $form->after(function () use (&$called) { + $called = true; + }); + + $request = request(); + $request->setMethod('POST'); + $request->merge(['option' => 'test']); + + $form->handle(); + $this->assertTrue($called); + } + + public function testDirectHook() + { + $called = false; + $form = new OptionForm('test', 'test'); + + $request = request(); + $request->setMethod('POST'); + $request->merge(['option' => 'test']); + + $form->handle(function () use (&$called) { + $called = true; + }); + $this->assertTrue($called); + } + + public function testHookAlways() + { + $called = false; + $form = new OptionForm('test', 'test'); + $form->always(function () use (&$called) { + $called = true; + }); + + $request = request(); + $request->setMethod('POST'); + $request->merge(['option' => 'test']); + + $form->handle(); + $this->assertFalse($called); + + $form->render(); + $this->assertTrue($called); + } + + public function testRenderInputTagsOnly() + { + $form = new OptionForm('test', 'test'); + $form->text('text'); + $form->renderInputTagsOnly(); + $html = $form->render(); + $this->assertFalse(Str::contains($html, '')); + $this->assertTrue(Str::contains($html, '')); + } + + public function testRenderWithoutSubmitButton() + { + $form = new OptionForm('test', 'test'); + $form->text('text'); + $form->renderWithoutSubmitButton(); + $html = $form->render(); + $this->assertFalse(Str::contains($html, '