/** * @covers ::render */ public function testRender() { $plugin_id = $this->randomMachineName(); $plugin_label = $this->randomMachineName(); $plugin_definition = ['label' => $plugin_label]; $this->paymentLineItemManager->expects($this->atLeastOnce())->method('getDefinition')->with($plugin_id)->willReturn($plugin_definition); $result_row = new ResultRow(); $result_row->{$this->sut->field_alias} = $plugin_id; $this->assertSame($plugin_label, $this->sut->render($result_row)); }
/** * @covers ::createPayment */ public function testCreatePayment() { $bundle = $this->randomMachineName(); $currency_code = $this->randomMachineName(); $entity_type_id = $this->randomMachineName(); $field_name = $this->randomMachineName(); $payment_type = $this->getMockBuilder(PaymentReference::class)->disableOriginalConstructor()->getMock(); $payment_type->expects($this->once())->method('setBundle')->with($bundle); $payment_type->expects($this->once())->method('setEntityTypeId')->with($entity_type_id); $payment_type->expects($this->once())->method('setFieldName')->with($field_name); $payment = $this->getMock(PaymentInterface::class); $payment->expects($this->once())->method('setCurrencyCode')->with($currency_code); $payment->expects($this->once())->method('getPaymentType')->willReturn($payment_type); $payment_storage = $this->getMock(EntityStorageInterface::class); $payment_storage->expects($this->once())->method('create')->with(array('bundle' => 'payment_reference'))->willReturn($payment); $this->entityManager->expects($this->once())->method('getStorage')->with('payment')->willReturn($payment_storage); $line_item_a = $this->getMock(PaymentLineItemInterface::class); $line_item_plugin_id_a = $this->randomMachineName(); $line_item_plugin_configuration_a = array('foo' => $this->randomMachineName()); $line_item_b = $this->getMock(PaymentLineItemInterface::class); $line_item_plugin_id_b = $this->randomMachineName(); $line_item_plugin_configuration_b = array('bar' => $this->randomMachineName()); $field_storage_definition = $this->getMock(FieldStorageDefinitionInterface::class); $field_storage_definition->expects($this->once())->method('getTargetEntityTypeId')->willReturn($entity_type_id); $field_definition = $this->getMock(FieldDefinitionInterface::class); $field_definition->expects($this->once())->method('getTargetBundle')->willReturn($bundle); $field_definition->expects($this->once())->method('getFieldStorageDefinition')->willReturn($field_storage_definition); $field_definition->expects($this->once())->method('getName')->willreturn($field_name); $map = array(array('currency_code', $currency_code), array('line_items_data', array(array('plugin_configuration' => $line_item_plugin_configuration_a, 'plugin_id' => $line_item_plugin_id_a), array('plugin_configuration' => $line_item_plugin_configuration_b, 'plugin_id' => $line_item_plugin_id_b)))); $field_definition->expects($this->exactly(2))->method('getSetting')->willReturnMap($map); $this->paymentLineItemManager->expects($this->at(0))->method('createInstance')->with($line_item_plugin_id_a, $line_item_plugin_configuration_a)->willReturn($line_item_a); $this->paymentLineItemManager->expects($this->at(1))->method('createInstance')->with($line_item_plugin_id_b, $line_item_plugin_configuration_b)->willReturn($line_item_b); $this->assertSame($payment, $this->sut->createPayment($field_definition)); }
/** * @covers ::addMoreSubmit */ public function testAddMoreSubmit() { $plugin_id = $this->randomMachineName(); $values = array('add_more' => array('type' => $plugin_id)); $line_item = $this->getMock(PaymentLineItemInterface::class); $line_item->expects($this->once())->method('setName')->with($plugin_id); $this->paymentLineItemManager->expects($this->once())->method('createInstance')->with($plugin_id)->willReturn($line_item); $form_build = array('foo' => array('#name' => $this->randomMachineName(), 'add_more' => array('add' => array('#array_parents' => array('foo', 'add_more', 'add'), '#parents' => [])))); $form_state = new FormState(); $form_state->setTriggeringElement($form_build['foo']['add_more']['add']); $form_state->setValues($values); $this->sut->addMoreSubmit($form_build, $form_state); $this->assertTrue($form_state->isRebuilding()); $element = $this->sut; $line_items = $element::getLineItems($form_build['foo'], $form_state); $this->assertTrue(in_array($line_item, $line_items, TRUE)); }
/** * @covers ::viewElements */ public function testViewElements() { $entity_type_id = $this->randomMachineName(); $bundle = $this->randomMachineName(); $field_name = $this->randomMachineName(); $destination_url = $this->randomMachineName(); $currency_code = $this->randomMachineName(); $plugin_id = $this->randomMachineName(); $plugin_configuration = [$this->randomMachineName() => $this->randomMachineName()]; $plugin_id_property = $this->getMock(TypedDataInterface::class); $plugin_id_property->expects($this->once())->method('getValue')->willReturn($plugin_id); $plugin_configuration_property = $this->getMock(TypedDataInterface::class); $plugin_configuration_property->expects($this->once())->method('getValue')->willReturn($plugin_configuration); $map = [['plugin_id', $plugin_id_property], ['plugin_configuration', $plugin_configuration_property]]; $item = $this->getMockBuilder(PaymentFormFieldType::class)->disableOriginalConstructor()->getMock(); $item->expects($this->exactly(2))->method('get')->willReturnMap($map); $entity = $this->getMock(EntityInterface::class); $entity->expects($this->atLeastOnce())->method('bundle')->willReturn($bundle); $entity->expects($this->atLeastOnce())->method('getEntityTypeId')->willReturn($entity_type_id); $iterator = new \ArrayIterator([$item]); $items = $this->getMockBuilder(FieldItemList::class)->disableOriginalConstructor()->setMethods(['getEntity', 'getIterator'])->getMock(); $items->expects($this->atLeastOnce())->method('getEntity')->willReturn($entity); $items->expects($this->atLeastOnce())->method('getIterator')->willReturn($iterator); $this->fieldDefinition->expects($this->once())->method('getName')->willReturn($field_name); $this->fieldDefinition->expects($this->atLeastOnce())->method('getSetting')->with('currency_code')->willReturn($currency_code); $payment_type = $this->getMockBuilder(PaymentFormPaymentType::class)->disableOriginalConstructor()->getMock(); $payment_type->expects($this->once())->method('setEntityTypeId')->with($entity_type_id); $payment_type->expects($this->once())->method('setBundle')->with($bundle); $payment_type->expects($this->once())->method('setFieldName')->with($field_name); $payment_type->expects($this->once())->method('setDestinationUrl')->with($destination_url); $payment = $this->getMock(PaymentInterface::class); $payment->expects($this->once())->method('setCurrencyCode')->with($currency_code); $payment->expects($this->once())->method('getPaymentType')->willReturn($payment_type); $payment_line_item = $this->getMock(PaymentLineItemInterface::class); $this->paymentLineItemManager->expects($this->once())->method('createInstance')->with($plugin_id, $plugin_configuration)->willReturn($payment_line_item); $this->paymentStorage->expects($this->once())->method('create')->with(['bundle' => 'payment_form'])->willReturn($payment); $this->request->expects($this->atLeastOnce())->method('getUri')->willReturn($destination_url); $form = ['#foo' => $this->randomMachineName()]; $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment, 'payment_form')->willReturn($form); $this->assertSame($form, $this->sut->viewElements($items, 'en')); }