Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BuilderCompositeTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class BuilderCompositeTest extends \PHPUnit\Framework\TestCase
12 {
13  public function testBuildEmpty()
14  {
15  $tMapFactory = $this->getMockBuilder(\Magento\Framework\ObjectManager\TMapFactory::class)
16  ->disableOriginalConstructor()
17  ->setMethods(['create'])
18  ->getMock();
19  $tMap = $this->getMockBuilder(\Magento\Framework\ObjectManager\TMap::class)
20  ->disableOriginalConstructor()
21  ->getMock();
22 
23  $tMapFactory->expects(static::once())
24  ->method('create')
25  ->with(
26  [
27  'array' => [],
28  'type' => BuilderInterface::class
29  ]
30  )
31  ->willReturn($tMap);
32  $tMap->expects(static::once())
33  ->method('getIterator')
34  ->willReturn(new \ArrayIterator([]));
35 
36  $builder = new BuilderComposite($tMapFactory, []);
37  static::assertEquals([], $builder->build([]));
38  }
39 
45  public function testBuild(array $expected)
46  {
47  $tMapFactory = $this->getMockBuilder(\Magento\Framework\ObjectManager\TMapFactory::class)
48  ->disableOriginalConstructor()
49  ->setMethods(['create'])
50  ->getMock();
51  $tMap = $this->getMockBuilder(\Magento\Framework\ObjectManager\TMap::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $customerBuilder = $this->getMockBuilder(\Magento\Payment\Gateway\Request\BuilderInterface::class)
55  ->getMockForAbstractClass();
56  $productBuilder = $this->getMockBuilder(\Magento\Payment\Gateway\Request\BuilderInterface::class)
57  ->getMockForAbstractClass();
58  $magentoBuilder = $this->getMockBuilder(\Magento\Payment\Gateway\Request\BuilderInterface::class)
59  ->getMockForAbstractClass();
60 
61  $customerBuilder->expects(static::once())
62  ->method('build')
63  ->willReturn(
64  [
65  'user' => $expected['user'],
66  'address' => $expected['address']
67  ]
68  );
69  $productBuilder->expects(static::once())
70  ->method('build')
71  ->willReturn(
72  [
73  'amount' => $expected['amount'],
74  'currency' => $expected['currency'],
75  'item' => $expected['item'],
76  'quantity' => $expected['quantity'],
77  'options' => ['product' => $expected['options']['product']]
78  ]
79  );
80  $magentoBuilder->expects(static::once())
81  ->method('build')
82  ->willReturn(
83  [
84  'url' => $expected['url'],
85  'options' => ['magento' => $expected['options']['magento']]
86  ]
87  );
88 
89  $tMapFactory->expects(static::once())
90  ->method('create')
91  ->with(
92  [
93  'array' => [
94  'customer' => \Magento\Payment\Gateway\Request\BuilderInterface::class,
95  'product' => \Magento\Payment\Gateway\Request\BuilderInterface::class,
96  'magento' => \Magento\Payment\Gateway\Request\BuilderInterface::class
97  ],
98  'type' => BuilderInterface::class
99  ]
100  )
101  ->willReturn($tMap);
102  $tMap->expects(static::once())
103  ->method('getIterator')
104  ->willReturn(new \ArrayIterator([$customerBuilder, $productBuilder, $magentoBuilder]));
105 
106  $builder = new BuilderComposite(
107  $tMapFactory,
108  [
109  'customer' => \Magento\Payment\Gateway\Request\BuilderInterface::class,
110  'product' => \Magento\Payment\Gateway\Request\BuilderInterface::class,
111  'magento' => \Magento\Payment\Gateway\Request\BuilderInterface::class
112  ]
113  );
114 
115  static::assertEquals($expected, $builder->build([]));
116  }
117 
121  public function buildDataProvider()
122  {
123  return [
124  [[
125  'user' => 'Mrs G. Crump',
126  'address' => '46 Egernon Crescent',
127  'amount' => 10.00,
128  'currency' => 'pound',
129  'item' => 'gas cooker',
130  'quantity' => 1,
131  'options' => ['product' => '', 'magento' => 'magento'],
132  'url' => 'https://url.in',
133  ]],
134  [[
135  'user' => 'John Doe',
136  'address' => '46 Main Street',
137  'amount' => 250.00,
138  'currency' => 'usd',
139  'item' => 'phone',
140  'quantity' => 2,
141  'options' => ['product' => 'product', 'magento' => 'magento'],
142  'url' => 'https://url.io',
143  ]],
144  [[
145  'user' => 'John Smit',
146  'address' => '46 Egernon Crescent',
147  'amount' => 1100.00,
148  'currency' => 'usd',
149  'item' => 'notebook',
150  'quantity' => 1,
151  'options' => ['product' => ['discount' => ['price' => 2.00]], 'magento' => 'magento'],
152  'url' => 'http://url.ua',
153  ]],
154  ];
155  }
156 }