Skip to content

Commit c6b2543

Browse files
committed
added phpunit for Meta_Conversion module
1 parent dec5416 commit c6b2543

12 files changed

+1476
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Conversion\Test\Unit\Block\Pixel;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
8+
use Meta\Conversion\Block\Pixel\CustomerRegistrationSuccess;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Meta\BusinessExtension\Helper\FBEHelper;
11+
use Meta\Conversion\Helper\MagentoDataHelper;
12+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
13+
use Magento\Framework\Escaper;
14+
use Magento\Checkout\Model\Session as CheckoutSession;
15+
use Magento\Customer\Model\Session as CustomerSession;
16+
17+
class CustomerRegistrationSuccessTest extends TestCase
18+
{
19+
private $contextMock;
20+
private $fbeHelperMock;
21+
private $magentoDataHelperMock;
22+
private $systemConfigMock;
23+
private $escaperMock;
24+
private $checkoutSessionMock;
25+
private $customerSessionMock;
26+
private $subject;
27+
28+
public function setUp(): void
29+
{
30+
$this->contextMock = $this->getMockBuilder(Context::class)
31+
->disableOriginalConstructor()
32+
->getMock();
33+
$this->fbeHelperMock = $this->getMockBuilder(FBEHelper::class)
34+
->disableOriginalConstructor()
35+
->getMock();
36+
$this->magentoDataHelperMock = $this->getMockBuilder(MagentoDataHelper::class)
37+
->disableOriginalConstructor()
38+
->getMock();
39+
$this->systemConfigMock = $this->getMockBuilder(SystemConfig::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$this->escaperMock = $this->getMockBuilder(Escaper::class)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
$this->checkoutSessionMock = $this->getMockBuilder(CheckoutSession::class)
46+
->disableOriginalConstructor()
47+
->getMock();
48+
$this->customerSessionMock = $this->getMockBuilder(CustomerSession::class)
49+
->addMethods(['getMetaEventIds'])
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$objectManager = new ObjectManager($this);
54+
$this->subject = $objectManager->getObject(CustomerRegistrationSuccess::class, [
55+
'context' => $this->contextMock,
56+
'fbeHelper' => $this->fbeHelperMock,
57+
'magentoDataHelper' => $this->magentoDataHelperMock,
58+
'systemConfig' => $this->systemConfigMock,
59+
'escaper' => $this->escaperMock,
60+
'checkoutSession' => $this->checkoutSessionMock,
61+
'customerSession' => $this->customerSessionMock
62+
]);
63+
}
64+
65+
public function testGetContentType()
66+
{
67+
$this->assertEquals('customer_registration', $this->subject->getContentType());
68+
}
69+
70+
public function testGetEventToObserveName()
71+
{
72+
$this->assertEquals('facebook_businessextension_ssapi_customer_registration_success', $this->subject->getEventToObserveName());
73+
}
74+
75+
public function testGetEventId()
76+
{
77+
$eventId = '34567fg-dafsd5-adfsg';
78+
$metaEventIds = [
79+
'facebook_businessextension_ssapi_customer_registration_success' => $eventId
80+
];
81+
82+
$this->customerSessionMock->expects($this->once())
83+
->method('getMetaEventIds')
84+
->willReturn($metaEventIds);
85+
86+
$this->assertEquals($eventId, $this->subject->getEventId());
87+
}
88+
89+
public function testGetEventIdNull()
90+
{
91+
$eventId = '34567fg-dafsd5-adfsg';
92+
$metaEventIds = [
93+
'facebook_businessextension_ssapi_customer_registration' => $eventId
94+
];
95+
96+
$this->customerSessionMock->expects($this->once())
97+
->method('getMetaEventIds')
98+
->willReturn($metaEventIds);
99+
100+
$this->assertNull($this->subject->getEventId());
101+
}
102+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Meta\Conversion\Test\Unit\Block\Pixel;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
8+
use Meta\Conversion\Block\Pixel\CustomizeProduct;
9+
class CustomizeProductTest extends TestCase
10+
{
11+
private $subject;
12+
13+
public function setUp(): void
14+
{
15+
$objectManager = new ObjectManager($this);
16+
$this->subject = $objectManager->getObject(CustomizeProduct::class);
17+
}
18+
19+
public function testGetEventToObserveName()
20+
{
21+
$this->assertEquals('facebook_businessextension_ssapi_customize_product', $this->subject->getEventToObserveName());
22+
}
23+
}

app/code/Meta/Conversion/Test/Unit/Block/Pixel/HeadTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,19 @@ public function testReturnNonEmptyJsonStringWhenUserIsLoggedIn()
155155
$expectedJsonString = json_encode($userDataArray, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
156156
$this->assertEquals($expectedJsonString, $jsonString);
157157
}
158+
159+
public function testGetEventToObserveName()
160+
{
161+
$this->assertEquals('facebook_businessextension_ssapi_page_view', $this->head->getEventToObserveName());
162+
}
163+
164+
public function testGetDataProcessingOptionsImgTag()
165+
{
166+
$this->assertEquals('', $this->head->getDataProcessingOptionsImgTag());
167+
}
168+
169+
public function testGetDataProcessingOptionsJSCode()
170+
{
171+
$this->assertEquals('', $this->head->getDataProcessingOptionsJSCode());
172+
}
158173
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Conversion\Test\Unit\Block\Pixel;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
8+
use Meta\Conversion\Block\Pixel\InitiateCheckout;
9+
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Quote\Model\Quote\Item as QuoteItem;
12+
use Magento\Catalog\Model\Product;
13+
use Meta\Conversion\Helper\MagentoDataHelper;
14+
use Magento\Checkout\Model\Session as CheckoutSession;
15+
use Meta\Conversion\Model\CapiEventIdHandler;
16+
use Magento\Framework\View\Element\Template\Context;
17+
use Meta\BusinessExtension\Helper\FBEHelper;
18+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
19+
use Magento\Framework\Escaper;
20+
21+
class InitiateCheckoutTest extends TestCase
22+
{
23+
private $contextMock;
24+
private $fbeHelperMock;
25+
private $magentoDataHelperMock;
26+
private $systemConfigMock;
27+
private $escaperMock;
28+
private $checkoutSessionMock;
29+
private $quoteMock;
30+
private $quoteItemMock;
31+
private $productMock;
32+
private $pricingHelperMock;
33+
private $capiEventIdHandlerMock;
34+
private $subject;
35+
36+
public function setUp(): void
37+
{
38+
$this->contextMock = $this->getMockBuilder(Context::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
$this->fbeHelperMock = $this->getMockBuilder(FBEHelper::class)
42+
->disableOriginalConstructor()
43+
->getMock();
44+
$this->magentoDataHelperMock = $this->getMockBuilder(MagentoDataHelper::class)
45+
->disableOriginalConstructor()
46+
->getMock();
47+
$this->systemConfigMock = $this->getMockBuilder(SystemConfig::class)
48+
->disableOriginalConstructor()
49+
->getMock();
50+
$this->escaperMock = $this->getMockBuilder(Escaper::class)
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->checkoutSessionMock = $this->getMockBuilder(CheckoutSession::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
$this->quoteMock = $this->getMockBuilder(Quote::class)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
$this->quoteItemMock = $this->getMockBuilder(QuoteItem::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$this->productMock = $this->getMockBuilder(Product::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$this->pricingHelperMock = $this->getMockBuilder(PricingHelper::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->capiEventIdHandlerMock = $this->getMockBuilder(CapiEventIdHandler::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
72+
$objectManager = new ObjectManager($this);
73+
$this->subject = $objectManager->getObject(InitiateCheckout::class, [
74+
'context' => $this->contextMock,
75+
'fbeHelper' => $this->fbeHelperMock,
76+
'magentoDataHelper' => $this->magentoDataHelperMock,
77+
'systemConfig' => $this->systemConfigMock,
78+
'escaper' => $this->escaperMock,
79+
'checkoutSession' => $this->checkoutSessionMock,
80+
'pricingHelper' => $this->pricingHelperMock,
81+
'capiEventIdHandler' => $this->capiEventIdHandlerMock,
82+
]);
83+
}
84+
85+
public function testGetEventToObserveName()
86+
{
87+
$this->assertEquals('facebook_businessextension_ssapi_initiate_checkout', $this->subject->getEventToObserveName());
88+
}
89+
90+
public function testGetContentTypeQuote()
91+
{
92+
$this->assertEquals('product', $this->subject->getContentTypeQuote());
93+
}
94+
95+
public function testGetEventId()
96+
{
97+
$eventId = 'sdfg-234e2-sfgd23-123ss';
98+
$this->capiEventIdHandlerMock->expects($this->once())
99+
->method('getMetaEventId')
100+
->with('facebook_businessextension_ssapi_initiate_checkout')
101+
->willReturn($eventId);
102+
$this->assertEquals($eventId, $this->subject->getEventId());
103+
}
104+
105+
public function testGetContentIDs()
106+
{
107+
$productId = 1;
108+
$this->checkoutSessionMock->expects($this->once())
109+
->method('getQuote')
110+
->willReturn($this->quoteMock);
111+
$this->quoteMock->expects($this->once())
112+
->method('getAllVisibleItems')
113+
->willReturn([$this->quoteItemMock]);
114+
$this->quoteItemMock->expects($this->once())
115+
->method('getProduct')
116+
->willReturn($this->productMock);
117+
$this->magentoDataHelperMock->expects($this->once())
118+
->method('getContentId')
119+
->with($this->productMock)
120+
->willReturn($productId);
121+
$this->assertEquals([$productId], $this->subject->getContentIDs());
122+
}
123+
124+
public function testGetValue()
125+
{
126+
$itemPrice = 10.00;
127+
$this->checkoutSessionMock->expects($this->once())
128+
->method('getQuote')
129+
->willReturn($this->quoteMock);
130+
$this->magentoDataHelperMock->expects($this->once())
131+
->method('getCartTotal')
132+
->with($this->quoteMock)
133+
->willReturn($itemPrice);
134+
$this->assertEquals($itemPrice, $this->subject->getValue());
135+
}
136+
137+
public function testGetContents()
138+
{
139+
$productId = 1;
140+
$productQty = 1;
141+
$productPrice = 10.00;
142+
$contents = [
143+
[
144+
'id' => $productId,
145+
'quantity' => $productQty,
146+
'item_price' => $productPrice
147+
]
148+
];
149+
$this->checkoutSessionMock->expects($this->once())
150+
->method('getQuote')
151+
->willReturn($this->quoteMock);
152+
$this->quoteMock->expects($this->once())
153+
->method('getAllVisibleItems')
154+
->willReturn([$this->quoteItemMock]);
155+
$this->quoteItemMock->expects($this->once())
156+
->method('getProduct')
157+
->willReturn($this->productMock);
158+
$this->productMock->expects($this->once())
159+
->method('getFinalPrice')
160+
->willReturn($productPrice);
161+
$this->pricingHelperMock->expects($this->once())
162+
->method('currency')
163+
->with($productPrice, false, false)
164+
->willReturn($productPrice);
165+
$this->magentoDataHelperMock->expects($this->once())
166+
->method('getContentId')
167+
->with($this->productMock)
168+
->willReturn($productId);
169+
$this->quoteItemMock->expects($this->once())
170+
->method('getQty')
171+
->willReturn($productQty);
172+
$this->assertEquals($contents, $this->subject->getContents());
173+
}
174+
175+
public function testGetNumItems()
176+
{
177+
$itemQty = 1;
178+
$this->checkoutSessionMock->expects($this->once())
179+
->method('getQuote')
180+
->willReturn($this->quoteMock);
181+
$this->magentoDataHelperMock->expects($this->once())
182+
->method('getCartNumItems')
183+
->with($this->quoteMock)
184+
->willReturn($itemQty);
185+
$this->assertEquals($itemQty, $this->subject->getNumItems());
186+
}
187+
188+
public function testGetContentCategory()
189+
{
190+
$categoryName = 'Test Category';
191+
$this->checkoutSessionMock->expects($this->once())
192+
->method('getQuote')
193+
->willReturn($this->quoteMock);
194+
$this->quoteMock->expects($this->once())
195+
->method('getAllVisibleItems')
196+
->willReturn([$this->quoteItemMock]);
197+
$this->quoteItemMock->expects($this->once())
198+
->method('getProduct')
199+
->willReturn($this->productMock);
200+
$this->magentoDataHelperMock->expects($this->once())
201+
->method('getCategoriesForProduct')
202+
->with($this->productMock)
203+
->willReturn($categoryName);
204+
$this->assertEquals($categoryName, $this->subject->getContentCategory());
205+
}
206+
}

0 commit comments

Comments
 (0)