Skip to content

Commit

Permalink
[bridge][symfony] allow unset sandbox checkbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Apr 15, 2015
1 parent a3709ad commit 3241ede
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
20 changes: 15 additions & 5 deletions Bridge/Symfony/Form/Type/PaymentConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,23 @@ public function buildCredentials(FormEvent $event)

$paymentFactory = $this->registry->getPaymentFactory($factoryName);
$config = $paymentFactory->createConfig();
$propertyPath = is_array($data) ? '[config]' : 'config';
$firstTime = false == PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);
foreach ($config['payum.default_options'] as $name => $value) {
$isRequired = in_array($name, $config['payum.required_options']);
$configForm->add($name, is_bool($value) ? 'checkbox' : 'text', array(
'empty_data' => $value,
'required' => $isRequired,
));
$propertyPath = is_array($data) ? "[config][$name]" : "config[$name]";
if ($firstTime) {
PropertyAccess::createPropertyAccessor()->setValue($data, $propertyPath, $value);
}

$type = is_bool($value) ? 'checkbox' : 'text';

$options = array();
$options['required'] = in_array($name, $config['payum.required_options']);

$configForm->add($name, $type, $options);
}

$event->setData($data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ public function shouldMarkFormInvalidAndAddConfigFields()
$this->assertTrue($form->get('config')->has('password'));
$this->assertEquals('defaultPass', $form->get('config')->get('password')->getData());

// TODO why it is null??
$this->assertTrue($form->get('config')->has('sandbox'));
$this->assertEquals(null, $form->get('config')->get('sandbox')->getData());
$this->assertEquals(true, $form->get('config')->get('sandbox')->getData());
}

/**
Expand Down Expand Up @@ -147,6 +146,49 @@ public function shouldSubmitWholePaymentConfig()
$this->assertEquals(false, $form->get('config')->get('sandbox')->getData());
}

/**
* @test
*/
public function shouldSetSandboxToFalseIfCheckboxUnset()
{
$this->fooPaymentFactoryMock
->expects($this->once())
->method('createConfig')
->with(array())
->willReturn(array(
'payum.default_options' => array(
'username' => 'defaultName',
'password' => 'defaultPass',
'sandbox' => true,
),
'payum.required_options' => array(),
))
;

$form = $this->formFactory->create('payum_payment_config');

$form->submit(array(
'paymentName' => 'foo',
'factoryName' => 'foo',
'config' => array(
'username' => 'submitName',
'password' => 'submitPass',
)

));

$this->assertTrue($form->has('config'));

$this->assertTrue($form->get('config')->has('username'));
$this->assertEquals('submitName', $form->get('config')->get('username')->getData());

$this->assertTrue($form->get('config')->has('password'));
$this->assertEquals('submitPass', $form->get('config')->get('password')->getData());

$this->assertTrue($form->get('config')->has('sandbox'));
$this->assertEquals(false, $form->get('config')->get('sandbox')->getData());
}

/**
* @test
*/
Expand Down

0 comments on commit 3241ede

Please sign in to comment.