forked from ezsystems/JMSJobQueueBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the form type (schmittjoh#187)
- Loading branch information
Showing
2 changed files
with
195 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
namespace JMS\Payment\CoreBundle\Tests\Form\ChoosePaymentMethodTypeTest; | ||
|
||
use JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType; | ||
use JMS\Payment\CoreBundle\Util\Legacy; | ||
use JMS\Payment\PaypalBundle\Form\ExpressCheckoutType; | ||
use Symfony\Component\Form\PreloadedExtension; | ||
use Symfony\Component\Form\Test\TypeTestCase; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
|
||
class ChoosePaymentMethodTypeTest extends TypeTestCase | ||
{ | ||
/** | ||
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
* @expectedExceptionMessage amount | ||
*/ | ||
public function testAmountIsRequired() | ||
{ | ||
$form = $this->createForm(array( | ||
'amount' => null, | ||
)); | ||
} | ||
|
||
/** | ||
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
* @expectedExceptionMessage currency | ||
*/ | ||
public function testCurrencyIsRequired() | ||
{ | ||
$form = $this->createForm(array( | ||
'currency' => null, | ||
)); | ||
} | ||
|
||
public function testMethod() | ||
{ | ||
$form = $this->createForm(); | ||
$this->assertTrue($form->isSynchronized()); | ||
$this->assertTrue($form->has('method')); | ||
} | ||
|
||
public function testMethodData() | ||
{ | ||
$form = $this->createForm(); | ||
|
||
foreach (array('foo', 'bar') as $method) { | ||
$this->assertTrue($form->has('data_'.$method)); | ||
|
||
$config = $form->get('data_'.$method)->getConfig(); | ||
|
||
$this->assertInstanceOf( | ||
'JMS\Payment\PaypalBundle\Form\ExpressCheckoutType', | ||
$config->getType()->getInnerType() | ||
); | ||
} | ||
} | ||
|
||
public function testMethodChoices() | ||
{ | ||
if (Legacy::formChoicesAsValues()) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$form = $this->createForm(); | ||
|
||
$this->assertArraySubset(array( | ||
'form.label.foo' => 'foo', | ||
'form.label.bar' => 'bar', | ||
), $form->get('method')->getConfig()->getOption('choices')); | ||
} | ||
|
||
public function testLegacyMethodChoices() | ||
{ | ||
if (!Legacy::formChoicesAsValues()) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$form = $this->createForm(); | ||
|
||
$expected = array( | ||
'foo' => 'form.label.foo', | ||
'bar' => 'form.label.bar', | ||
); | ||
|
||
if (version_compare(Kernel::VERSION, '2.7.0', '>=')) { | ||
$expected = array( | ||
'foo' => 0, | ||
'bar' => 1, | ||
); | ||
} | ||
|
||
$this->assertArraySubset($expected, $form->get('method')->getConfig()->getOption('choices')); | ||
} | ||
|
||
public function testDefaultMethod() | ||
{ | ||
$form = $this->createForm(array( | ||
'default_method' => 'foo', | ||
)); | ||
|
||
$this->assertTrue($form->isSynchronized()); | ||
$this->assertEquals('foo', $form->get('method')->getConfig()->getOption('data')); | ||
} | ||
|
||
public function testAllowedMethods() | ||
{ | ||
if (Legacy::formChoicesAsValues()) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$form = $this->createForm(array( | ||
'allowed_methods' => array('bar'), | ||
)); | ||
|
||
$this->assertTrue($form->isSynchronized()); | ||
|
||
$choices = $form->get('method')->getConfig()->getOption('choices'); | ||
$this->assertArrayNotHasKey('form.label.foo', $choices); | ||
$this->assertArraySubset(array('form.label.bar' => 'bar'), $choices); | ||
|
||
$this->assertTrue($form->has('data_bar')); | ||
$this->assertFalse($form->has('data_foo')); | ||
} | ||
|
||
public function testLegacyAllowedMethods() | ||
{ | ||
if (!Legacy::formChoicesAsValues()) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$form = $this->createForm(array( | ||
'allowed_methods' => array('bar'), | ||
)); | ||
|
||
$choices = $form->get('method')->getConfig()->getOption('choices'); | ||
$this->assertArrayNotHasKey('foo', $choices); | ||
$this->assertArraySubset(array('bar' => 'form.label.bar'), $choices); | ||
} | ||
|
||
private function createForm($options = array(), $data = array()) | ||
{ | ||
$options = array_merge(array( | ||
'amount' => '10.42', | ||
'currency' => 'EUR', | ||
), $options); | ||
|
||
$form = Legacy::supportsFormTypeName() | ||
? 'jms_choose_payment_method' | ||
: 'JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType' | ||
; | ||
|
||
$form = $this->factory->create($form, null, $options); | ||
$form->submit($data); | ||
|
||
return $form; | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
$this->pluginController = $this->getMockBuilder('JMS\Payment\CoreBundle\PluginController\PluginControllerInterface') | ||
->getMock(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
protected function getExtensions() | ||
{ | ||
$pluginType = new ExpressCheckoutType(); | ||
|
||
if (Legacy::supportsFormTypeName()) { | ||
$pluginTypeName = $pluginType->getName(); | ||
} else { | ||
$pluginTypeName = get_class($pluginType); | ||
} | ||
|
||
$type = new ChoosePaymentMethodType($this->pluginController, array( | ||
'foo' => $pluginTypeName, | ||
'bar' => $pluginTypeName, | ||
)); | ||
|
||
if (Legacy::supportsFormTypeName()) { | ||
$extensions = array( | ||
$pluginType->getName() => $pluginType, | ||
$type->getName() => $type, | ||
); | ||
} else { | ||
$extensions = array($pluginType, $type); | ||
} | ||
|
||
return array(new PreloadedExtension($extensions, array())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters