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