Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
missing tests, check for parameter in update
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 committed Jan 22, 2018
1 parent bf75264 commit 624abd9
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Form Annotation Bundle
============

[![Build Status](https://travis-ci.org/kunicmarko20/FormAnnotationBundle.svg?branch=master)](https://travis-ci.org/kunicmarko20/FormAnnotationBundle)
[![StyleCI](https://styleci.io/repos/118287935/shield?branch=master)](https://styleci.io/repos/118287935)
[![Coverage Status](https://coveralls.io/repos/github/kunicmarko20/FormAnnotationBundle/badge.svg)](https://coveralls.io/github/kunicmarko20/FormAnnotationBundle)

Adds Form Annotations that helps you avoid boilerplate code when using forms.
Expand Down
10 changes: 4 additions & 6 deletions src/EventListener/CreateFormAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ private function getReflectionParameterType(\ReflectionParameter $reflectionPara
if (!($type = $reflectionParameter->getType())) {
throw new \InvalidArgumentException(
sprintf(
'Parameter "%s" in "%s" method is missing a type.',
$parameter,
$this->reflectionMethod->getName()
'Parameter "%s" is missing a type.',
$parameter
)
);
}
Expand All @@ -103,9 +102,8 @@ private function getReflectionParameter(string $parameter) : \ReflectionParamete

throw new \InvalidArgumentException(
sprintf(
'Parameter "%s" not found in "%s" method.',
$parameter,
$this->reflectionMethod->getName()
'Parameter "%s" not found.',
$parameter
)
);
}
Expand Down
19 changes: 18 additions & 1 deletion src/EventListener/UpdateFormAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,25 @@ protected function createForm(AbstractFormAnnotation $annotation): FormInterface
{
return $this->formFactory->create(
$annotation->formType,
$this->request->attributes->get($annotation->parameter),
$this->getObject($annotation),
['method' => $annotation->getMethod()]
);
}

/**
* @throws \InvalidArgumentException
*/
private function getObject(AbstractFormAnnotation $annotation)
{
if ($object = $this->request->attributes->get($annotation->parameter)) {
return $object;
}

throw new \InvalidArgumentException(
sprintf(
'Parameter "%s" not found.',
$annotation->parameter
)
);
}
}
167 changes: 152 additions & 15 deletions tests/EventListener/CreateFormAnnotationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
use Doctrine\Common\Annotations\Reader;
use KunicMarko\FormAnnotationBundle\Annotation\Form;
use KunicMarko\FormAnnotationBundle\EventListener\CreateFormAnnotationListener;
use KunicMarko\FormAnnotationBundle\Tests\Fixtures\FakeClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

Expand All @@ -29,7 +33,7 @@ public function testNotSupported($annotation)
$requestStack
);

$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent());
$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent('testBlank'));
}

public function getNotSupportedAnnotations()
Expand All @@ -54,19 +58,18 @@ private function mockReader($returnValue = null)
return $reader;
}

private function mockFilterControllerEvent()
private function mockFilterControllerEvent(string $method)
{
$event = $this->createMock(FilterControllerEvent::class);

$event->expects($this->once())
->method('getController')
->willReturn([
new class() {
public function test()
{
}
},
'test', ]);
->willReturn(
[
FakeClass::class,
$method,
]
);

return $event;
}
Expand All @@ -76,19 +79,16 @@ public function test()
*/
public function testMissingArguments($object, $message)
{
$formFactory = $this->createMock(FormFactory::class);
$requestStack = $this->createMock(RequestStack::class);

$createFormAnnotationListener = new CreateFormAnnotationListener(
$this->mockReader($object),
$formFactory,
$requestStack
$this->createMock(FormFactory::class),
$this->createMock(RequestStack::class)
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($message);

$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent());
$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent('testBlank'));
}

public function missingArgumentsData()
Expand All @@ -109,4 +109,141 @@ public function missingArgumentsData()
],
];
}

public function testWrongParameterName()
{
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Post()));

$formFactory = $this->createMock(FormFactory::class);

$requestStack = $this->createMock(RequestStack::class);

$createFormAnnotationListener = new CreateFormAnnotationListener($reader, $formFactory, $requestStack);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Parameter "parameter" not found.');

$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent('testBlank'));
}

private function getValidAnnotation($annotation): Form\AbstractFormAnnotation
{
$annotation->formType = 'formType';
$annotation->parameter = 'parameter';

return $annotation;
}

public function testMissingParameterType()
{
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Post()));

$formFactory = $this->createMock(FormFactory::class);

$requestStack = $this->createMock(RequestStack::class);

$createFormAnnotationListener = new CreateFormAnnotationListener($reader, $formFactory, $requestStack);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Parameter "parameter" is missing a type.');

$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent('testWithParameter'));
}

public function testInvalidForm()
{
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Post()));

$formFactory = $this->mockForm($annotation);

$requestStack = $this->mockRequest();

$createFormAnnotationListener = new CreateFormAnnotationListener($reader, $formFactory, $requestStack);

$event = $this->mockFilterControllerEvent('testWithParameterAndType');

$event->expects($this->once())
->method('setController');

$createFormAnnotationListener->onKernelController($event);
}

private function mockForm(Form\AbstractFormAnnotation $annotation, bool $isValid = false)
{
$formInterface = $this->createMock(FormInterface::class);

$formInterface->expects($this->once())
->method('isValid')
->willReturn($isValid);

$formInterface->expects($this->once())
->method('submit')
->with([], $annotation->clearMissing);

$formFactory = $this->createMock(FormFactory::class);

$formFactory->expects($this->once())
->method('create')
->willReturn($formInterface);

return $formFactory;
}

private function mockRequest()
{
$request = $this->createMock(Request::class);

$mockRequest = $this->createMock(ParameterBag::class);
$mockRequest->expects($this->once())
->method('all')
->willReturn([]);

$request->request = $mockRequest;

$requestStack = $this->createMock(RequestStack::class);
$requestStack->expects($this->once())
->method('getCurrentRequest')
->willReturn($request);

return $requestStack;
}

public function testValidForm()
{
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Post()));

$formFactory = $this->mockForm($annotation, true);

$requestStack = $this->mockRequestWithAttributes($annotation);

$createFormAnnotationListener = new CreateFormAnnotationListener($reader, $formFactory, $requestStack);

$createFormAnnotationListener->onKernelController($this->mockFilterControllerEvent('testWithParameterAndType'));
}

private function mockRequestWithAttributes($annotation)
{
$request = $this->createMock(Request::class);

$mockAttributes = $this->createMock(ParameterBag::class);
$mockAttributes->expects($this->once())
->method('set')
->with($annotation->parameter);

$request->attributes = $mockAttributes;

$mockRequest = $this->createMock(ParameterBag::class);
$mockRequest->expects($this->once())
->method('all')
->willReturn([]);

$request->request = $mockRequest;

$requestStack = $this->createMock(RequestStack::class);
$requestStack->expects($this->once())
->method('getCurrentRequest')
->willReturn($request);

return $requestStack;
}
}
43 changes: 40 additions & 3 deletions tests/EventListener/UpdateFormAnnotationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,46 @@ public function missingArgumentsData()
];
}

public function testWrongParameterName()
{
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Put()));

$formFactory = $this->createMock(FormFactory::class);

$requestStack = $this->mockRequestAttribute($annotation);

$updateFormAnnotationListener = new UpdateFormAnnotationListener($reader, $formFactory, $requestStack);

$event = $this->mockFilterControllerEvent();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Parameter "parameter" not found.');
$updateFormAnnotationListener->onKernelController($event);
}

private function mockRequestAttribute($annotation)
{
$request = $this->createMock(Request::class);

$mockAttributes = $this->createMock(ParameterBag::class);
$mockAttributes->expects($this->once())
->method('get')
->with($annotation->parameter)
->willReturn(null);

$request->attributes = $mockAttributes;

$requestStack = $this->createMock(RequestStack::class);
$requestStack->expects($this->once())
->method('getCurrentRequest')
->willReturn($request);

return $requestStack;
}

public function testInvalidForm()
{
$reader = $this->mockReader($annotation = $this->populateAnnotation(new Form\Put()));
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Put()));

list($formFactory, $parameterValue) = $this->mockForm($annotation);

Expand All @@ -159,7 +196,7 @@ public function testInvalidForm()
$updateFormAnnotationListener->onKernelController($event);
}

private function populateAnnotation($annotation): Form\AbstractFormAnnotation
private function getValidAnnotation($annotation): Form\AbstractFormAnnotation
{
$annotation->formType = 'formType';
$annotation->parameter = 'parameter';
Expand Down Expand Up @@ -218,7 +255,7 @@ private function mockRequest($annotation, $parameterValue)

public function testValidForm()
{
$reader = $this->mockReader($annotation = $this->populateAnnotation(new Form\Patch()));
$reader = $this->mockReader($annotation = $this->getValidAnnotation(new Form\Patch()));

list($formFactory, $parameterValue) = $this->mockForm($annotation, true);

Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/FakeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace KunicMarko\FormAnnotationBundle\Tests\Fixtures;

/**
* @author Marko Kunic <kunicmarko20@gmail.com>
*/
class FakeClass
{
public static function testBlank()
{
}

public static function testWithParameter($parameter)
{
}

public static function testWithParameterAndType(FakeClass $parameter)
{
}
}

0 comments on commit 624abd9

Please sign in to comment.