AbstractBlockTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element;
  7. use Magento\Framework\View\Element\AbstractBlock;
  8. /**
  9. * @magentoAppIsolation enabled
  10. */
  11. class AbstractBlockTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\View\Element\AbstractBlock
  15. */
  16. protected $_block;
  17. /**
  18. * @var \Magento\Framework\View\LayoutInterface
  19. */
  20. protected $_layout = null;
  21. protected static $_mocks = [];
  22. protected function setUp()
  23. {
  24. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
  25. ->setAreaCode('frontend');
  26. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  27. \Magento\Framework\View\DesignInterface::class
  28. )->setDefaultDesignTheme();
  29. $this->_block = $this->getMockForAbstractClass(
  30. \Magento\Framework\View\Element\AbstractBlock::class,
  31. [
  32. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  33. \Magento\Framework\View\Element\Context::class
  34. ),
  35. ['module_name' => 'Magento_Theme']
  36. ]
  37. );
  38. }
  39. /**
  40. * Checks, that not existing image in CSS not affected own publication
  41. *
  42. * @magentoAppIsolation enabled
  43. */
  44. public function testCssWithWrongImage()
  45. {
  46. $cssUrl = $this->_block->getViewFileUrl(
  47. 'css/wrong.css',
  48. ['area' => 'frontend', 'theme' => 'Magento/luma', 'locale' => 'en_US']
  49. );
  50. $this->assertStringMatchesFormat('%s/css/wrong.css', $cssUrl);
  51. }
  52. public function testGetRequest()
  53. {
  54. $this->assertInstanceOf(\Magento\Framework\App\RequestInterface::class, $this->_block->getRequest());
  55. }
  56. /**
  57. * @magentoAppIsolation enabled
  58. */
  59. public function testGetParentBlock()
  60. {
  61. // Without layout
  62. $this->assertFalse($this->_block->getParentBlock());
  63. // Need to create blocks through layout
  64. $parentBlock = $this->_createBlockWithLayout('block1', 'block1', \Magento\Framework\View\Element\Text::class);
  65. $childBlock = $this->_createBlockWithLayout('block2', 'block2');
  66. $this->assertEmpty($childBlock->getParentBlock());
  67. $parentBlock->setChild('block2', $childBlock);
  68. $this->assertSame($parentBlock, $childBlock->getParentBlock());
  69. }
  70. /**
  71. * @covers \Magento\Framework\View\Element\AbstractBlock::addChild
  72. */
  73. public function testAddChild()
  74. {
  75. $parentBlock = $this->_createBlockWithLayout(
  76. 'testAddChild',
  77. 'testAddChild',
  78. \Magento\Framework\View\Element\Text::class
  79. );
  80. $child = $parentBlock->addChild(
  81. 'testAddChildAlias',
  82. \Magento\Framework\View\Element\Text::class,
  83. ['content' => 'content']
  84. );
  85. $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $child);
  86. $this->assertEquals('testAddChild.testAddChildAlias', $child->getNameInLayout());
  87. $this->assertEquals($child, $parentBlock->getChildBlock('testAddChildAlias'));
  88. $this->assertEquals('content', $child->getContent());
  89. }
  90. public function testSetGetNameInLayout()
  91. {
  92. // Basic setting/getting
  93. $this->assertEmpty($this->_block->getNameInLayout());
  94. $name = uniqid('name');
  95. $this->_block->setNameInLayout($name);
  96. $this->assertEquals($name, $this->_block->getNameInLayout());
  97. // Setting second time, along with the layout
  98. $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  99. \Magento\Framework\View\LayoutInterface::class
  100. );
  101. $layout->createBlock(\Magento\Framework\View\Element\Template::class, $name);
  102. $block = $layout->getBlock($name);
  103. $this->assertInstanceOf(\Magento\Framework\View\Element\AbstractBlock::class, $block);
  104. $block->setNameInLayout($name);
  105. $this->assertInstanceOf(\Magento\Framework\View\Element\AbstractBlock::class, $layout->getBlock($name));
  106. $this->assertEquals($name, $block->getNameInLayout());
  107. $this->assertTrue($layout->hasElement($name));
  108. $newName = 'new_name';
  109. $block->setNameInLayout($newName);
  110. $this->assertTrue($layout->hasElement($newName));
  111. $this->assertFalse($layout->hasElement($name));
  112. }
  113. /**
  114. * @magentoAppIsolation enabled
  115. * @covers \Magento\Framework\View\Element\AbstractBlock::getChildNames
  116. * @covers \Magento\Framework\View\Element\AbstractBlock::insert
  117. */
  118. public function testGetChildNames()
  119. {
  120. // Without layout
  121. $this->assertEquals([], $this->_block->getChildNames());
  122. // With layout
  123. $parent = $this->_createBlockWithLayout('parent', 'parent');
  124. $block1 = $this->_createBlockWithLayout('block1');
  125. $block2 = $this->_createBlockWithLayout('block2');
  126. $block3 = $this->_createBlockWithLayout('block3');
  127. $block4 = $this->_createBlockWithLayout('block4');
  128. $parent->insert($block1);
  129. // add one block
  130. $parent->insert($block2, 'block1', false);
  131. // add second to the 1st position
  132. $parent->insert($block3, 'block1', false);
  133. // add third to the 2nd position
  134. $parent->insert($block4, 'block3', true);
  135. // add fourth block to the 3rd position
  136. $this->assertEquals(['block2', 'block3', 'block4', 'block1'], $parent->getChildNames());
  137. }
  138. public function testSetAttribute()
  139. {
  140. $this->assertEmpty($this->_block->getSomeValue());
  141. $this->_block->setAttribute('some_value', 'value');
  142. $this->assertEquals('value', $this->_block->getSomeValue());
  143. }
  144. /**
  145. * @magentoAppIsolation enabled
  146. */
  147. public function testSetGetUnsetChild()
  148. {
  149. // With layout
  150. $parent = $this->_createBlockWithLayout('parent', 'parent');
  151. // Regular block
  152. $nameOne = uniqid('block.');
  153. $blockOne = $this->_createBlockWithLayout($nameOne, $nameOne);
  154. $parent->setChild('block1', $blockOne);
  155. $this->assertSame($blockOne, $parent->getChildBlock('block1'));
  156. // Block factory name
  157. $blockTwo = $this->_createBlockWithLayout('parent_block2', 'parent_block2');
  158. $blockTwo->setChild('block2', $nameOne);
  159. $this->assertSame($blockOne, $blockTwo->getChildBlock('block2'));
  160. // No name block
  161. $blockThree = $this->_createBlockWithLayout('');
  162. $parent->setChild('block3', $blockThree);
  163. $this->assertSame($blockThree, $parent->getChildBlock('block3'));
  164. // Unset
  165. $parent->unsetChild('block3');
  166. $this->assertNotSame($blockThree, $parent->getChildBlock('block3'));
  167. $parent->insert($blockOne, '', true, 'block1');
  168. $this->assertContains($nameOne, $parent->getChildNames());
  169. $parent->unsetChild('block1');
  170. $this->assertNotSame($blockOne, $parent->getChildBlock('block1'));
  171. $this->assertNotContains($nameOne, $parent->getChildNames());
  172. }
  173. /**
  174. * @magentoAppIsolation enabled
  175. */
  176. public function testUnsetCallChild()
  177. {
  178. $blockParent = $this->_createBlockWithLayout('parent', 'parent');
  179. $block = $this->_createBlockWithLayout('block1', 'block1');
  180. $block->setSomeValue(true);
  181. $blockParent->setChild('block1', $block);
  182. $this->assertSame($block, $blockParent->getChildBlock('block1'));
  183. $blockParent->unsetCallChild('block1', 'getSomeValue', true, []);
  184. $this->assertNotSame($block, $blockParent->getChildBlock('block1'));
  185. }
  186. /**
  187. * @magentoAppIsolation enabled
  188. * @covers \Magento\Framework\View\Element\AbstractBlock::unsetChildren
  189. * @covers \Magento\Framework\View\Element\AbstractBlock::getChildBlock
  190. */
  191. public function testUnsetChildren()
  192. {
  193. $parent = $this->_createBlockWithLayout('block', 'block');
  194. $this->assertEquals([], $parent->getChildNames());
  195. $blockOne = $this->_createBlockWithLayout('block1', 'block1');
  196. $blockTwo = $this->_createBlockWithLayout('block2', 'block2');
  197. $parent->setChild('block1', $blockOne);
  198. $parent->setChild('block2', $blockTwo);
  199. $this->assertSame($blockOne, $parent->getChildBlock('block1'));
  200. $this->assertSame($blockTwo, $parent->getChildBlock('block2'));
  201. $parent->unsetChildren();
  202. $this->assertEquals([], $parent->getChildNames());
  203. }
  204. /**
  205. * @magentoAppIsolation enabled
  206. */
  207. public function testGetChildBlock()
  208. {
  209. $childAlias = 'child_alias';
  210. $childName = 'child';
  211. $parentName = 'parent';
  212. // Without layout
  213. $this->assertFalse($this->_block->getChildBlock($childAlias));
  214. // With layout
  215. /** @var $layout \Magento\Framework\View\Layout */
  216. $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  217. \Magento\Framework\View\LayoutInterface::class
  218. );
  219. $child = $layout->createBlock(\Magento\Framework\View\Element\Text::class, $childName);
  220. $layout->addBlock($this->_block, $parentName);
  221. $this->_block->setChild($childAlias, $child);
  222. $result = $this->_block->getChildBlock($childAlias);
  223. $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $result);
  224. $this->assertEquals($childName, $result->getNameInLayout());
  225. $this->assertEquals($child, $result);
  226. }
  227. /**
  228. * @magentoAppIsolation enabled
  229. * @covers \Magento\Framework\View\Element\AbstractBlock::getChildHtml
  230. * @covers \Magento\Framework\View\Element\AbstractBlock::getChildChildHtml
  231. */
  232. public function testGetChildHtml()
  233. {
  234. // Without layout
  235. $this->assertEmpty($this->_block->getChildHtml());
  236. $this->assertEmpty($this->_block->getChildHtml('block'));
  237. // With layout
  238. $parent = $this->_createBlockWithLayout('parent', 'parent');
  239. $blockOne = $this->_createBlockWithLayout('block1', 'block1', \Magento\Framework\View\Element\Text::class);
  240. $blockTwo = $this->_createBlockWithLayout('block2', 'block2', \Magento\Framework\View\Element\Text::class);
  241. $blockOne->setText('one');
  242. $blockTwo->setText('two');
  243. $parent->insert($blockTwo, '-', false, 'block2');
  244. // make block2 1st
  245. $parent->insert($blockOne, '-', false, 'block1');
  246. // make block1 1st
  247. $this->assertEquals('one', $parent->getChildHtml('block1'));
  248. $this->assertEquals('two', $parent->getChildHtml('block2'));
  249. // GetChildChildHtml
  250. $blockTwo->setChild('block11', $blockOne);
  251. $this->assertEquals('one', $parent->getChildChildHtml('block2'));
  252. $this->assertEquals('', $parent->getChildChildHtml(''));
  253. $this->assertEquals('', $parent->getChildChildHtml('block3'));
  254. }
  255. /**
  256. * @magentoAppIsolation enabled
  257. */
  258. public function testGetChildChildHtml()
  259. {
  260. // Without layout
  261. $this->assertEmpty($this->_block->getChildChildHtml('alias'));
  262. // With layout
  263. $parent1 = $this->_createBlockWithLayout('parent1', 'parent1');
  264. $parent2 = $this->_createBlockWithLayout('parent2', 'parent2');
  265. $block1 = $this->_createBlockWithLayout('block1', 'block1', \Magento\Framework\View\Element\Text::class);
  266. $block2 = $this->_createBlockWithLayout('block2', 'block2', \Magento\Framework\View\Element\Text::class);
  267. $block3 = $this->_createBlockWithLayout('block3', 'block3', \Magento\Framework\View\Element\Text::class);
  268. $block4 = $this->_createBlockWithLayout('block4', 'block4', \Magento\Framework\View\Element\Text::class);
  269. $block1->setText('one');
  270. $block2->setText('two');
  271. $block3->setText('three');
  272. $block4->setText('four');
  273. $parent1->insert($parent2);
  274. $parent2->insert($block1, '-', false, 'block1');
  275. $parent2->insert($block2, '-', false, 'block2');
  276. $parent2->insert($block3, '-', true, 'block3');
  277. $parent1->insert($block4);
  278. $this->assertEquals('twoonethree', $parent1->getChildChildHtml('parent2'));
  279. }
  280. public function testGetBlockHtml()
  281. {
  282. // Without layout
  283. /** @var $blockFactory \Magento\Framework\View\Element\BlockFactory */
  284. $blockFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  285. \Magento\Framework\View\Element\BlockFactory::class
  286. );
  287. $block1 = $blockFactory->createBlock(\Magento\Framework\View\Element\Text::class);
  288. $block1->setText('Block text');
  289. $block1->setNameInLayout('block');
  290. $html = $this->_block->getBlockHtml('block');
  291. $this->assertInternalType('string', $html);
  292. $this->assertEmpty($html);
  293. // With layout
  294. $expected = 'Block2';
  295. $block2 = $this->_createBlockWithLayout('block2', 'block2', \Magento\Framework\View\Element\Text::class);
  296. $block3 = $this->_createBlockWithLayout('block3', 'block3');
  297. $block2->setText($expected);
  298. $html = $block3->getBlockHtml('block2');
  299. $this->assertInternalType('string', $html);
  300. $this->assertEquals($expected, $html);
  301. }
  302. /**
  303. * @magentoAppIsolation enabled
  304. */
  305. public function testInsertBlockWithoutName()
  306. {
  307. $parent = $this->_createBlockWithLayout('parent', 'parent');
  308. $block = $this->_createBlockWithLayout('');
  309. $parent->setChild('', $block);
  310. $this->assertContains('abstractblockmock_0', $parent->getChildNames());
  311. }
  312. /**
  313. * @magentoAppIsolation enabled
  314. */
  315. public function testInsertBlockWithAlias()
  316. {
  317. $parent = $this->_createBlockWithLayout('parent', 'parent');
  318. $block = $this->_createBlockWithLayout('block_name');
  319. $parent->insert($block, '', true, 'block_alias');
  320. $this->assertContains('block_name', $parent->getChildNames());
  321. $this->assertSame($block, $parent->getChildBlock('block_alias'));
  322. }
  323. public function testInsertWithSibling()
  324. {
  325. $name1 = 'block_one';
  326. $parent = $this->_createBlockWithLayout('parent', 'parent');
  327. $blockOne = $this->_createBlockWithLayout($name1);
  328. $parent->insert($blockOne);
  329. $this->assertContains($name1, $parent->getChildNames());
  330. $name2 = 'block_two';
  331. $blockTwo = $this->_createBlockWithLayout($name2);
  332. $parent->insert($blockTwo, 'wrong_sibling', false);
  333. $this->assertSame(1, array_search($name2, $parent->getChildNames()));
  334. $name3 = 'block_three';
  335. $blockThree = $this->_createBlockWithLayout($name3);
  336. $parent->insert($blockThree, $name2, false);
  337. $this->assertSame(1, array_search($name3, $parent->getChildNames()));
  338. $name4 = 'block_four';
  339. $blockFour = $this->_createBlockWithLayout($name4);
  340. $parent->insert($blockFour, $name1, true);
  341. $this->assertSame(1, array_search($name4, $parent->getChildNames()));
  342. }
  343. /**
  344. * @magentoAppIsolation enabled
  345. * @expectedException \OutOfBoundsException
  346. */
  347. public function testInsertWithoutCreateBlock()
  348. {
  349. $parent = $this->_createBlockWithLayout('parent', 'parent');
  350. $parent->insert('block');
  351. }
  352. /**
  353. * @magentoAppIsolation enabled
  354. */
  355. public function testInsertContainer()
  356. {
  357. $parentName = 'parent';
  358. $name = 'container';
  359. $parent = $this->_createBlockWithLayout($parentName, $parentName);
  360. $layout = $parent->getLayout();
  361. $this->assertEmpty($layout->getChildNames($parentName));
  362. $layout->addContainer($name, 'Container');
  363. $parent->insert($name);
  364. $this->assertEquals([$name], $layout->getChildNames($parentName));
  365. }
  366. /**
  367. * @magentoAppIsolation enabled
  368. */
  369. public function testAppend()
  370. {
  371. $parent = $this->_createBlockWithLayout('parent', 'parent');
  372. $child1 = $this->_createBlockWithLayout('child1');
  373. $parent->append($child1, 'child1');
  374. $child2 = $this->_createBlockWithLayout('child2');
  375. $parent->append($child2);
  376. $this->assertEquals(['child1', 'child2'], $parent->getChildNames());
  377. }
  378. /**
  379. * @magentoAppIsolation enabled
  380. * @covers \Magento\Framework\View\Element\AbstractBlock::getGroupChildNames
  381. * @covers \Magento\Framework\View\Layout::addToParentGroup
  382. */
  383. public function testAddToParentGroup()
  384. {
  385. $parent = $this->_createBlockWithLayout('parent', 'parent');
  386. $block1 = $this->_createBlockWithLayout('block1', 'block1');
  387. $block2 = $this->_createBlockWithLayout('block2', 'block2');
  388. $parent->setChild('block1', $block1)->setChild('block2', $block2);
  389. $this->_layout->addToParentGroup('block1', 'group');
  390. $this->_layout->addToParentGroup('block2', 'group');
  391. $group = $parent->getGroupChildNames('group');
  392. $this->assertContains('block1', $group);
  393. $this->assertContains('block2', $group);
  394. $this->assertSame($group[0], 'block1');
  395. $this->assertSame($group[1], 'block2');
  396. }
  397. public function testGetChildData()
  398. {
  399. $parent = $this->_createBlockWithLayout('parent', 'parent');
  400. $block = $this->_createBlockWithLayout('block', 'block');
  401. $block->setSomeProperty('some_value');
  402. $parent->setChild('block1', $block);
  403. // all child data
  404. $actualChildData = $parent->getChildData('block1');
  405. $this->assertArrayHasKey('some_property', $actualChildData);
  406. $this->assertEquals('some_value', $actualChildData['some_property']);
  407. // specific child data key
  408. $this->assertEquals('some_value', $parent->getChildData('block1', 'some_property'));
  409. // non-existing child block
  410. $this->assertNull($parent->getChildData('unknown_block'));
  411. }
  412. public function testGetUrl()
  413. {
  414. $base = 'http://localhost/index.php/';
  415. $withRoute = "{$base}catalog/product/view/id/10/";
  416. $this->assertEquals($base, $this->_block->getUrl());
  417. $this->assertEquals($withRoute, $this->_block->getUrl('catalog/product/view', ['id' => 10]));
  418. }
  419. public function testGetViewFileUrl()
  420. {
  421. $actualResult = $this->_block->getViewFileUrl('css/styles.css');
  422. $this->assertStringMatchesFormat(
  423. 'http://localhost/pub/static/%s/frontend/%s/en_US/css/styles.css',
  424. $actualResult
  425. );
  426. }
  427. public function testGetModuleName()
  428. {
  429. $this->assertEquals('Magento_Theme', $this->_block->getModuleName());
  430. $this->assertEquals('Magento_Theme', $this->_block->getData('module_name'));
  431. }
  432. /**
  433. * @dataProvider escapeHtmlDataProvider
  434. */
  435. public function testEscapeHtml($data, $expected)
  436. {
  437. $actual = $this->_block->escapeHtml($data);
  438. $this->assertEquals($expected, $actual);
  439. }
  440. /**
  441. * @return array
  442. */
  443. public function escapeHtmlDataProvider()
  444. {
  445. return [
  446. 'array data' => [
  447. 'data' => ['one', '<two>three</two>'],
  448. 'expected' => ['one', '&lt;two&gt;three&lt;/two&gt;'],
  449. ],
  450. 'string data conversion' => [
  451. 'data' => '<two>three</two>',
  452. 'expected' => '&lt;two&gt;three&lt;/two&gt;',
  453. ],
  454. 'string data no conversion' => ['data' => 'one', 'expected' => 'one']
  455. ];
  456. }
  457. public function testStripTags()
  458. {
  459. $str = '<p>text</p>';
  460. $this->assertEquals('text', $this->_block->stripTags($str));
  461. }
  462. public function testEscapeUrl()
  463. {
  464. $url = 'http://example.com/?wsdl=1';
  465. $this->assertEquals($url, $this->_block->escapeUrl($url));
  466. }
  467. public function testEscapeJsQuote()
  468. {
  469. $script = "var s = 'text';";
  470. $this->assertEquals('var s = \\\'text\\\';', $this->_block->escapeJsQuote($script));
  471. }
  472. public function testGetCacheKeyInfo()
  473. {
  474. $name = uniqid('block.');
  475. $block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  476. \Magento\Framework\View\LayoutInterface::class
  477. )->createBlock(
  478. \Magento\Framework\View\Element\Text::class
  479. );
  480. $block->setNameInLayout($name);
  481. $this->assertEquals([$name], $block->getCacheKeyInfo());
  482. }
  483. public function testGetCacheKey()
  484. {
  485. $name = uniqid('block.');
  486. $block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  487. \Magento\Framework\View\LayoutInterface::class
  488. )->createBlock(
  489. \Magento\Framework\View\Element\Text::class
  490. );
  491. $block->setNameInLayout($name);
  492. $key = $block->getCacheKey();
  493. $this->assertNotEmpty($key);
  494. $this->assertNotEquals('key', $key);
  495. $this->assertNotEquals($name, $key);
  496. $block->setCacheKey('key');
  497. $this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . 'key', $block->getCacheKey());
  498. }
  499. /**
  500. * Create <N> sample blocks
  501. *
  502. * @param int $qty
  503. * @param bool $withLayout
  504. * @param string $className
  505. * @return array
  506. */
  507. protected function _createSampleBlocks(
  508. $qty,
  509. $withLayout = true,
  510. $className = \Magento\Framework\View\Element\Template::class
  511. ) {
  512. $blocks = [];
  513. $names = [];
  514. $layout = false;
  515. if ($withLayout) {
  516. $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  517. \Magento\Framework\View\LayoutInterface::class
  518. );
  519. }
  520. for ($i = 0; $i < $qty; $i++) {
  521. $name = uniqid('block.');
  522. if ($layout) {
  523. $block = $layout->createBlock($className, $name);
  524. } else {
  525. $block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($className);
  526. $block->setNameInLayout($name);
  527. }
  528. $blocks[] = $block;
  529. $names[] = $name;
  530. }
  531. return [$blocks, $names];
  532. }
  533. /**
  534. * Create Block with Layout
  535. *
  536. * @param string $name
  537. * @param null|string $alias
  538. * @param null|string $type
  539. * @return \Magento\Framework\View\Element\AbstractBlock
  540. */
  541. protected function _createBlockWithLayout(
  542. $name = 'block',
  543. $alias = null,
  544. $type = \Magento\Framework\View\Element\AbstractBlock::class
  545. ) {
  546. $typePart = explode('\\', $type);
  547. $mockClass = array_pop($typePart) . 'Mock';
  548. if (!isset(self::$_mocks[$mockClass])) {
  549. self::$_mocks[$mockClass] = $this->getMockForAbstractClass(
  550. $type,
  551. [
  552. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  553. \Magento\Framework\View\Element\Context::class
  554. ),
  555. ['module_name' => 'Magento_Theme']
  556. ],
  557. $mockClass
  558. );
  559. }
  560. if ($this->_layout === null) {
  561. $this->_layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  562. \Magento\Framework\View\LayoutInterface::class
  563. );
  564. }
  565. $block = $this->_layout->addBlock($mockClass, $name, '', $alias);
  566. return $block;
  567. }
  568. }