Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShortcutTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class ShortcutTest extends \PHPUnit\Framework\TestCase
13 {
15  protected $shortcut;
16 
19 
21  protected $paymentHelperMock;
22 
24  protected $randomMock;
25 
28 
29  protected function setUp()
30  {
31  $this->paymentHelperMock = $this->createMock(\Magento\Payment\Helper\Data::class);
32  $this->randomMock = $this->createMock(\Magento\Framework\Math\Random::class);
33  $this->paypalShortcutHelperMock = $this->createMock(\Magento\Paypal\Helper\Shortcut\ValidatorInterface::class);
34 
35  $this->objectManagerHelper = new ObjectManagerHelper($this);
36  $this->shortcut = $this->objectManagerHelper->getObject(
37  \Magento\Paypal\Block\Bml\Shortcut::class,
38  [
39  'paymentData' => $this->paymentHelperMock,
40  'mathRandom' => $this->randomMock,
41  'shortcutValidator' => $this->paypalShortcutHelperMock,
42  ]
43  );
44  }
45 
46  public function testIsOrPositionBefore()
47  {
48  $this->assertFalse($this->shortcut->isOrPositionBefore());
49  $this->shortcut->setShowOrPosition(CatalogBlock\ShortcutButtons::POSITION_BEFORE);
50  $this->assertTrue($this->shortcut->isOrPositionBefore());
51  }
52 
53  public function testIsOrPositionAfter()
54  {
55  $this->assertFalse($this->shortcut->isOrPositionAfter());
56  $this->shortcut->setShowOrPosition(CatalogBlock\ShortcutButtons::POSITION_AFTER);
57  $this->assertTrue($this->shortcut->isOrPositionAfter());
58  }
59 
60  public function testGetAlias()
61  {
62  $this->assertEmpty($this->shortcut->getAlias());
63  }
64 
65  public function testToHtmlWrongValidation()
66  {
67  $isInCatalog = true;
68  $paymentMethodCode = '';
69  $this->shortcut->setIsInCatalogProduct($isInCatalog);
70 
71  $this->paypalShortcutHelperMock->expects($this->once())->method('validate')
72  ->with($paymentMethodCode, $isInCatalog)->will($this->returnValue(false));
73 
74  $this->assertEmpty($this->shortcut->toHtml());
75  }
76 
77  public function testToHtmlMethodNotAvailable()
78  {
79  $isInCatalog = true;
80  $paymentMethodCode = '';
81  $bmlMethodCode = '';
82  $this->shortcut->setIsInCatalogProduct($isInCatalog);
83  $expressMethod = $this->getMockBuilder(\Magento\Paypal\Model\Express::class)->disableOriginalConstructor()
84  ->setMethods([])->getMock();
85 
86  $this->paypalShortcutHelperMock->expects($this->once())->method('validate')
87  ->with($paymentMethodCode, $isInCatalog)->will($this->returnValue(true));
88  $this->paymentHelperMock->expects($this->once())->method('getMethodInstance')->with($bmlMethodCode)
89  ->will($this->returnValue($expressMethod));
90  $expressMethod->expects($this->once())->method('isAvailable')->will($this->returnValue(false));
91 
92  $this->assertEmpty($this->shortcut->toHtml());
93  }
94 
95  public function testToHtmlMethodSetBmlData()
96  {
97  $isInCatalog = true;
98  $paymentMethodCode = '';
99  $bmlMethodCode = '';
100  $hash = 'hash';
101  $this->shortcut->setIsInCatalogProduct($isInCatalog);
102  $expressMethod = $this->getMockBuilder(\Magento\Paypal\Model\Express::class)->disableOriginalConstructor()
103  ->setMethods([])->getMock();
104  $expectedData = [
105  'is_in_catalog_product' => $isInCatalog,
106  'shortcut_html_id' => $hash,
107  'checkout_url' => null,
108  'image_url' => 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/ppcredit-logo-medium.png',
109  'additional_link_image' => [
110  'href' => 'https://www.securecheckout.billmelater.com/paycapture-content/'
111  . 'fetch?hash=AU826TU8&content=/bmlweb/ppwpsiw.html',
112  'src' => 'https://www.paypalobjects.com/webstatic/en_US/btn/btn_bml_text.png',
113  ],
114  ];
115 
116  $this->paypalShortcutHelperMock->expects($this->once())->method('validate')
117  ->with($paymentMethodCode, $isInCatalog)->will($this->returnValue(true));
118  $this->paymentHelperMock->expects($this->once())->method('getMethodInstance')->with($bmlMethodCode)
119  ->will($this->returnValue($expressMethod));
120  $expressMethod->expects($this->once())->method('isAvailable')->will($this->returnValue(true));
121  $this->randomMock->expects($this->once())->method('getUniqueHash')->with('ec_shortcut_bml_')
122  ->will($this->returnValue($hash));
123 
124  $this->assertEmpty($this->shortcut->toHtml());
125  $this->assertContains($expectedData, $this->shortcut->getData());
126  }
127 }