123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace Codeception\Extension;
- use Codeception\Event\PrintResultEvent;
- use Codeception\Events;
- use Codeception\Extension;
- use Codeception\Test\Descriptor;
- class RunFailed extends Extension
- {
- public static $events = [
- Events::RESULT_PRINT_AFTER => 'saveFailed'
- ];
-
- protected $group = 'failed';
- public function _initialize()
- {
- if (array_key_exists('fail-group', $this->config) && $this->config['fail-group']) {
- $this->group = $this->config['fail-group'];
- }
- $logPath = str_replace($this->getRootDir(), '', $this->getLogDir());
- $this->_reconfigure(['groups' => [$this->group => $logPath . $this->group]]);
- }
- public function saveFailed(PrintResultEvent $e)
- {
- $file = $this->getLogDir() . $this->group;
- $result = $e->getResult();
- if ($result->wasSuccessful()) {
- if (is_file($file)) {
- unlink($file);
- }
- return;
- }
- $output = [];
- foreach ($result->failures() as $fail) {
- $output[] = $this->localizePath(Descriptor::getTestFullName($fail->failedTest()));
- }
- foreach ($result->errors() as $fail) {
- $output[] = $this->localizePath(Descriptor::getTestFullName($fail->failedTest()));
- }
- file_put_contents($file, implode("\n", $output));
- }
- protected function localizePath($path)
- {
- $root = realpath($this->getRootDir()) . DIRECTORY_SEPARATOR;
- if (substr($path, 0, strlen($root)) == $root) {
- return substr($path, strlen($root));
- }
- return $path;
- }
- }
|