77
88namespace Magento \ProductAlert \Model ;
99
10- use Magento \Customer \Api \AccountManagementInterface ;
11- use Magento \Customer \Model \Session ;
12- use Magento \Framework \App \Area ;
13- use Magento \Framework \Locale \Resolver ;
14- use Magento \Framework \Module \Dir \Reader ;
15- use Magento \Framework \Phrase ;
16- use Magento \Framework \Phrase \Renderer \Translate as PhraseRendererTranslate ;
17- use Magento \Framework \Phrase \RendererInterface ;
18- use Magento \Framework \Translate ;
19- use Magento \Store \Model \StoreRepository ;
10+ use Magento \Framework \DB \Select ;
11+ use Magento \ProductAlert \Model \ResourceModel \Price as PriceResource ;
2012use Magento \TestFramework \Helper \Bootstrap ;
21- use Magento \TestFramework \Helper \CacheCleaner ;
22- use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
23- use Magento \TestFramework \ObjectManager ;
13+ use Magento \TestFramework \MessageQueue \EnvironmentPreconditionException ;
14+ use Magento \TestFramework \MessageQueue \PreconditionFailedException ;
15+ use Magento \TestFramework \MessageQueue \PublisherConsumerController ;
16+ use PHPUnit \Framework \TestCase ;
2417
2518/**
26- * Test for Magento\ProductAlert\Model\Observer
19+ * Test Product Alert observer
2720 *
28- * @magentoAppIsolation enabled
29- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+ * @magentoDbIsolation disabled
3022 */
31- class ObserverTest extends \ PHPUnit \ Framework \ TestCase
23+ class ObserverTest extends TestCase
3224{
33- /**
34- * @var ObjectManager
35- */
36- protected $ _objectManager ;
37-
3825 /**
3926 * @var Observer
4027 */
4128 private $ observer ;
4229
4330 /**
44- * @var TransportBuilderMock
31+ * @var PriceResource
4532 */
46- private $ transportBuilder ;
33+ private $ priceResource ;
4734
4835 /**
4936 * @inheritDoc
5037 */
5138 protected function setUp (): void
5239 {
53- Bootstrap::getInstance ()->loadArea (Area::AREA_FRONTEND );
54- $ this ->_objectManager = Bootstrap::getObjectManager ();
55- $ this ->observer = $ this ->_objectManager ->get (Observer::class);
56- $ this ->transportBuilder = $ this ->_objectManager ->get (TransportBuilderMock::class);
57- $ service = $ this ->_objectManager ->create (AccountManagementInterface::class);
58- $ customer = $ service ->authenticate ('customer@example.com ' , 'password ' );
59- $ customerSession = $ this ->_objectManager ->get (Session::class);
60- $ customerSession ->setCustomerDataAsLoggedIn ($ customer );
40+ $ objectManager = Bootstrap::getObjectManager ();
41+ $ this ->observer = $ objectManager ->get (Observer::class);
42+ $ this ->priceResource = $ objectManager ->create (PriceResource::class);
6143 }
6244
6345 /**
@@ -69,57 +51,59 @@ protected function setUp(): void
6951 public function testProcess ()
7052 {
7153 $ this ->observer ->process ();
72- $ this ->assertStringContainsString (
73- 'John Smith, ' ,
74- $ this ->transportBuilder ->getSentMessage ()->getBody ()->getParts ()[0 ]->getRawContent ()
75- );
54+ $ this ->assertProcessAlertByConsumer ();
7655 }
7756
7857 /**
79- * Check translations for product alerts
58+ * Waiting for execute consumer
8059 *
81- * @magentoDbIsolation disabled
82- * @magentoAppArea frontend
83- * @magentoDataFixture Magento/Catalog/_files/category.php
84- * @magentoConfigFixture current_store catalog/productalert/allow_price 1
85- * @magentoDataFixture Magento/Store/_files/second_store.php
86- * @magentoConfigFixture fixture_second_store_store general/locale/code pt_BR
87- * @magentoDataFixture Magento/ProductAlert/_files/product_alert_with_store.php
60+ * @return void
61+ * @throws PreconditionFailedException
8862 */
89- public function testProcessPortuguese ()
63+ private function assertProcessAlertByConsumer (): void
9064 {
91- // get second store
92- $ storeRepository = $ this ->_objectManager ->create (StoreRepository::class);
93- $ secondStore = $ storeRepository ->get ('fixture_second_store ' );
65+ /** @var PublisherConsumerController $publisherConsumerController */
66+ $ publisherConsumerController = Bootstrap::getObjectManager ()->create (
67+ PublisherConsumerController::class,
68+ [
69+ 'consumers ' => ['product_alert ' ],
70+ 'logFilePath ' => TESTS_TEMP_DIR . "/MessageQueueTestLog.txt " ,
71+ 'maxMessages ' => 1 ,
72+ 'appInitParams ' => Bootstrap::getInstance ()->getAppInitParams ()
73+ ]
74+ );
75+ try {
76+ $ publisherConsumerController ->startConsumers ();
77+ } catch (EnvironmentPreconditionException $ e ) {
78+ $ this ->markTestSkipped ($ e ->getMessage ());
79+ } catch (PreconditionFailedException $ e ) {
80+ $ this ->fail (
81+ $ e ->getMessage ()
82+ );
83+ }
84+
85+ sleep (15 ); // timeout to processing Magento queue
9486
95- // check if Portuguese language is specified for the second store
96- $ storeResolver = $ this ->_objectManager ->get (Resolver::class);
97- $ storeResolver ->emulate ($ secondStore ->getId ());
98- $ this ->assertEquals ('pt_BR ' , $ storeResolver ->getLocale ());
87+ $ publisherConsumerController ->waitForAsynchronousResult (
88+ function () {
89+ return $ this ->loadLastPriceAlertStatus ();
90+ },
91+ []
92+ );
93+ }
9994
100- // set translation data and check it
101- $ modulesReader = $ this ->createPartialMock (Reader::class, ['getModuleDir ' ]);
102- $ modulesReader ->expects ($ this ->any ())
103- ->method ('getModuleDir ' )
104- ->willReturn (dirname (__DIR__ ) . '/_files/i18n ' );
105- /** @var Translate $translator */
106- $ translator = $ this ->_objectManager ->create (Translate::class, ['modulesReader ' => $ modulesReader ]);
107- $ translation = [
108- 'Price change alert! We wanted you to know that prices have changed for these products: ' =>
109- 'Alerta de mudanca de preco! Queriamos que voce soubesse que os precos mudaram para esses produtos: '
110- ];
111- $ translator ->loadData ();
112- $ this ->assertEquals ($ translation , $ translator ->getData ());
113- $ this ->_objectManager ->addSharedInstance ($ translator , Translate::class);
114- $ this ->_objectManager ->removeSharedInstance (PhraseRendererTranslate::class);
115- Phrase::setRenderer ($ this ->_objectManager ->create (RendererInterface::class));
95+ /**
96+ * Load last created price alert
97+ *
98+ * @return bool
99+ */
100+ private function loadLastPriceAlertStatus (): bool
101+ {
102+ $ select = $ this ->priceResource ->getConnection ()->select ();
103+ $ select ->from ($ this ->priceResource ->getMainTable (), ['status ' ])
104+ ->order ($ this ->priceResource ->getIdFieldName () . ' ' . Select::SQL_DESC )
105+ ->limit (1 );
116106
117- // dispatch process() method and check sent message
118- $ this ->observer ->process ();
119- $ message = $ this ->transportBuilder ->getSentMessage ();
120- $ messageContent = $ message ->getBody ()->getParts ()[0 ]->getRawContent ();
121- $ expectedText = array_shift ($ translation );
122- $ this ->assertStringContainsString ('/frontend/Magento/luma/pt_BR/ ' , $ messageContent );
123- $ this ->assertStringContainsString (substr ($ expectedText , 0 , 50 ), $ messageContent );
107+ return (bool )$ this ->priceResource ->getConnection ()->fetchOne ($ select );
124108 }
125109}
0 commit comments