Skip to content

Commit

Permalink
Merge pull request #14 from iisisrael/patch-13
Browse files Browse the repository at this point in the history
Updated FormType for Symfony 3.
  • Loading branch information
eymengunay committed Apr 26, 2016
2 parents c17675f + 37c1ff0 commit f3e99a7
Show file tree
Hide file tree
Showing 5 changed files with 541 additions and 227 deletions.
42 changes: 26 additions & 16 deletions Form/Type/HoneypotType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,36 @@
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;

class HoneypotType extends AbstractType
{
protected $request;
/**
* @var Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* @var Eo\HoneypotBundle\Manager\HoneypotManager
*/
protected $honeypotManager;

/**
* @var Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;

public function __construct(Request $request, HoneypotManager $honeypotManager, EventDispatcherInterface $eventDispatcher)
/**
* Class constructor
*
* @param Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param Eo\HoneypotBundle\Manager\HoneypotManager $honeypotManager
* @param Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
*/
public function __construct(RequestStack $requestStack, HoneypotManager $honeypotManager, EventDispatcherInterface $eventDispatcher)
{
$this->request = $request;
$this->requestStack = $requestStack;
$this->honeypotManager = $honeypotManager;
$this->eventDispatcher = $eventDispatcher;
}
Expand All @@ -46,7 +64,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// and re-introduced with 5.4. This small hack is here for 5.3 compability.
// https://wiki.php.net/rfc/closures/removal-of-this
// http://php.net/manual/en/migration54.new-features.php
$request = $this->request;
$request = $this->requestStack->getCurrentRequest();
$honeypotManager = $this->honeypotManager;
$eventDispatcher = $this->eventDispatcher;

Expand Down Expand Up @@ -74,7 +92,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'required' => false,
Expand All @@ -95,14 +113,6 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function getParent()
{
return 'text';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'honeypot';
return 'Symfony\Component\Form\Extension\Core\Type\TextType';
}
}
4 changes: 2 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</service>

<!-- Form Type -->
<service id="eo_honeypot.form.type.honeypot" class="%eo_honeypot.form.type.class%" scope="request">
<argument type="service" id="request"/>
<service id="eo_honeypot.form.type.honeypot" class="%eo_honeypot.form.type.class%">
<argument type="service" id="request_stack"/>
<argument type="service" id="eo_honeypot.manager"/>
<argument type="service" id="event_dispatcher"/>
<tag name="form.type" alias="honeypot" />
Expand Down
14 changes: 13 additions & 1 deletion Tests/Form/Type/HoneypotTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public function testBuildForm($data, $trigger)
->will($this->returnValue(new HoneypotPrey()))
;

$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')
->disableOriginalConstructor()
->setMethods(array('getCurrentRequest'))
->getMock()
;

$requestStack
->expects($this->any())
->method('getCurrentRequest')
->will($this->returnValue(new Request()))
;

$that = $this;

$eventDispatcher = new EventDispatcher();
Expand All @@ -56,7 +68,7 @@ public function testBuildForm($data, $trigger)

$builder = new FormBuilder('honeypot', null, $eventDispatcher, $factory);

$type = new HoneypotType(new Request(), $honeypotManager, $eventDispatcher);
$type = new HoneypotType($requestStack, $honeypotManager, $eventDispatcher);
$type->buildForm($builder, array());

$parent = $this->getMockBuilder('Symfony\Component\Form\Form')
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eo/honeypot-bundle",
"type": "symfony-bundle",
"description": "Honeypot trap for Symfony2 forms.",
"description": "Honeypot trap for Symfony3 forms.",
"keywords": ["honeypot", "spam"],
"license": "MIT",
"authors": [
Expand All @@ -11,11 +11,17 @@
}
],
"require": {
"symfony/symfony": "~2.1"
"symfony/symfony": "~3.0"
},
"require-dev": {
"eo/symfony-test-edition": "dev-master"
"eo/symfony-test-edition": "~3.0"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/iisisrael/symfony-test"
}
],
"target-dir": "Eo/HoneypotBundle",
"autoload": {
"psr-0": {
Expand All @@ -24,7 +30,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "3.0.x-dev"
}
}
}

0 comments on commit f3e99a7

Please sign in to comment.