PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Test/Integrity/Magento/Payment/MethodsTest.php

https://gitlab.com/svillegas/magento2
PHP | 149 lines | 106 code | 9 blank | 34 comment | 6 complexity | 08e1cb678434e6514d24482130371126 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Locate all payment methods in the system and verify declaration of their blocks
  8. */
  9. namespace Magento\Test\Integrity\Magento\Payment;
  10. use Magento\Framework\App\State;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class MethodsTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @param string $methodClass
  19. * @param string $code
  20. * @dataProvider paymentMethodDataProvider
  21. * @magentoAppArea frontend
  22. * @throws \Exception on various assertion failures
  23. */
  24. public function testPaymentMethod($code, $methodClass)
  25. {
  26. if ($code == 'vault') {
  27. return;
  28. }
  29. Bootstrap::getObjectManager()->configure($this->getTestConfiguration());
  30. /** @var $blockFactory \Magento\Framework\View\Element\BlockFactory */
  31. $blockFactory = Bootstrap::getObjectManager()->get(
  32. \Magento\Framework\View\Element\BlockFactory::class
  33. );
  34. $storeId = Bootstrap::getObjectManager()->get(
  35. \Magento\Store\Model\StoreManagerInterface::class
  36. )->getStore()->getId();
  37. /** @var $model \Magento\Payment\Model\MethodInterface */
  38. if (empty($methodClass)) {
  39. /**
  40. * Note that $code is not whatever the payment method getCode() returns
  41. */
  42. $this->fail("Model of '{$code}' payment method is not found.");
  43. }
  44. $model = Bootstrap::getObjectManager()->create($methodClass);
  45. if ($code == \Magento\Payment\Model\Method\Substitution::CODE) {
  46. $paymentInfo = $this->getMockBuilder(
  47. \Magento\Payment\Model\Info::class
  48. )->disableOriginalConstructor()->setMethods(
  49. []
  50. )->getMock();
  51. $paymentInfo->expects(
  52. $this->any()
  53. )->method(
  54. 'getAdditionalInformation'
  55. )->will(
  56. $this->returnValue('Additional data mock')
  57. );
  58. $model->setInfoInstance($paymentInfo);
  59. }
  60. Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
  61. ->setMode(State::MODE_DEVELOPER);
  62. $this->assertNotEmpty($model->getTitle());
  63. foreach ([$model->getFormBlockType(), $model->getInfoBlockType()] as $blockClass) {
  64. $message = "Block class: {$blockClass}";
  65. /** @var $block \Magento\Framework\View\Element\Template */
  66. $block = $blockFactory->createBlock($blockClass);
  67. $block->setArea('frontend');
  68. $this->assertFileExists((string)$block->getTemplateFile(), $message);
  69. if ($model->canUseInternal()) {
  70. try {
  71. Bootstrap::getObjectManager()->get(
  72. \Magento\Store\Model\StoreManagerInterface::class
  73. )->getStore()->setId(
  74. \Magento\Store\Model\Store::DEFAULT_STORE_ID
  75. );
  76. $block->setArea('adminhtml');
  77. $this->assertFileExists((string)$block->getTemplateFile(), $message);
  78. Bootstrap::getObjectManager()->get(
  79. \Magento\Store\Model\StoreManagerInterface::class
  80. )->getStore()->setId(
  81. $storeId
  82. );
  83. } catch (\Exception $e) {
  84. Bootstrap::getObjectManager()->get(
  85. \Magento\Store\Model\StoreManagerInterface::class
  86. )->getStore()->setId(
  87. $storeId
  88. );
  89. throw $e;
  90. }
  91. }
  92. }
  93. Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
  94. ->setMode(State::MODE_DEFAULT);
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function paymentMethodDataProvider()
  100. {
  101. /** @var $helper \Magento\Payment\Helper\Data */
  102. $helper = Bootstrap::getObjectManager()->get(\Magento\Payment\Helper\Data::class);
  103. $result = [];
  104. foreach ($helper->getPaymentMethods() as $code => $method) {
  105. $result[] = [$code, $method['model']];
  106. }
  107. return $result;
  108. }
  109. /**
  110. * @param string $path
  111. * @return \RegexIterator
  112. */
  113. private function collectFiles($path)
  114. {
  115. $ds = preg_quote(DIRECTORY_SEPARATOR);
  116. $flags = \FilesystemIterator::CURRENT_AS_FILEINFO
  117. | \FilesystemIterator::SKIP_DOTS;
  118. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, $flags));
  119. return new \RegexIterator(
  120. $iterator,
  121. '#' . $ds . 'etc' . $ds . 'di\.php$#',
  122. \RegexIterator::MATCH,
  123. \RegexIterator::USE_KEY
  124. );
  125. }
  126. /**
  127. * @return array
  128. */
  129. private function getTestConfiguration()
  130. {
  131. $result = [];
  132. $ds = DIRECTORY_SEPARATOR;
  133. $path = __DIR__ . $ds . str_repeat('..' . $ds, 5) . 'Magento';
  134. foreach ($this->collectFiles($path) as $file) {
  135. $config = include $file->getPathname();
  136. $result = array_replace_recursive($result, $config);
  137. }
  138. return $result;
  139. }
  140. }