33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Catalog \Helper \Product ;
89
10+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11+ use Magento \Catalog \Model \Product ;
912use Magento \Customer \Controller \RegistryConstants ;
13+ use Magento \Framework \DataObject ;
14+ use Magento \Framework \ObjectManagerInterface ;
1015use Magento \Framework \Registry ;
1116use Magento \TestFramework \Helper \Bootstrap ;
17+ use PHPUnit \Framework \TestCase ;
1218
1319/**
1420 * Test Composite
1521 */
16- class CompositeTest extends \ PHPUnit \ Framework \ TestCase
22+ class CompositeTest extends TestCase
1723{
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 ;
2235
2336 /**
24- * @var Registry
37+ * @inheritdoc
2538 */
26- protected $ registry ;
27-
2839 protected function setUp (): void
2940 {
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 ();
3246 }
3347
48+ /**
49+ * @inheritdoc
50+ */
3451 protected function tearDown (): void
3552 {
3653 $ this ->registry ->unregister ('composite_configure_result_error_message ' );
@@ -42,40 +59,85 @@ protected function tearDown(): void
4259 /**
4360 * @magentoDataFixture Magento/Catalog/_files/product_simple.php
4461 * @magentoDataFixture Magento/Customer/_files/customer.php
62+ * @return void
4563 */
46- public function testRenderConfigureResult ()
64+ public function testRenderConfigureResult (): void
4765 {
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);
5672 $ configureResult ->setOk (true )
5773 ->setProductId ($ product ->getId ())
74+ ->setBuyRequest ($ buyRequest )
5875 ->setCurrentCustomerId (1 );
5976
60- $ this ->helper ->renderConfigureResult ($ configureResult );
77+ $ resultLayout = $ this ->helper ->renderConfigureResult ($ configureResult );
6178
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+ );
6693 }
6794
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
69102 {
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 );
73106
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+ }
75118
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+ ];
80142 }
81143}
0 commit comments