Cache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\Annotation;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * Implementation of the @magentoCache DocBlock annotation
  10. */
  11. class Cache
  12. {
  13. /**
  14. * Original values for cache type states
  15. *
  16. * @var array
  17. */
  18. private $origValues = [];
  19. /**
  20. * Handler for 'startTest' event
  21. *
  22. * @param \PHPUnit\Framework\TestCase $test
  23. * @return void
  24. */
  25. public function startTest(\PHPUnit\Framework\TestCase $test)
  26. {
  27. $source = $test->getAnnotations();
  28. if (isset($source['method']['magentoCache'])) {
  29. $annotations = $source['method']['magentoCache'];
  30. } elseif (isset($source['class']['magentoCache'])) {
  31. $annotations = $source['class']['magentoCache'];
  32. } else {
  33. return;
  34. }
  35. $this->setValues($this->parseValues($annotations, $test), $test);
  36. }
  37. /**
  38. * Handler for 'endTest' event
  39. *
  40. * @param \PHPUnit\Framework\TestCase $test
  41. * @return void
  42. */
  43. public function endTest(\PHPUnit\Framework\TestCase $test)
  44. {
  45. if ($this->origValues) {
  46. $this->setValues($this->origValues, $test);
  47. $this->origValues = [];
  48. }
  49. }
  50. /**
  51. * Determines from docblock annotations which cache types to set
  52. *
  53. * @param array $annotations
  54. * @param \PHPUnit\Framework\TestCase $test
  55. * @return array
  56. */
  57. private function parseValues($annotations, \PHPUnit\Framework\TestCase $test)
  58. {
  59. $result = [];
  60. $typeList = self::getTypeList();
  61. foreach ($annotations as $subject) {
  62. if (!preg_match('/^([a-z_]+)\s(enabled|disabled)$/', $subject, $matches)) {
  63. self::fail("Invalid @magentoCache declaration: '{$subject}'", $test);
  64. }
  65. list(, $requestedType, $isEnabled) = $matches;
  66. $isEnabled = $isEnabled == 'enabled' ? 1 : 0;
  67. if ('all' === $requestedType) {
  68. $result = [];
  69. foreach ($typeList->getTypes() as $type) {
  70. $result[$type['id']] = $isEnabled;
  71. }
  72. } else {
  73. $result[$requestedType] = $isEnabled;
  74. }
  75. }
  76. return $result;
  77. }
  78. /**
  79. * Sets the values of cache types
  80. *
  81. * @param array $values
  82. * @param \PHPUnit\Framework\TestCase $test
  83. */
  84. private function setValues($values, \PHPUnit\Framework\TestCase $test)
  85. {
  86. $typeList = self::getTypeList();
  87. if (!$this->origValues) {
  88. $this->origValues = [];
  89. foreach ($typeList->getTypes() as $type => $row) {
  90. $this->origValues[$type] = $row['status'];
  91. }
  92. }
  93. /** @var \Magento\Framework\App\Cache\StateInterface $states */
  94. $states = Bootstrap::getInstance()->getObjectManager()->get(\Magento\Framework\App\Cache\StateInterface::class);
  95. foreach ($values as $type => $isEnabled) {
  96. if (!isset($this->origValues[$type])) {
  97. self::fail("Unknown cache type specified: '{$type}' in @magentoCache", $test);
  98. }
  99. $states->setEnabled($type, $isEnabled);
  100. }
  101. }
  102. /**
  103. * Getter for cache types list
  104. *
  105. * @return \Magento\Framework\App\Cache\TypeListInterface
  106. */
  107. private static function getTypeList()
  108. {
  109. return Bootstrap::getInstance()->getObjectManager()->get(\Magento\Framework\App\Cache\TypeListInterface::class);
  110. }
  111. /**
  112. * Fails the test with specified error message
  113. *
  114. * @param string $message
  115. * @param \PHPUnit\Framework\TestCase $test
  116. * @throws \Exception
  117. */
  118. private static function fail($message, \PHPUnit\Framework\TestCase $test)
  119. {
  120. $test->fail("{$message} in the test '{$test->toString()}'");
  121. throw new \Exception('The above line was supposed to throw an exception.');
  122. }
  123. }