From 575122cc855fb608785fb4611047e714d26b64aa Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 02:00:08 +0900 Subject: [PATCH 01/10] prefer setter injection in base class user class can use constructor freely. --- src/AbstractForm.php | 11 +++++++---- tests/AbstractFormTest.php | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/AbstractForm.php b/src/AbstractForm.php index d316b65..96ea82c 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -13,10 +13,7 @@ use Aura\Input\AntiCsrfInterface; use Aura\Input\Builder; use Aura\Input\BuilderInterface; -use Aura\Input\Filter; -use Aura\Input\FilterInterface; use Aura\Input\Form; -use Ray\Di\Di\Inject; use Ray\Di\Di\PostConstruct; abstract class AbstractForm extends Form implements FormInterface @@ -35,8 +32,10 @@ abstract class AbstractForm extends Form implements FormInterface * @param BuilderInterface $builder * @param FilterFactory $filterFactory * @param HelperLocatorFactory $helperFactory + * + * @\Ray\Di\Di\Inject */ - public function __construct( + public function setBaseDependencies( BuilderInterface $builder = null, FilterFactory $filterFactory = null, HelperLocatorFactory $helperFactory = null @@ -46,6 +45,10 @@ public function __construct( $this->helper = $helperFactory ? $helperFactory->newInstance() : (new HelperLocatorFactory)->newInstance(); } + public function __construct() + { + } + /** * @PostConstruct */ diff --git a/tests/AbstractFormTest.php b/tests/AbstractFormTest.php index ff2620d..d600a2f 100644 --- a/tests/AbstractFormTest.php +++ b/tests/AbstractFormTest.php @@ -16,7 +16,8 @@ class AbstractFormTest extends \PHPUnit_Framework_TestCase public function setUp() { parent::setUp(); - $this->form = new FakeNameForm(new Builder, new FilterFactory, new HelperLocatorFactory); + $this->form = new FakeNameForm; + $this->form->setBaseDependencies(new Builder, new FilterFactory, new HelperLocatorFactory); } public function testSubmit() From 47b2ee96cab6b0f82a6fa77e8f00d58265bbd264 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 02:17:26 +0900 Subject: [PATCH 02/10] empty constructor for AbstractAuraForm same refactor for previous commit --- src/AbstractAuraForm.php | 25 ++++++++++++++----------- tests/AbstractAuraFormTest.php | 4 +++- tests/AuraInputInterceptorTest.php | 19 ++++++++++++++++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/AbstractAuraForm.php b/src/AbstractAuraForm.php index 2e536e5..b175987 100644 --- a/src/AbstractAuraForm.php +++ b/src/AbstractAuraForm.php @@ -18,14 +18,13 @@ abstract class AbstractAuraForm extends Form implements FormInterface { /** - * @\Ray\Di\Di\Inject - * * @param BuilderInterface $builder An object to build input objects. * @param FilterInterface $filter A filter object for this fieldset. * @param object $options An arbitrary options object for use when setting * up inputs and filters. + * @\Ray\Di\Di\Inject */ - public function parentConstruct( + public function setBaseDependencies( BuilderInterface $builder, FilterInterface $filter, $options = null @@ -35,6 +34,18 @@ public function parentConstruct( $this->options = $options; } + /** + * @Inject + */ + public function setFormHelper(HelperLocatorFactory $factory) + { + $this->helper = $factory->newInstance(); + } + + public function __construct() + { + } + /** * @PostConstruct */ @@ -55,14 +66,6 @@ public function postConstruct() */ protected $string = '
'; - /** - * @Inject - */ - public function setFormHelper(HelperLocatorFactory $factory) - { - $this->helper = $factory->newInstance(); - } - /** * @param AntiCsrfInterface $antiCsrf */ diff --git a/tests/AbstractAuraFormTest.php b/tests/AbstractAuraFormTest.php index 7574651..0b851ef 100644 --- a/tests/AbstractAuraFormTest.php +++ b/tests/AbstractAuraFormTest.php @@ -16,7 +16,9 @@ class AbstractAuraFormTest extends \PHPUnit_Framework_TestCase public function setUp() { parent::setUp(); - $this->form = new FakeForm(new Builder, new Filter); + $this->form = new FakeForm; + $this->form->setBaseDependencies(new Builder, new Filter); + $this->form->postConstruct(); $this->form->setFormHelper(new HelperLocatorFactory); } diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php index 96dbb87..a04ab56 100644 --- a/tests/AuraInputInterceptorTest.php +++ b/tests/AuraInputInterceptorTest.php @@ -45,14 +45,26 @@ public function getMethodInvocation($method, array $submit, FailureHandlerInterf public function getController(array $submit) { $controller = new FakeController; - $fakeForm = new FakeForm(new Builder, new Filter); + $fakeForm = $this->getFakeForm(); $fakeForm->setSubmit($submit); - $fakeForm->setFormHelper(new HelperLocatorFactory); $controller->setForm($fakeForm); return $controller; } + /** + * @return FakeForm + */ + private function getFakeForm() + { + $fakeForm = new FakeForm; + $fakeForm->setBaseDependencies(new Builder ,new Filter); + $fakeForm->setFormHelper(new HelperLocatorFactory); + $fakeForm->postConstruct(); + + return $fakeForm; + } + public function proceed($controller) { $invocation = new ReflectiveMethodInvocation( @@ -103,7 +115,8 @@ public function testInvalidFormPropertyException() { $this->setExpectedException(InvalidOnFailureMethod::class); $controller = new FakeInvalidController3; - $controller->setForm(new FakeForm(new Builder, new Filter)); + $fakeForm = $this->getFakeForm(); + $controller->setForm($fakeForm); $this->proceed($controller); } From 6f1b2acb62a34b4220aff182ff7f7e5dcf410e70 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 02:24:23 +0900 Subject: [PATCH 03/10] fix phpcs --- docs/demo/1.csrf/run.php | 1 - tests/AuraInputInterceptorTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/demo/1.csrf/run.php b/docs/demo/1.csrf/run.php index a10ee6e..c172253 100644 --- a/docs/demo/1.csrf/run.php +++ b/docs/demo/1.csrf/run.php @@ -20,4 +20,3 @@ echo 'Anti CSRF DOES NOT works !' . PHP_EOL; //$works = $controller->response['body'] == 'create bear'; //echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; - diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php index a04ab56..9a8dca8 100644 --- a/tests/AuraInputInterceptorTest.php +++ b/tests/AuraInputInterceptorTest.php @@ -58,7 +58,7 @@ public function getController(array $submit) private function getFakeForm() { $fakeForm = new FakeForm; - $fakeForm->setBaseDependencies(new Builder ,new Filter); + $fakeForm->setBaseDependencies(new Builder, new Filter); $fakeForm->setFormHelper(new HelperLocatorFactory); $fakeForm->postConstruct(); From aebfddab66d790ff97ef111f71501ab8f9dfd3a4 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 02:37:06 +0900 Subject: [PATCH 04/10] FQDN @Inject annotation for hhvm --- src/AbstractAuraForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AbstractAuraForm.php b/src/AbstractAuraForm.php index b175987..09931b9 100644 --- a/src/AbstractAuraForm.php +++ b/src/AbstractAuraForm.php @@ -35,7 +35,7 @@ public function setBaseDependencies( } /** - * @Inject + * @\Ray\Di\Di\Inject */ public function setFormHelper(HelperLocatorFactory $factory) { From 24510320a499fbd3d45168135a17535003b4f48f Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 02:42:49 +0900 Subject: [PATCH 05/10] unite dependency setter --- src/AbstractAuraForm.php | 9 ++------- tests/AbstractAuraFormTest.php | 3 +-- tests/AuraInputInterceptorTest.php | 3 +-- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/AbstractAuraForm.php b/src/AbstractAuraForm.php index 09931b9..2d01a97 100644 --- a/src/AbstractAuraForm.php +++ b/src/AbstractAuraForm.php @@ -27,19 +27,14 @@ abstract class AbstractAuraForm extends Form implements FormInterface public function setBaseDependencies( BuilderInterface $builder, FilterInterface $filter, + HelperLocatorFactory $factory, $options = null ) { $this->builder = $builder; $this->filter = $filter; $this->options = $options; - } - - /** - * @\Ray\Di\Di\Inject - */ - public function setFormHelper(HelperLocatorFactory $factory) - { $this->helper = $factory->newInstance(); + } public function __construct() diff --git a/tests/AbstractAuraFormTest.php b/tests/AbstractAuraFormTest.php index 0b851ef..e3d5c26 100644 --- a/tests/AbstractAuraFormTest.php +++ b/tests/AbstractAuraFormTest.php @@ -17,9 +17,8 @@ public function setUp() { parent::setUp(); $this->form = new FakeForm; - $this->form->setBaseDependencies(new Builder, new Filter); + $this->form->setBaseDependencies(new Builder, new Filter, new HelperLocatorFactory); $this->form->postConstruct(); - $this->form->setFormHelper(new HelperLocatorFactory); } public function testForm() diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php index 9a8dca8..61afbd7 100644 --- a/tests/AuraInputInterceptorTest.php +++ b/tests/AuraInputInterceptorTest.php @@ -58,8 +58,7 @@ public function getController(array $submit) private function getFakeForm() { $fakeForm = new FakeForm; - $fakeForm->setBaseDependencies(new Builder, new Filter); - $fakeForm->setFormHelper(new HelperLocatorFactory); + $fakeForm->setBaseDependencies(new Builder, new Filter, new HelperLocatorFactory); $fakeForm->postConstruct(); return $fakeForm; From 89590337425400cbd3acf6d3cb4142b0528cd2e2 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 03:00:15 +0900 Subject: [PATCH 06/10] FQDN @PostConstruct annotation --- src/AbstractAuraForm.php | 3 +-- src/AbstractForm.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/AbstractAuraForm.php b/src/AbstractAuraForm.php index 2d01a97..9890f78 100644 --- a/src/AbstractAuraForm.php +++ b/src/AbstractAuraForm.php @@ -34,7 +34,6 @@ public function setBaseDependencies( $this->filter = $filter; $this->options = $options; $this->helper = $factory->newInstance(); - } public function __construct() @@ -42,7 +41,7 @@ public function __construct() } /** - * @PostConstruct + * @\Ray\Di\Di\PostConstruct */ public function postConstruct() { diff --git a/src/AbstractForm.php b/src/AbstractForm.php index 96ea82c..212c42e 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -14,7 +14,6 @@ use Aura\Input\Builder; use Aura\Input\BuilderInterface; use Aura\Input\Form; -use Ray\Di\Di\PostConstruct; abstract class AbstractForm extends Form implements FormInterface { @@ -50,7 +49,7 @@ public function __construct() } /** - * @PostConstruct + * @\Ray\Di\Di\PostConstruct */ public function postConstruct() { From 3eb9cbef17afc7f733eb2011b14a70414b847ed9 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 03:11:10 +0900 Subject: [PATCH 07/10] injector tmp dir for tests --- tests/AuraInputModuleTest.php | 2 +- tests/VndErrorHandlerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/AuraInputModuleTest.php b/tests/AuraInputModuleTest.php index 0ccb122..a667feb 100644 --- a/tests/AuraInputModuleTest.php +++ b/tests/AuraInputModuleTest.php @@ -23,7 +23,7 @@ public function setUp() public function testAuraInputModule() { - $injector = new Injector(new FakeModule); + $injector = new Injector(new FakeModule, __DIR__ . '/tmp'); $controller = $injector->getInstance(FakeController::class); $this->assertInstanceOf(WeavedInterface::class, $controller); } diff --git a/tests/VndErrorHandlerTest.php b/tests/VndErrorHandlerTest.php index 67bb0ce..d23709d 100644 --- a/tests/VndErrorHandlerTest.php +++ b/tests/VndErrorHandlerTest.php @@ -15,7 +15,7 @@ class VndErrorHandlerTest extends \PHPUnit_Framework_TestCase public function setUp() { parent::setUp(); - $this->controller = (new Injector(new FakeVndErrorModule))->getInstance(FakeController::class); + $this->controller = (new Injector(new FakeVndErrorModule, __DIR__ . '/tmp'))->getInstance(FakeController::class); } public function testValidationException() From 1c6a3ec9860ade8e20aa58f2a73be2f14dc6470c Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 03:22:24 +0900 Subject: [PATCH 08/10] replace trait --- tests/Fake/FakeForm.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/Fake/FakeForm.php b/tests/Fake/FakeForm.php index 2c854b9..9a58b1b 100644 --- a/tests/Fake/FakeForm.php +++ b/tests/Fake/FakeForm.php @@ -2,11 +2,20 @@ namespace Ray\WebFormModule; +use Aura\Input\AntiCsrfInterface; use Aura\Input\Filter; class FakeForm extends AbstractAuraForm { - use SetAntiCsrfTrait; + /** + * @param AntiCsrfInterface $antiCsrf + * + * @\Ray\Di\Di\Inject + */ + public function injectAntiCsrf(AntiCsrfInterface $antiCsrf) + { + $this->setAntiCsrf($antiCsrf); + } /** * @var array From daf8ffdafd22c8d73e95549fd0ed174f1ef69f6c Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 03:32:23 +0900 Subject: [PATCH 09/10] Revert "replace trait" This reverts commit 1c6a3ec9860ade8e20aa58f2a73be2f14dc6470c. --- tests/Fake/FakeForm.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/Fake/FakeForm.php b/tests/Fake/FakeForm.php index 9a58b1b..2c854b9 100644 --- a/tests/Fake/FakeForm.php +++ b/tests/Fake/FakeForm.php @@ -2,20 +2,11 @@ namespace Ray\WebFormModule; -use Aura\Input\AntiCsrfInterface; use Aura\Input\Filter; class FakeForm extends AbstractAuraForm { - /** - * @param AntiCsrfInterface $antiCsrf - * - * @\Ray\Di\Di\Inject - */ - public function injectAntiCsrf(AntiCsrfInterface $antiCsrf) - { - $this->setAntiCsrf($antiCsrf); - } + use SetAntiCsrfTrait; /** * @var array From 2b1c48be6018a65d3324bf285f7da82077af255d Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 11 Sep 2015 03:35:33 +0900 Subject: [PATCH 10/10] set AntiCsrf on post construct --- src/AbstractAuraForm.php | 3 +++ src/AbstractForm.php | 3 +++ src/SetAntiCsrfTrait.php | 7 ++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/AbstractAuraForm.php b/src/AbstractAuraForm.php index 9890f78..3886744 100644 --- a/src/AbstractAuraForm.php +++ b/src/AbstractAuraForm.php @@ -46,6 +46,9 @@ public function __construct() public function postConstruct() { $this->init(); + if ($this->antiCsrf instanceof AntiCsrfInterface) { + $this->setAntiCsrf($this->antiCsrf); + } } /** diff --git a/src/AbstractForm.php b/src/AbstractForm.php index 212c42e..7ac4998 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -54,6 +54,9 @@ public function __construct() public function postConstruct() { $this->init(); + if ($this->antiCsrf instanceof AntiCsrfInterface) { + $this->setAntiCsrf($this->antiCsrf); + } } /** diff --git a/src/SetAntiCsrfTrait.php b/src/SetAntiCsrfTrait.php index b70d636..528b0db 100644 --- a/src/SetAntiCsrfTrait.php +++ b/src/SetAntiCsrfTrait.php @@ -10,6 +10,11 @@ trait SetAntiCsrfTrait { + /** + * @var AntiCsrfInterface + */ + protected $antiCsrf; + /** * @param AntiCsrfInterface $antiCsrf * @@ -17,6 +22,6 @@ trait SetAntiCsrfTrait */ public function injectAntiCsrf(AntiCsrfInterface $antiCsrf) { - $this->setAntiCsrf($antiCsrf); + $this->antiCsrf = $antiCsrf; } }