|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JMS\Payment\CoreBundle\Tests\Form\ChoosePaymentMethodTypeTest; |
| 4 | + |
| 5 | +use JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType; |
| 6 | +use JMS\Payment\CoreBundle\Util\Legacy; |
| 7 | +use JMS\Payment\PaypalBundle\Form\ExpressCheckoutType; |
| 8 | +use Symfony\Component\Form\PreloadedExtension; |
| 9 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 10 | +use Symfony\Component\HttpKernel\Kernel; |
| 11 | + |
| 12 | +class ChoosePaymentMethodTypeTest extends TypeTestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
| 16 | + * @expectedExceptionMessage amount |
| 17 | + */ |
| 18 | + public function testAmountIsRequired() |
| 19 | + { |
| 20 | + $form = $this->createForm(array( |
| 21 | + 'amount' => null, |
| 22 | + )); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
| 27 | + * @expectedExceptionMessage currency |
| 28 | + */ |
| 29 | + public function testCurrencyIsRequired() |
| 30 | + { |
| 31 | + $form = $this->createForm(array( |
| 32 | + 'currency' => null, |
| 33 | + )); |
| 34 | + } |
| 35 | + |
| 36 | + public function testMethod() |
| 37 | + { |
| 38 | + $form = $this->createForm(); |
| 39 | + $this->assertTrue($form->isSynchronized()); |
| 40 | + $this->assertTrue($form->has('method')); |
| 41 | + } |
| 42 | + |
| 43 | + public function testMethodData() |
| 44 | + { |
| 45 | + $form = $this->createForm(); |
| 46 | + |
| 47 | + foreach (array('foo', 'bar') as $method) { |
| 48 | + $this->assertTrue($form->has('data_'.$method)); |
| 49 | + |
| 50 | + $config = $form->get('data_'.$method)->getConfig(); |
| 51 | + |
| 52 | + $this->assertInstanceOf( |
| 53 | + 'JMS\Payment\PaypalBundle\Form\ExpressCheckoutType', |
| 54 | + $config->getType()->getInnerType() |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public function testMethodChoices() |
| 60 | + { |
| 61 | + if (Legacy::formChoicesAsValues()) { |
| 62 | + $this->markTestSkipped(); |
| 63 | + } |
| 64 | + |
| 65 | + $form = $this->createForm(); |
| 66 | + |
| 67 | + $this->assertArraySubset(array( |
| 68 | + 'form.label.foo' => 'foo', |
| 69 | + 'form.label.bar' => 'bar', |
| 70 | + ), $form->get('method')->getConfig()->getOption('choices')); |
| 71 | + } |
| 72 | + |
| 73 | + public function testLegacyMethodChoices() |
| 74 | + { |
| 75 | + if (!Legacy::formChoicesAsValues()) { |
| 76 | + $this->markTestSkipped(); |
| 77 | + } |
| 78 | + |
| 79 | + $form = $this->createForm(); |
| 80 | + |
| 81 | + $expected = array( |
| 82 | + 'foo' => 'form.label.foo', |
| 83 | + 'bar' => 'form.label.bar', |
| 84 | + ); |
| 85 | + |
| 86 | + if (version_compare(Kernel::VERSION, '2.7.0', '>=')) { |
| 87 | + $expected = array( |
| 88 | + 'foo' => 0, |
| 89 | + 'bar' => 1, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + $this->assertArraySubset($expected, $form->get('method')->getConfig()->getOption('choices')); |
| 94 | + } |
| 95 | + |
| 96 | + public function testDefaultMethod() |
| 97 | + { |
| 98 | + $form = $this->createForm(array( |
| 99 | + 'default_method' => 'foo', |
| 100 | + )); |
| 101 | + |
| 102 | + $this->assertTrue($form->isSynchronized()); |
| 103 | + $this->assertEquals('foo', $form->get('method')->getConfig()->getOption('data')); |
| 104 | + } |
| 105 | + |
| 106 | + public function testAllowedMethods() |
| 107 | + { |
| 108 | + if (Legacy::formChoicesAsValues()) { |
| 109 | + $this->markTestSkipped(); |
| 110 | + } |
| 111 | + |
| 112 | + $form = $this->createForm(array( |
| 113 | + 'allowed_methods' => array('bar'), |
| 114 | + )); |
| 115 | + |
| 116 | + $this->assertTrue($form->isSynchronized()); |
| 117 | + |
| 118 | + $choices = $form->get('method')->getConfig()->getOption('choices'); |
| 119 | + $this->assertArrayNotHasKey('form.label.foo', $choices); |
| 120 | + $this->assertArraySubset(array('form.label.bar' => 'bar'), $choices); |
| 121 | + |
| 122 | + $this->assertTrue($form->has('data_bar')); |
| 123 | + $this->assertFalse($form->has('data_foo')); |
| 124 | + } |
| 125 | + |
| 126 | + public function testLegacyAllowedMethods() |
| 127 | + { |
| 128 | + if (!Legacy::formChoicesAsValues()) { |
| 129 | + $this->markTestSkipped(); |
| 130 | + } |
| 131 | + |
| 132 | + $form = $this->createForm(array( |
| 133 | + 'allowed_methods' => array('bar'), |
| 134 | + )); |
| 135 | + |
| 136 | + $choices = $form->get('method')->getConfig()->getOption('choices'); |
| 137 | + $this->assertArrayNotHasKey('foo', $choices); |
| 138 | + $this->assertArraySubset(array('bar' => 'form.label.bar'), $choices); |
| 139 | + } |
| 140 | + |
| 141 | + private function createForm($options = array(), $data = array()) |
| 142 | + { |
| 143 | + $options = array_merge(array( |
| 144 | + 'amount' => '10.42', |
| 145 | + 'currency' => 'EUR', |
| 146 | + ), $options); |
| 147 | + |
| 148 | + $form = Legacy::supportsFormTypeName() |
| 149 | + ? 'jms_choose_payment_method' |
| 150 | + : 'JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType' |
| 151 | + ; |
| 152 | + |
| 153 | + $form = $this->factory->create($form, null, $options); |
| 154 | + $form->submit($data); |
| 155 | + |
| 156 | + return $form; |
| 157 | + } |
| 158 | + |
| 159 | + protected function setUp() |
| 160 | + { |
| 161 | + $this->pluginController = $this->getMockBuilder('JMS\Payment\CoreBundle\PluginController\PluginControllerInterface') |
| 162 | + ->getMock(); |
| 163 | + |
| 164 | + parent::setUp(); |
| 165 | + } |
| 166 | + |
| 167 | + protected function getExtensions() |
| 168 | + { |
| 169 | + $pluginType = new ExpressCheckoutType(); |
| 170 | + |
| 171 | + if (Legacy::supportsFormTypeName()) { |
| 172 | + $pluginTypeName = $pluginType->getName(); |
| 173 | + } else { |
| 174 | + $pluginTypeName = get_class($pluginType); |
| 175 | + } |
| 176 | + |
| 177 | + $type = new ChoosePaymentMethodType($this->pluginController, array( |
| 178 | + 'foo' => $pluginTypeName, |
| 179 | + 'bar' => $pluginTypeName, |
| 180 | + )); |
| 181 | + |
| 182 | + if (Legacy::supportsFormTypeName()) { |
| 183 | + $extensions = array( |
| 184 | + $pluginType->getName() => $pluginType, |
| 185 | + $type->getName() => $type, |
| 186 | + ); |
| 187 | + } else { |
| 188 | + $extensions = array($pluginType, $type); |
| 189 | + } |
| 190 | + |
| 191 | + return array(new PreloadedExtension($extensions, array())); |
| 192 | + } |
| 193 | +} |
0 commit comments