From 4d003aa791b78ac7264381dbec75b118cbe2870a Mon Sep 17 00:00:00 2001 From: Paulo Rodrigues Pinto Date: Wed, 19 Oct 2016 00:25:15 +0100 Subject: [PATCH] Add tests for the form type (#187) --- Tests/Form/ChoosePaymentMethodTypeTest.php | 193 ++++++++++++++++++ .../ChoosePaymentMethodTransformerTest.php | 8 +- 2 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 Tests/Form/ChoosePaymentMethodTypeTest.php diff --git a/Tests/Form/ChoosePaymentMethodTypeTest.php b/Tests/Form/ChoosePaymentMethodTypeTest.php new file mode 100644 index 00000000..cfb6cdc3 --- /dev/null +++ b/Tests/Form/ChoosePaymentMethodTypeTest.php @@ -0,0 +1,193 @@ +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())); + } +} diff --git a/Tests/Form/Transformer/ChoosePaymentMethodTransformerTest.php b/Tests/Form/Transformer/ChoosePaymentMethodTransformerTest.php index 77098e42..1d61036e 100644 --- a/Tests/Form/Transformer/ChoosePaymentMethodTransformerTest.php +++ b/Tests/Form/Transformer/ChoosePaymentMethodTransformerTest.php @@ -126,9 +126,7 @@ public function testReverseTransformPredefinedDataWrongType() { $options = array( 'currency' => 'EUR', - 'amount' => function () { - return '10.42'; - }, + 'amount' => '10.42', 'predefined_data' => array( 'foo' => new self(), ), @@ -141,9 +139,7 @@ public function testReverseTransformPredefinedData() { $options = array( 'currency' => 'EUR', - 'amount' => function () { - return '10.42'; - }, + 'amount' => '10.42', 'predefined_data' => array( 'foo' => array( 'bar' => 'baz',