17
17
18
18
namespace Meta \Conversion \Test \Unit \Observer ;
19
19
20
- use Meta \Conversion \Helper \AAMFieldsExtractorHelper ;
21
- use Meta \Conversion \Helper \ServerSideHelper ;
22
20
use Meta \Conversion \Observer \AddToCart ;
21
+ use Meta \Conversion \Helper \ServerSideHelper ;
23
22
use Magento \Framework \App \RequestInterface ;
23
+ use Magento \Framework \Escaper ;
24
24
use Magento \Framework \Event \Observer ;
25
- use PHPUnit \ Framework \ MockObject \ MockObject ;
25
+ use Magento \ Catalog \ Model \ Product ;
26
26
27
27
class AddToCartTest extends CommonTest
28
28
{
29
+
29
30
/**
30
- * @var MockObject
31
+ * @var ServerSideHelper
31
32
*/
32
- protected $ request ;
33
+ private $ serverSideHelper ;
33
34
34
35
/**
35
- * @var AddToCart
36
+ * @var RequestInterface
36
37
*/
37
- protected $ addToCartObserver ;
38
+ private $ request ;
38
39
39
40
/**
40
- * @var ServerSideHelper
41
+ * @var Escaper
41
42
*/
42
- protected $ serverSideHelper ;
43
+ private $ escaper ;
43
44
44
45
/**
45
- * @var AAMFieldsExtractorHelper
46
+ * @var ObserverInterface
46
47
*/
47
- protected $ aamFieldsExtractorHelper ;
48
+ private $ observer ;
49
+
50
+ /**
51
+ * @var AddToCart
52
+ */
53
+ private $ addToCartObserver ;
48
54
49
55
/**
50
56
* Used to set the values before running a test
@@ -54,62 +60,102 @@ class AddToCartTest extends CommonTest
54
60
public function setUp (): void
55
61
{
56
62
parent ::setUp ();
57
- $ this ->request = $ this ->createMock (RequestInterface::class);
58
- $ this ->aamFieldsExtractorHelper = new AAMFieldsExtractorHelper (
59
- $ this ->magentoDataHelper ,
60
- $ this ->fbeHelper
61
- );
62
- $ this ->serverSideHelper = new ServerSideHelper (
63
- $ this ->fbeHelper ,
64
- $ this ->aamFieldsExtractorHelper ,
65
- $ this ->systemConfig
66
- );
67
- $ this ->addToCartObserver = new AddToCart (
68
- $ this ->fbeHelper ,
69
- $ this ->magentoDataHelper ,
70
- $ this ->serverSideHelper ,
71
- $ this ->request
72
- );
63
+
64
+ $ this ->serverSideHelper = $ this ->getMockBuilder (ServerSideHelper::class)
65
+ ->disableOriginalConstructor ()
66
+ ->getMock ();
67
+ $ this ->request = $ this ->getMockBuilder (RequestInterface::class)
68
+ ->disableOriginalConstructor ()
69
+ ->getMockForAbstractClass ();
70
+ $ this ->escaper = $ this ->getMockBuilder (Escaper::class)
71
+ ->disableOriginalConstructor ()
72
+ ->getMock ();
73
+ $ this ->observer = $ this ->getMockBuilder (Observer::class)
74
+ ->onlyMethods (['getData ' ])
75
+ ->disableOriginalConstructor ()
76
+ ->getMock ();
77
+
78
+ $ this ->addToCartObserver = $ this ->objectManager ->getObject (AddToCart::class, [
79
+ 'fbeHelper ' => $ this ->fbeHelper ,
80
+ 'magentoDataHelper ' => $ this ->magentoDataHelper ,
81
+ 'serverSideHelper ' => $ this ->serverSideHelper ,
82
+ 'request ' => $ this ->request ,
83
+ 'escaper ' => $ this ->escaper
84
+ ]);
73
85
}
74
86
75
- public function testAddToCartEventCreated ()
87
+ /**
88
+ * Test execute methood
89
+ *
90
+ * @return void
91
+ */
92
+ public function testExecute ()
76
93
{
77
- $ id = 123 ;
78
- $ sku = 'SKU-123 ' ;
94
+ $ eventId = '12ghjs-34vcv1-dfff3v-43kj97 ' ;
95
+ $ productId = 12 ;
96
+ $ productName = 'Test Product ' ;
97
+ $ currency = 'USD ' ;
98
+ $ value = 100.00 ;
79
99
$ contentType = 'product ' ;
80
- $ eventId = '1234 ' ;
81
-
82
- $ this ->magentoDataHelper ->method ('getValueForProduct ' )->willReturn (12.99 );
83
- $ this ->magentoDataHelper ->method ('getCategoriesForProduct ' )->willReturn ('Electronics ' );
84
- $ this ->magentoDataHelper ->method ('getContentId ' )->willReturn ($ sku );
85
- $ this ->magentoDataHelper ->method ('getContentType ' )->willReturn ($ contentType );
86
-
87
- $ product = $ this ->objectManager ->getObject ('\Magento\Catalog\Model\Product ' );
88
- $ product ->setId ($ id )->setSku ($ sku );
89
- $ product ->setName ('Earphones ' );
90
- $ this ->request ->method ('getParam ' )->willReturn ($ sku );
91
- $ this ->magentoDataHelper ->method ('getProductBySku ' )->willReturn ($ product );
92
- $ this ->magentoDataHelper ->method ('getProductById ' )->willReturn ($ product );
93
-
94
- $ observer = new Observer (['eventId ' => $ eventId ]);
95
-
96
- $ this ->addToCartObserver ->execute ($ observer );
97
-
98
- $ this ->assertEquals (1 , count ($ this ->serverSideHelper ->getTrackedEvents ()));
99
-
100
- $ event = $ this ->serverSideHelper ->getTrackedEvents ()[0 ];
101
-
102
- $ this ->assertEquals ($ eventId , $ event ->getEventId ());
103
-
104
- $ customDataArray = [
105
- 'currency ' => 'USD ' ,
106
- 'value ' => 12.99 ,
107
- 'content_type ' => $ contentType ,
108
- 'content_ids ' => [$ sku ],
109
- 'content_category ' => 'Electronics ' ,
110
- 'content_name ' => 'Earphones '
111
- ];
112
-
113
- $ this ->assertEqualsCustomData ($ customDataArray , $ event ->getCustomData ());
100
+ $ contentIds = 'test-product ' ;
101
+ $ contentCategory = 'Test Category ' ;
102
+
103
+ $ productMock = $ this ->getMockBuilder (Product::class)
104
+ ->disableOriginalConstructor ()
105
+ ->getMock ();
106
+
107
+ $ this ->observer ->expects ($ this ->once ())
108
+ ->method ('getData ' )
109
+ ->with ('eventId ' )
110
+ ->willReturn ($ eventId );
111
+
112
+ $ this ->request ->expects ($ this ->once ())
113
+ ->method ('getParam ' )
114
+ ->with ('product_id ' , null )
115
+ ->willReturn ($ productId );
116
+
117
+ $ this ->magentoDataHelper ->expects ($ this ->once ())
118
+ ->method ('getProductById ' )
119
+ ->with ($ productId )
120
+ ->willReturn ($ productMock );
121
+
122
+ $ productMock ->expects ($ this ->once ())
123
+ ->method ('getId ' )
124
+ ->willReturn ($ productId );
125
+
126
+ $ this ->magentoDataHelper ->expects ($ this ->once ())
127
+ ->method ('getCurrency ' )
128
+ ->willReturn ($ currency );
129
+
130
+ $ this ->magentoDataHelper ->expects ($ this ->once ())
131
+ ->method ('getValueForProduct ' )
132
+ ->with ($ productMock )
133
+ ->willReturn ($ value );
134
+
135
+ $ this ->magentoDataHelper ->expects ($ this ->once ())
136
+ ->method ('getContentType ' )
137
+ ->with ($ productMock )
138
+ ->willReturn ($ contentType );
139
+
140
+ $ this ->magentoDataHelper ->expects ($ this ->once ())
141
+ ->method ('getContentId ' )
142
+ ->with ($ productMock )
143
+ ->willReturn ($ contentIds );
144
+
145
+ $ this ->magentoDataHelper ->expects ($ this ->once ())
146
+ ->method ('getCategoriesForProduct ' )
147
+ ->with ($ productMock )
148
+ ->willReturn ($ contentCategory );
149
+
150
+ $ productMock ->expects ($ this ->once ())
151
+ ->method ('getName ' )
152
+ ->willReturn ($ productName );
153
+
154
+ $ this ->escaper ->expects ($ this ->once ())
155
+ ->method ('escapeUrl ' )
156
+ ->with ($ productName )
157
+ ->willReturn ($ productName );
158
+
159
+ $ this ->addToCartObserver ->execute ($ this ->observer );
114
160
}
115
161
}
0 commit comments