diff --git a/.gitignore b/.gitignore
index 59370d4..b47a5dd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,6 @@ build
vendor/
composer.phar
composer.lock
+tests/tmp/*
+!tests/tmp/.placefolder
+.phpunit.result.cache
diff --git a/.scrutinizer.yml b/.scrutinizer.yml
index 0ca2b93..9c4042c 100644
--- a/.scrutinizer.yml
+++ b/.scrutinizer.yml
@@ -1,19 +1,12 @@
+build:
+ nodes:
+ analysis:
+ tests:
+ override:
+ - php-scrutinizer-run
+ environment:
+ php:
+ version: 8.2
+
filter:
paths: ["src/*"]
-tools:
- external_code_coverage: true
- php_code_coverage:
- timeout: 1200
- php_sim: true
- php_mess_detector: true
- php_pdepend: true
- php_analyzer: true
- php_cpd: true
- php_mess_detector:
- enabled: true
- config:
- ruleset: ./phpmd.xml
- php_code_sniffer:
- enabled: true
- config:
- ruleset: ./phpcs.xml
diff --git a/README.JA.md b/README.JA.md
index b3acce9..c96343d 100644
--- a/README.JA.md
+++ b/README.JA.md
@@ -95,18 +95,14 @@ class MyController
*/
protected $contactForm;
- /**
- * @Inject
- * @Named("contact_form")
- */
+ #[Inject]
+ #[Named("contact_form")]
public function setForm(FormInterface $form)
{
$this->contactForm = $form;
}
- /**
- * @FormValidation(form="contactForm", onFailure="badRequestAction")
- */
+ #[FormValidation(form: "contactForm", onFailure: "badRequestAction")]
public function createAction()
{
// validation success
@@ -151,9 +147,7 @@ use Ray\WebFormModule\Annotation\InputValidation;
class Foo
{
- /**
- * @InputValidation(form="form1")
- */
+ #[InputValidation(form: "form1")]
public function createAction($name)
{
// ...
@@ -190,17 +184,11 @@ echo $e->error;
//}
```
-`@VndError`アノテーションで`vnd.error+json`に必要な情報を加えることができます。
+`#[VndError]`属性で`vnd.error+json`に必要な情報を加えることができます。
```php
- /**
- * @FormValidation(form="contactForm")
- * @VndError(
- * message="foo validation failed",
- * logref="a1000", path="/path/to/error",
- * href={"_self"="/path/to/error", "help"="/path/to/help"}
- * )
- */
+ #[FormValidation(form: "contactForm")]
+ #[VndError(message: "foo validation failed", logref: "a1000", path: "/path/to/error", href: ["_self" => "/path/to/error", "help" => "/path/to/help"])]
```
このオプションのモジュールはAPIアプリケーションの時に有用です。
diff --git a/README.md b/README.md
index 2265909..b07cc87 100644
--- a/README.md
+++ b/README.md
@@ -103,21 +103,18 @@ class MyController
*/
protected $contactForm;
- /**
- * @Inject
- * @Named("contact_form")
- */
+ #[Inject]
+ #[Named("contact_form")]
public function setForm(FormInterface $form)
{
$this->contactForm = $form;
}
- /**
- * @FormValidation(form="contactForm", onFailure="badRequestAction")
- */
+ #[FormValidation(form: "contactForm", onFailure: "badRequestAction")]
public function createAction()
{
// validation success
+ // More detail for `vnd.error+json` can be added with `#[VndError]`.
}
public function badRequestAction()
@@ -186,14 +183,8 @@ echo $e->error;
More detail for `vnd.error+json`can be add with `@VndError` annotation.
```php
- /**
- * @FormValidation(form="contactForm")
- * @VndError(
- * message="foo validation failed",
- * logref="a1000", path="/path/to/error",
- * href={"_self"="/path/to/error", "help"="/path/to/help"}
- * )
- */
+ #[FormValidation(form: "contactForm")]
+ #[VndError(message: "foo validation failed", logref: "a1000", path: "/path/to/error", href: ["_self" => "/path/to/error", "help" => "/path/to/help"])]
```
This optional module is handy for API application.
diff --git a/composer.json b/composer.json
index 37ad5fd..e36bd59 100644
--- a/composer.json
+++ b/composer.json
@@ -6,15 +6,16 @@
"Ray.Di module"
],
"require": {
- "php": ">=7.0.0",
- "ray/di": "^2.7",
+ "php": ">=8.0.0",
+ "ray/di": "^2.16",
+ "ray/aop": "^2.14",
"aura/input": "^1.2",
"aura/filter": "^2.3|3.x-dev",
"aura/html": "^2.5",
"ray/aura-session-module": "^1.1"
},
"require-dev": {
- "phpunit/phpunit": "^5.7.13"
+ "phpunit/phpunit": "^9.5"
},
"license": "MIT",
"autoload":{
@@ -32,5 +33,10 @@
"coverage": ["php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=./phpcs.xml src"],
"cs-fix": ["php-cs-fixer fix -v", "phpcbf src"]
+ },
+ "config": {
+ "allow-plugins": {
+ "aura/installer-default": true
+ }
}
}
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 0c37fb9..ca6b708 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,19 +1,21 @@
-
+
tests
+
+
+ src
+
+
+
+
+
+
+
-
-
-
+
-
-
-
- src
-
-
diff --git a/src/AbstractForm.php b/src/AbstractForm.php
index 1c109ab..6d9e5b2 100644
--- a/src/AbstractForm.php
+++ b/src/AbstractForm.php
@@ -1,11 +1,16 @@
toString();
- } catch (\Exception $e) {
+ } catch (Exception $e) {
trigger_error($e->getMessage() . PHP_EOL . $e->getTraceAsString(), E_USER_ERROR);
-
- return '';
}
+
+ return '';
}
/**
* @param BuilderInterface $builder
* @param FilterFactory $filterFactory
* @param HelperLocatorFactory $helperFactory
- *
- * @\Ray\Di\Di\Inject
*/
+ #[Inject]
public function setBaseDependencies(
BuilderInterface $builder,
FilterFactory $filterFactory,
HelperLocatorFactory $helperFactory
- ) {
+ ): void {
$this->builder = $builder;
$this->filter = $filterFactory->newSubjectFilter();
$this->helper = $helperFactory->newInstance();
}
- public function setAntiCsrf(AntiCsrfInterface $antiCsrf)
+ public function setAntiCsrf(AntiCsrfInterface $antiCsrf): void
{
$this->antiCsrf = $antiCsrf;
}
- /**
- * @\Ray\Di\Di\PostConstruct
- */
- public function postConstruct()
+ #[PostConstruct]
+ public function postConstruct(): void
{
$this->init();
if ($this->antiCsrf instanceof AntiCsrfInterface) {
@@ -101,18 +99,14 @@ public function postConstruct()
}
}
- /**
- * {@inheritdoc}
- */
+ /** {@inheritdoc} */
public function input($input)
{
return $this->helper->input($this->get($input));
}
- /**
- * {@inheritdoc}
- */
- public function error($input)
+ /** {@inheritdoc} */
+ public function error(string $input): string
{
if (! $this->errorMessages) {
$failure = $this->filter->getFailures();
@@ -131,12 +125,10 @@ public function error($input)
/**
* @param array $attr attributes for the form tag
*
- * @throws \Aura\Html\Exception\HelperNotFound
* @throws \Aura\Input\Exception\NoSuchInput
- *
- * @return string
+ * @throws \Aura\Html\Exception\HelperNotFound
*/
- public function form($attr = [])
+ public function form(array $attr = []): string
{
$form = $this->helper->form($attr);
if (isset($this->inputs['__csrf_token'])) {
@@ -152,14 +144,13 @@ public function form($attr = [])
* @param array $data
*
* @throws CsrfViolationException
- *
- * @return bool
*/
- public function apply(array $data)
+ public function apply(array $data): bool
{
if ($this->antiCsrf && ! $this->antiCsrf->isValid($data)) {
throw new CsrfViolationException;
}
+
$this->fill($data);
return $this->filter->apply($data);
@@ -168,20 +159,16 @@ public function apply(array $data)
/**
* Returns all failure messages for all fields.
*
- * @return array
+ * @return list
*/
- public function getFailureMessages()
+ public function getFailureMessages(): array
{
return $this->filter->getFailures()->getMessages();
}
- /**
- * Returns all the fields collection
- *
- * @return \ArrayIterator
- */
- public function getIterator()
+ /** Returns all the fields collection */
+ public function getIterator(): ArrayIterator
{
- return new \ArrayIterator($this->inputs);
+ return new ArrayIterator($this->inputs);
}
}
diff --git a/src/Annotation/AbstractValidation.php b/src/Annotation/AbstractValidation.php
index 3cf5e2f..c009216 100644
--- a/src/Annotation/AbstractValidation.php
+++ b/src/Annotation/AbstractValidation.php
@@ -1,19 +1,21 @@
$href
*
* @see http://www.w3.org/TR/html5/links.html#link-type-help
- *
- * about
* @see http://tools.ietf.org/html/rfc6903#section-2
- *
- * describes
* @see http://tools.ietf.org/html/rfc6892
*/
- public $path;
+ public function __construct(
+ public string $message,
+ public array $href,
+ public string|null $logref = null,
+ public string|null $path = null
+ ) {
+ }
}
diff --git a/src/AntiCsrf.php b/src/AntiCsrf.php
index 478ec0e..bec9e6a 100644
--- a/src/AntiCsrf.php
+++ b/src/AntiCsrf.php
@@ -1,68 +1,57 @@
session = $session;
$this->isCli = is_bool($isCli) ? $isCli : PHP_SAPI === 'cli';
}
- public function setField(Fieldset $fieldset)
+ public function setField(Fieldset $fieldset): void
{
$fieldset->setField(self::TOKEN_KEY, 'hidden')
- ->setAttribs(['value' => $this->getToken()]);
+ ->setAttribs(['value' => $this->getToken()]);
}
- /**
- * @param array $data
- *
- * @return bool
- */
- public function isValid(array $data)
+ /** @param array $data */
+ public function isValid(array $data): bool
{
if ($this->isCli) {
return true;
}
- return isset($data[self::TOKEN_KEY]) && $data[self::TOKEN_KEY] == $this->getToken();
+ return isset($data[self::TOKEN_KEY]) && $data[self::TOKEN_KEY] === $this->getToken();
}
- /**
- * @return string
- */
- private function getToken()
+ private function getToken(): string
{
- $value = $this->isCli ? self::TEST_TOKEN : $this->session->getCsrfToken()->getValue();
-
- return $value;
+ return $this->isCli ? self::TEST_TOKEN : $this->session->getCsrfToken()->getValue();
}
}
diff --git a/src/AuraInputInterceptor.php b/src/AuraInputInterceptor.php
index 16fed29..697693a 100644
--- a/src/AuraInputInterceptor.php
+++ b/src/AuraInputInterceptor.php
@@ -1,38 +1,33 @@
reader = $reader;
$this->failureHandler = $handler;
}
@@ -44,33 +39,45 @@ public function __construct(Reader $reader, FailureHandlerInterface $handler)
public function invoke(MethodInvocation $invocation)
{
$object = $invocation->getThis();
- /* @var $formValidation FormValidation */
- $method = $invocation->getMethod();
- $formValidation = $this->reader->getMethodAnnotation($method, AbstractValidation::class);
+ $formValidation = $this->getValidationAttribute($invocation->getMethod());
+ if ($formValidation === null) {
+ throw new InvalidArgumentException('The method must be attributed with #[FormValidation] or #[InputValidation]');
+ }
+
$form = $this->getFormProperty($formValidation, $object);
$data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation);
$isValid = $this->isValid($data, $form);
if ($isValid === true) {
- // validation success
return $invocation->proceed();
}
return $this->failureHandler->handle($formValidation, $invocation, $form);
}
+ private function getValidationAttribute(ReflectionMethod $method): AbstractValidation|null
+ {
+ $attributes = $method->getAttributes(AbstractValidation::class, ReflectionAttribute::IS_INSTANCEOF);
+ if ($attributes === []) {
+ return null;
+ }
+
+ $instance = $attributes[0]->newInstance();
+ assert($instance instanceof AbstractValidation);
+
+ return $instance;
+ }
+
/**
* @param array $submit
* @param AbstractForm $form
*
+ * @return bool
* @throws Exception\CsrfViolationException
*
- * @return bool
*/
- public function isValid(array $submit, AbstractForm $form)
+ public function isValid(array $submit, AbstractForm $form): bool
{
- $isValid = $form->apply($submit);
-
- return $isValid;
+ return $form->apply($submit);
}
/**
@@ -80,7 +87,7 @@ public function isValid(array $submit, AbstractForm $form)
*
* @return array
*/
- private function getNamedArguments(MethodInvocation $invocation)
+ private function getNamedArguments(MethodInvocation $invocation): array
{
$submit = [];
$params = $invocation->getMethod()->getParameters();
@@ -89,7 +96,8 @@ private function getNamedArguments(MethodInvocation $invocation)
$arg = array_shift($args);
$submit[$param->getName()] = $arg;
}
- // has token ?
+
+ // has token?
if (isset($_POST[AntiCsrf::TOKEN_KEY])) {
$submit[AntiCsrf::TOKEN_KEY] = $_POST[AntiCsrf::TOKEN_KEY];
}
@@ -110,7 +118,8 @@ private function getFormProperty(AbstractValidation $formValidation, $object)
if (! property_exists($object, $formValidation->form)) {
throw new InvalidFormPropertyException($formValidation->form);
}
- $prop = (new \ReflectionClass($object))->getProperty($formValidation->form);
+
+ $prop = (new ReflectionClass($object))->getProperty($formValidation->form);
$prop->setAccessible(true);
$form = $prop->getValue($object);
if (! $form instanceof AbstractForm) {
diff --git a/src/AuraInputModule.php b/src/AuraInputModule.php
index 738c346..d2f56d6 100644
--- a/src/AuraInputModule.php
+++ b/src/AuraInputModule.php
@@ -1,9 +1,13 @@
install(new AuraSessionModule);
- $this->bind(Reader::class)->to(AnnotationReader::class)->in(Scope::SINGLETON);
$this->bind(BuilderInterface::class)->to(Builder::class);
$this->bind(FilterInterface::class)->to(Filter::class);
$this->bind(AntiCsrfInterface::class)->to(AntiCsrf::class)->in(Scope::SINGLETON);
$this->bind(FailureHandlerInterface::class)->to(OnFailureMethodHandler::class);
- $this->bind(FailureHandlerInterface::class)->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in(Scope::SINGLETON);
+ $this->bind(FailureHandlerInterface::class)
+ ->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in(Scope::SINGLETON);
$this->bind(HelperLocatorFactory::class);
$this->bind(FilterFactory::class);
$this->bindInterceptor(
diff --git a/src/Exception/CsrfViolationException.php b/src/Exception/CsrfViolationException.php
index c1daeca..c97b0b3 100644
--- a/src/Exception/CsrfViolationException.php
+++ b/src/Exception/CsrfViolationException.php
@@ -1,9 +1,13 @@
error = $error;
diff --git a/src/FailureHandlerInterface.php b/src/FailureHandlerInterface.php
index 1da0e22..948becf 100644
--- a/src/FailureHandlerInterface.php
+++ b/src/FailureHandlerInterface.php
@@ -1,9 +1,13 @@
bind(FailureHandlerInterface::class)->to(VndErrorHandler::class);
diff --git a/src/InputValidationInterceptor.php b/src/InputValidationInterceptor.php
index 0fa677d..e59098e 100644
--- a/src/InputValidationInterceptor.php
+++ b/src/InputValidationInterceptor.php
@@ -1,24 +1,23 @@
getThis()));
}
+
$onFailureMethod = $formValidation->onFailure ?: $invocation->getMethod()->getName() . self::FAILURE_SUFFIX;
- if (! $formValidation instanceof FormValidation || ! method_exists($object, $onFailureMethod)) {
+ if (! method_exists($object, $onFailureMethod)) {
throw new InvalidOnFailureMethod(get_class($invocation->getThis()));
}
diff --git a/src/SetAntiCsrfTrait.php b/src/SetAntiCsrfTrait.php
index 2d7b289..e3fb26b 100644
--- a/src/SetAntiCsrfTrait.php
+++ b/src/SetAntiCsrfTrait.php
@@ -1,21 +1,22 @@
antiCsrf = $antiCsrf;
}
diff --git a/src/SubmitInterface.php b/src/SubmitInterface.php
index 647277d..10db621 100644
--- a/src/SubmitInterface.php
+++ b/src/SubmitInterface.php
@@ -1,9 +1,13 @@
reader = $reader;
- }
-
- /**
- * {@inheritdoc}
- */
+ /** {@inheritdoc} */
public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form)
{
unset($formValidation);
- $vndError = $this->reader->getMethodAnnotation($invocation->getMethod(), VndError::class);
+ $vndError = $this->getVndErrorAttribute($invocation->getMethod());
$error = new FormValidationError($this->makeVndError($form, $vndError));
throw new ValidationException('Validation failed.', 400, null, $error);
}
+ private function getVndErrorAttribute(ReflectionMethod $method): VndError|null
+ {
+ $attributes = $method->getAttributes(VndError::class);
+ if ($attributes === []) {
+ return null;
+ }
+
+ $instance = $attributes[0]->newInstance();
+ assert($instance instanceof VndError);
+
+ return $instance;
+ }
+
private function makeVndError(AbstractForm $form, VndError $vndError = null)
{
$body = ['message' => 'Validation failed'];
- $body['path'] = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
+ $body['path'] = $_SERVER['PATH_INFO'] ?? '';
$body['validation_messages'] = $form->getFailureMessages();
$body = $vndError ? $this->optionalAttribute($vndError) + $body : $body;
@@ -52,9 +58,11 @@ private function optionalAttribute(VndError $vndError)
if ($vndError->message) {
$body['message'] = $vndError->message;
}
+
if ($vndError->path) {
$body['path'] = $vndError->path;
}
+
if ($vndError->logref) {
$body['logref'] = $vndError->logref;
}
diff --git a/tests/AbstractAuraFormTest.php b/tests/AbstractAuraFormTest.php
index d11bdd7..16862dd 100644
--- a/tests/AbstractAuraFormTest.php
+++ b/tests/AbstractAuraFormTest.php
@@ -6,14 +6,16 @@
*/
namespace Ray\WebFormModule;
-class AbstractAuraFormTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class AbstractAuraFormTest extends TestCase
{
/**
* @var AbstractForm
*/
private $form;
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
$this->form = (new FormFactory)->newInstance(FakeForm::class);
diff --git a/tests/AbstractFormTest.php b/tests/AbstractFormTest.php
index d87ad54..86e8263 100644
--- a/tests/AbstractFormTest.php
+++ b/tests/AbstractFormTest.php
@@ -11,18 +11,19 @@
use Aura\Session\Randval;
use Aura\Session\SegmentFactory;
use Aura\Session\Session;
-use Doctrine\Common\Annotations\AnnotationReader;
+use PHPUnit\Framework\TestCase;
use Ray\Aop\ReflectiveMethodInvocation;
use Ray\WebFormModule\Exception\CsrfViolationException;
+use Ray\WebFormModule\Exception\ValidationException;
-class AbstractFormTest extends \PHPUnit_Framework_TestCase
+class AbstractFormTest extends TestCase
{
/**
* @var AbstractForm
*/
private $form;
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
$this->form = (new FormFactory)->newInstance(FakeMiniForm::class);
@@ -39,12 +40,11 @@ public function getMethodInvocation(array $arguments)
$controller = new FakeController;
$controller->setForm($fakeForm);
// interceptor
- $reader = new AnnotationReader;
- $interceptor = new AuraInputInterceptor($reader, new VndErrorHandler($reader));
+ $interceptor = new AuraInputInterceptor(new VndErrorHandler());
return new ReflectiveMethodInvocation(
$controller,
- new \ReflectionMethod($controller, 'createAction'),
+ 'createAction',
$arguments,
[
$interceptor
@@ -61,7 +61,7 @@ public function testApply()
public function testSubmit()
{
- $this->expectException(\ReflectionException::class);
+ $this->expectException(ValidationException::class);
$invocation = $this->getMethodInvocation(['na']);
$invocation->proceed();
}
@@ -89,7 +89,7 @@ public function testGetItelator()
public function testAntiCsrfViolation()
{
- $this->setExpectedException(CsrfViolationException::class);
+ $this->expectException(CsrfViolationException::class);
$session = new Session(
new SegmentFactory,
new CsrfTokenFactory(new Randval(new Phpfunc)),
diff --git a/tests/AntiCsrfTest.php b/tests/AntiCsrfTest.php
index a20bc94..ebb4297 100644
--- a/tests/AntiCsrfTest.php
+++ b/tests/AntiCsrfTest.php
@@ -14,8 +14,9 @@
use Aura\Session\Randval;
use Aura\Session\SegmentFactory;
use Aura\Session\Session;
+use PHPUnit\Framework\TestCase;
-class AntiCsrfTest extends \PHPUnit_Framework_TestCase
+class AntiCsrfTest extends TestCase
{
private $phpfunc;
@@ -29,7 +30,7 @@ class AntiCsrfTest extends \PHPUnit_Framework_TestCase
*/
private $session;
- protected function setUp()
+ protected function setUp(): void
{
$this->phpfunc = new FakePhpfunc;
$this->session = $this->newSession();
diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php
index f153f5b..18ad270 100644
--- a/tests/AuraInputInterceptorTest.php
+++ b/tests/AuraInputInterceptorTest.php
@@ -6,15 +6,14 @@
*/
namespace Ray\WebFormModule;
-use Doctrine\Common\Annotations\AnnotationReader;
-use Ray\Aop\ReflectiveMethodInvocation;
+use PHPUnit\Framework\TestCase;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\Di\InjectorInterface;
use Ray\WebFormModule\Exception\InvalidFormPropertyException;
use Ray\WebFormModule\Exception\ValidationException;
-class AuraInputInterceptorTest extends \PHPUnit_Framework_TestCase
+class AuraInputInterceptorTest extends TestCase
{
/**
* @var InjectorInterface
@@ -26,7 +25,7 @@ class AuraInputInterceptorTest extends \PHPUnit_Framework_TestCase
*/
private $controller;
- public function setUp()
+ public function setUp(): void
{
$this->injector = new Injector(new class() extends AbstractModule {
protected function configure()
@@ -94,31 +93,31 @@ public function testProceed()
$this->assertSame('201', $result);
}
- public function invalidControllerProvider()
- {
- return [
- [$this->injector->getInstance(FakeInvalidController1::class)],
- [$this->injector->getInstance(FakeInvalidController2::class)]
- ];
- }
+// public function invalidControllerProvider()
+// {
+// return [
+// [$this->injector->getInstance(FakeInvalidController1::class)],
+// [$this->injector->getInstance(FakeInvalidController2::class)]
+// ];
+// }
public function testInvalidFormPropertyByMissingProperty()
{
- $this->setExpectedException(InvalidFormPropertyException::class);
+ $this->expectException(InvalidFormPropertyException::class);
$controller = $this->injector->getInstance(FakeInvalidController1::class);
$controller->createAction();
}
public function testInvalidFormPropertyByMissingProperty2()
{
- $this->setExpectedException(InvalidFormPropertyException::class);
+ $this->expectException(InvalidFormPropertyException::class);
$controller = $this->injector->getInstance(FakeInvalidController2::class);
$controller->createAction();
}
public function testInvalidFormPropertyException()
{
- $this->setExpectedException(InvalidFormPropertyException::class);
+ $this->expectException(InvalidFormPropertyException::class);
/** @var FakeInvalidController3 $controller */
$controller = $this->injector->getInstance(FakeInvalidController3::class);
$controller->createAction('');
@@ -126,24 +125,25 @@ public function testInvalidFormPropertyException()
public function testInvalidFormPropertyByInvalidInstance()
{
- $this->setExpectedException(InvalidFormPropertyException::class);
- $this->setExpectedException(InvalidFormPropertyException::class);
+ $this->expectException(InvalidFormPropertyException::class);
$controller = $this->injector->getInstance(FakeInvalidController1::class);
$controller->createAction('');
}
public function testProceedWithVndErrorHandler()
{
- /** @var FakeController $controller */
- $controller = $this->injector->getInstance(FakeController::class);
+ /** @var FakeControllerVndError $controller */
+ $controller = $this->injector->getInstance(FakeControllerVndError::class);
try {
$controller->createAction('');
+ $this->fail('ValidationException should be thrown');
} catch (ValidationException $e) {
$this->assertInstanceOf(FormValidationError::class, $e->error);
$json = (string) $e->error;
$this->assertSame('{
- "message": "Validation failed",
- "path": "",
+ "message": "foo validation failed",
+ "path": "/path/to/error",
+ "logref": "a1000",
"validation_messages": {
"name": [
"Name must be alphabetic only."
diff --git a/tests/AuraInputModuleTest.php b/tests/AuraInputModuleTest.php
index 2d51b2b..47add84 100644
--- a/tests/AuraInputModuleTest.php
+++ b/tests/AuraInputModuleTest.php
@@ -6,11 +6,12 @@
*/
namespace Ray\WebFormModule;
+use PHPUnit\Framework\TestCase;
use Ray\Aop\WeavedInterface;
use Ray\Di\Injector;
use Ray\WebFormModule\Exception\ValidationException;
-class AuraInputModuleTest extends \PHPUnit_Framework_TestCase
+class AuraInputModuleTest extends TestCase
{
public function testAuraInputModule()
{
@@ -21,7 +22,7 @@ public function testAuraInputModule()
public function testExceptionOnFailure()
{
- $this->setExpectedException(ValidationException::class);
+ $this->expectException(ValidationException::class);
$injector = new Injector(new FakeModule, __DIR__ . '/tmp');
/** @var $controller FakeInputValidationController */
$controller = $injector->getInstance(FakeInputValidationController::class);
diff --git a/tests/Fake/FakeController.php b/tests/Fake/FakeController.php
index d642e52..9484301 100644
--- a/tests/Fake/FakeController.php
+++ b/tests/Fake/FakeController.php
@@ -13,20 +13,14 @@ class FakeController
*/
protected $form;
- /**
- * @Inject
- * @Named("contact_form")
- */
+ #[Inject]
+ #[Named('contact_form')]
public function setForm(FormInterface $form)
{
$this->form = $form;
}
- /**
- * @FormValidation
- *
- * = is same as @ FormValidation(form="form", onFailure="createActionValidationFailed")
- */
+ #[FormValidation]
public function createAction($name)
{
return '201';
diff --git a/tests/Fake/FakeControllerVndError.php b/tests/Fake/FakeControllerVndError.php
index 3f44ab5..d952c87 100644
--- a/tests/Fake/FakeControllerVndError.php
+++ b/tests/Fake/FakeControllerVndError.php
@@ -14,24 +14,15 @@ class FakeControllerVndError
*/
protected $form1;
- /**
- * @Inject
- * @Named("contact_form")
- */
+ #[Inject]
+ #[Named('contact_form')]
public function setForm(FormInterface $form)
{
$this->form1 = $form;
}
- /**
- * @InputValidation(form="form1")
- * @VndError(
- * message="foo validation failed",
- * logref="a1000",
- * path="/path/to/error",
- * href={"_self"="/path/to/error", "help"="/path/to/help"}
- * )
- */
+ #[InputValidation(form: 'form1')]
+ #[VndError(message: 'foo validation failed', href: ['_self' => '/path/to/error', 'help' => '/path/to/help'], logref: 'a1000', path: '/path/to/error')]
public function createAction($name)
{
}
diff --git a/tests/Fake/FakeInputValidationController.php b/tests/Fake/FakeInputValidationController.php
index 9619276..162ad83 100644
--- a/tests/Fake/FakeInputValidationController.php
+++ b/tests/Fake/FakeInputValidationController.php
@@ -13,18 +13,14 @@ class FakeInputValidationController
*/
protected $form;
- /**
- * @Inject
- * @Named("contact_form")
- */
+ #[Inject]
+ #[Named('contact_form')]
public function setForm(FormInterface $form)
{
$this->form = $form;
}
- /**
- * @InputValidation
- */
+ #[InputValidation]
public function createAction($name)
{
}
diff --git a/tests/Fake/FakeInvalidController1.php b/tests/Fake/FakeInvalidController1.php
index 32aa3dc..122fb70 100644
--- a/tests/Fake/FakeInvalidController1.php
+++ b/tests/Fake/FakeInvalidController1.php
@@ -6,9 +6,7 @@
class FakeInvalidController1
{
- /**
- * @FormValidation(form="missing")
- */
+ #[FormValidation(form: "missing")]
public function createAction()
{
}
diff --git a/tests/Fake/FakeInvalidController2.php b/tests/Fake/FakeInvalidController2.php
index 9f6bb47..711449c 100644
--- a/tests/Fake/FakeInvalidController2.php
+++ b/tests/Fake/FakeInvalidController2.php
@@ -8,9 +8,7 @@ class FakeInvalidController2
{
private $form = null;
- /**
- * @FormValidation
- */
+ #[FormValidation]
public function createAction()
{
}
diff --git a/tests/Fake/FakeInvalidController3.php b/tests/Fake/FakeInvalidController3.php
index 8322619..be70fac 100644
--- a/tests/Fake/FakeInvalidController3.php
+++ b/tests/Fake/FakeInvalidController3.php
@@ -13,9 +13,7 @@ public function setForm(FormInterface $form)
$this->form = $form;
}
- /**
- * @FormValidation(onFailure="missing_method")
- */
+ #[FormValidation(onFailure: "missing_method")]
public function createAction($name)
{
}
diff --git a/tests/Fake/FakeInvalidInstanceController.php b/tests/Fake/FakeInvalidInstanceController.php
index d64b9c6..5f93b9d 100644
--- a/tests/Fake/FakeInvalidInstanceController.php
+++ b/tests/Fake/FakeInvalidInstanceController.php
@@ -8,9 +8,7 @@ class FakeInvalidInstanceController
{
private $form;
- /**
- * @FormValidation
- */
+ #[FormValidation]
public function createAction()
{
}
diff --git a/tests/FormFactoryTest.php b/tests/FormFactoryTest.php
index 07451e3..c8ae411 100644
--- a/tests/FormFactoryTest.php
+++ b/tests/FormFactoryTest.php
@@ -6,14 +6,16 @@
*/
namespace Ray\WebFormModule;
-class FormFactoryTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class FormFactoryTest extends TestCase
{
/**
* @var FormFactory
*/
private $factory;
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
$this->factory = new FormFactory;
diff --git a/tests/VndErrorHandlerTest.php b/tests/VndErrorHandlerTest.php
index 80519d7..2b96fb8 100644
--- a/tests/VndErrorHandlerTest.php
+++ b/tests/VndErrorHandlerTest.php
@@ -6,17 +6,18 @@
*/
namespace Ray\WebFormModule;
+use PHPUnit\Framework\TestCase;
use Ray\Di\Injector;
use Ray\WebFormModule\Exception\ValidationException;
-class VndErrorHandlerTest extends \PHPUnit_Framework_TestCase
+class VndErrorHandlerTest extends TestCase
{
/**
* @var FakeController
*/
private $controller;
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
$this->controller = (new Injector(new FakeVndErrorModule, __DIR__ . '/tmp'))->getInstance(FakeController::class);
@@ -24,7 +25,7 @@ public function setUp()
public function testValidationException()
{
- $this->setExpectedException(ValidationException::class);
+ $this->expectException(ValidationException::class);
$this->controller->createAction('');
}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 29ca915..adacf3f 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -4,11 +4,7 @@
*
* @license http://opensource.org/licenses/MIT MIT
*/
-use Doctrine\Common\Annotations\AnnotationRegistry;
-
-/* @var $loader \Composer\Autoload\ClassLoader */
-$loader = require dirname(__DIR__) . '/vendor/autoload.php';
-AnnotationRegistry::registerLoader([$loader, 'loadClass']);
+require dirname(__DIR__) . '/vendor/autoload.php';
$handler = new \Ray\WebFormModule\FakeSessionHandler();
session_set_save_handler(