|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace IntegerNet\SessionUnblocker\Test\Util; |
| 5 | + |
| 6 | +use Magento\TestFramework\Helper\Bootstrap; |
| 7 | + |
| 8 | +class MethodLog |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var string[] |
| 12 | + */ |
| 13 | + private $calls = []; |
| 14 | + /** |
| 15 | + * @var string[] |
| 16 | + */ |
| 17 | + private $callDetails = []; |
| 18 | + |
| 19 | + private const SESSION_START = 'session_start'; |
| 20 | + |
| 21 | + private const SESSION_MODIFY = 'session_modify'; |
| 22 | + |
| 23 | + private const CONTROLLER_ACTION = 'controller_action'; |
| 24 | + |
| 25 | + private const SESSION_WRITE_CLOSE = 'session_write_close'; |
| 26 | + |
| 27 | + public static function instance(): self |
| 28 | + { |
| 29 | + // should only be used in integration test context |
| 30 | + if (class_exists(Bootstrap::class)) { |
| 31 | + return Bootstrap::getObjectManager()->get(MethodLog::class); |
| 32 | + } |
| 33 | + |
| 34 | + return new self; |
| 35 | + } |
| 36 | + |
| 37 | + public function logSessionStart(string $class, string $method, string $namespace): void |
| 38 | + { |
| 39 | + $this->log(self::SESSION_START, $class, $method, $namespace); |
| 40 | + } |
| 41 | + |
| 42 | + public function logSessionModify(string $class, string $method, string $namespace, string $key) |
| 43 | + { |
| 44 | + $this->log(self::SESSION_MODIFY, $class, $method, $namespace, $key); |
| 45 | + } |
| 46 | + |
| 47 | + public function logControllerAction(string $class, string $method) |
| 48 | + { |
| 49 | + $this->log(self::CONTROLLER_ACTION, $class, $method); |
| 50 | + } |
| 51 | + |
| 52 | + public function logWriteClose(string $class, string $method) |
| 53 | + { |
| 54 | + $this->log(self::SESSION_WRITE_CLOSE, $class, $method); |
| 55 | + } |
| 56 | + |
| 57 | + private function log(string $category, string $class, string $method, ...$args): void |
| 58 | + { |
| 59 | + $this->calls[] = $category; |
| 60 | + $this->callDetails[] = $class . '::' . $method . '(' . implode(',', $args) . ')'; |
| 61 | + } |
| 62 | + |
| 63 | + public function asString(): string |
| 64 | + { |
| 65 | + return implode("\n", $this->callDetails); |
| 66 | + } |
| 67 | + |
| 68 | + public function hasSessionStartAfterAction(): bool |
| 69 | + { |
| 70 | + $actionPosition = array_search(self::CONTROLLER_ACTION, $this->calls); |
| 71 | + $sessionStartPositions = array_keys($this->calls, self::SESSION_START); |
| 72 | + if (empty($sessionStartPositions)) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + return max($sessionStartPositions) > $actionPosition; |
| 76 | + } |
| 77 | + |
| 78 | + public function hasSessionStartBeforeAction(): bool |
| 79 | + { |
| 80 | + $actionPosition = array_search(self::CONTROLLER_ACTION, $this->calls); |
| 81 | + $sessionStartPositions = array_keys($this->calls, self::SESSION_START); |
| 82 | + if (empty($sessionStartPositions)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + return min($sessionStartPositions) < $actionPosition; |
| 86 | + } |
| 87 | + |
| 88 | + public function hasSessionWriteCloseBeforeAction(): bool |
| 89 | + { |
| 90 | + $actionPosition = array_search(self::CONTROLLER_ACTION, $this->calls); |
| 91 | + $writeClosePositions = array_keys($this->calls, self::SESSION_WRITE_CLOSE); |
| 92 | + if (empty($writeClosePositions)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + return max($writeClosePositions) < $actionPosition; |
| 96 | + } |
| 97 | + |
| 98 | + public function hasModifyAfterSessionWriteClose() |
| 99 | + { |
| 100 | + $writeClosePosition = array_search(self::SESSION_WRITE_CLOSE, $this->calls); |
| 101 | + $modifyPositions = array_keys($this->calls, self::SESSION_MODIFY); |
| 102 | + if (empty($modifyPositions)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + return max($modifyPositions) > $writeClosePosition; |
| 106 | + } |
| 107 | + |
| 108 | + public function reset(): void |
| 109 | + { |
| 110 | + $this->calls = []; |
| 111 | + $this->callDetails = []; |
| 112 | + } |
| 113 | +} |
0 commit comments