3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Helper \Product ;
8
9
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Model \Product ;
9
12
use Magento \Customer \Controller \RegistryConstants ;
13
+ use Magento \Framework \DataObject ;
14
+ use Magento \Framework \ObjectManagerInterface ;
10
15
use Magento \Framework \Registry ;
11
16
use Magento \TestFramework \Helper \Bootstrap ;
17
+ use PHPUnit \Framework \TestCase ;
12
18
13
19
/**
14
20
* Test Composite
15
21
*/
16
- class CompositeTest extends \ PHPUnit \ Framework \ TestCase
22
+ class CompositeTest extends TestCase
17
23
{
18
- /**
19
- * @var Composite
20
- */
21
- protected $ helper ;
24
+ /** @var ObjectManagerInterface */
25
+ private $ objectManager ;
26
+
27
+ /** @var Composite */
28
+ private $ helper ;
29
+
30
+ /** @var Registry */
31
+ private $ registry ;
32
+
33
+ /** @var ProductRepositoryInterface */
34
+ private $ productRepository ;
22
35
23
36
/**
24
- * @var Registry
37
+ * @inheritdoc
25
38
*/
26
- protected $ registry ;
27
-
28
39
protected function setUp (): void
29
40
{
30
- $ this ->helper = Bootstrap::getObjectManager ()->get (\Magento \Catalog \Helper \Product \Composite::class);
31
- $ this ->registry = Bootstrap::getObjectManager ()->get (\Magento \Framework \Registry::class);
41
+ $ this ->objectManager = Bootstrap::getObjectManager ();
42
+ $ this ->helper = $ this ->objectManager ->get (Composite::class);
43
+ $ this ->registry = $ this ->objectManager ->get (Registry::class);
44
+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
45
+ $ this ->productRepository ->cleanCache ();
32
46
}
33
47
48
+ /**
49
+ * @inheritdoc
50
+ */
34
51
protected function tearDown (): void
35
52
{
36
53
$ this ->registry ->unregister ('composite_configure_result_error_message ' );
@@ -42,40 +59,85 @@ protected function tearDown(): void
42
59
/**
43
60
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
44
61
* @magentoDataFixture Magento/Customer/_files/customer.php
62
+ * @return void
45
63
*/
46
- public function testRenderConfigureResult ()
64
+ public function testRenderConfigureResult (): void
47
65
{
48
- /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
49
- $ productRepository = Bootstrap::getObjectManager ()->create (
50
- \Magento \Catalog \Api \ProductRepositoryInterface::class
51
- );
52
- /** @var $product \Magento\Catalog\Model\Product */
53
- $ product = $ productRepository ->get ('simple ' );
54
-
55
- $ configureResult = new \Magento \Framework \DataObject ();
66
+ $ product = $ this ->productRepository ->get ('simple ' );
67
+ /** @var DataObject $buyRequest */
68
+ $ buyRequest = $ this ->objectManager ->create (DataObject::class);
69
+ $ buyRequest ->setData (['qty ' => 1 ]);
70
+ /** @var DataObject $configureResult */
71
+ $ configureResult = $ this ->objectManager ->create (DataObject::class);
56
72
$ configureResult ->setOk (true )
57
73
->setProductId ($ product ->getId ())
74
+ ->setBuyRequest ($ buyRequest )
58
75
->setCurrentCustomerId (1 );
59
76
60
- $ this ->helper ->renderConfigureResult ($ configureResult );
77
+ $ resultLayout = $ this ->helper ->renderConfigureResult ($ configureResult );
61
78
62
- $ customerId = $ this ->registry ->registry (RegistryConstants::CURRENT_CUSTOMER_ID );
63
- $ this ->assertEquals (1 , $ customerId );
64
- $ errorMessage = $ this ->registry ->registry ('composite_configure_result_error_message ' );
65
- $ this ->assertNull ($ errorMessage );
79
+ /** @var Product $preparedProduct */
80
+ $ preparedProduct = $ this ->registry ->registry ('product ' );
81
+ $ preparedCurrentProduct = $ this ->registry ->registry ('current_product ' );
82
+ $ this ->assertTrue ($ preparedProduct && $ preparedCurrentProduct );
83
+ $ this ->assertEquals (1 , $ this ->registry ->registry (RegistryConstants::CURRENT_CUSTOMER_ID ));
84
+ $ this ->assertNotNull ($ preparedProduct ->getPreconfiguredValues ());
85
+ $ this ->assertContains (
86
+ 'CATALOG_PRODUCT_COMPOSITE_CONFIGURE ' ,
87
+ $ resultLayout ->getLayout ()->getUpdate ()->getHandles ()
88
+ );
89
+ $ this ->assertContains (
90
+ 'catalog_product_view_type_ ' . $ product ->getTypeId (),
91
+ $ resultLayout ->getLayout ()->getUpdate ()->getHandles ()
92
+ );
66
93
}
67
94
68
- public function testRenderConfigureResultNotOK ()
95
+ /**
96
+ * @dataProvider renderConfigureResultExceptionProvider
97
+ * @param array $data
98
+ * @param string $expectedErrorMessage
99
+ * @return void
100
+ */
101
+ public function testRenderConfigureResultException (array $ data , string $ expectedErrorMessage ): void
69
102
{
70
- $ configureResult = new \ Magento \ Framework \ DataObject ();
71
- $ configureResult-> setError ( true )
72
- -> setMessage ( ' Test Message ' );
103
+ /** @var DataObject $configureResult */
104
+ $ configureResult = $ this -> objectManager -> create (DataObject::class);
105
+ $ configureResult -> setData ( $ data );
73
106
74
- $ this ->helper ->renderConfigureResult ($ configureResult );
107
+ $ resultLayout = $ this ->helper ->renderConfigureResult ($ configureResult );
108
+
109
+ $ this ->assertEquals (
110
+ $ expectedErrorMessage ,
111
+ $ this ->registry ->registry ('composite_configure_result_error_message ' )
112
+ );
113
+ $ this ->assertContains (
114
+ 'CATALOG_PRODUCT_COMPOSITE_CONFIGURE_ERROR ' ,
115
+ $ resultLayout ->getLayout ()->getUpdate ()->getHandles ()
116
+ );
117
+ }
75
118
76
- $ customerId = $ this ->registry ->registry (RegistryConstants::CURRENT_CUSTOMER_ID );
77
- $ this ->assertNull ($ customerId );
78
- $ errorMessage = $ this ->registry ->registry ('composite_configure_result_error_message ' );
79
- $ this ->assertEquals ('Test Message ' , $ errorMessage );
119
+ /**
120
+ * Create render configure result exception provider
121
+ *
122
+ * @return array
123
+ */
124
+ public function renderConfigureResultExceptionProvider (): array
125
+ {
126
+ return [
127
+ 'error_true ' => [
128
+ 'data ' => [
129
+ 'error ' => true ,
130
+ 'message ' => 'Test Message '
131
+ ],
132
+ 'expected_error_message ' => 'Test Message ' ,
133
+ ],
134
+ 'without_product ' => [
135
+ 'data ' => [
136
+ 'ok ' => true ,
137
+ ],
138
+ 'expected_error_message ' => 'The product that was requested doesn \'t exist. '
139
+ . ' Verify the product and try again. ' ,
140
+ ],
141
+ ];
80
142
}
81
143
}
0 commit comments