<commit_before><?php

namespace Lmc\Steward\Listener;

use Lmc\Steward\Test\AbstractTestCase;
use Nette\Utils\Strings;

/**
 * Listener to take snapshots of the page (screenshot and html snapshot) on each error or failure.
 */
class SnapshotListener extends \PHPUnit_Framework_BaseTestListener
{
    public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
    {
        $this->takeSnapshot($test, $e, $time);
    }

    public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
    {
        $this->takeSnapshot($test, $e, $time);
    }

    /**
     * Take screenshot and save it.
     * @param AbstractTestCase $test
     * @param \Exception $e
     * @param $time
     */
    protected function takeSnapshot(AbstractTestCase $test, \Exception $e, $time)
    {
        $savePath = LOGS_DIR . '/';

        $testIdentifier = Strings::webalize(get_class($test), null, $lower = false)
            . '-'
            . Strings::webalize($test->getName(), null, $lower = false)
            . '-'
            . date('Y-m-d-H-i-s');

        if (!$test->wd instanceof \RemoteWebDriver) {
            $test->warn('WebDriver instance not found, cannot take screenshot.');
            return;
        }

        $test->appendTestLog(
            'Taking snapshots of page "%s" because: "%s"',
            $test->wd->getCurrentURL(),
            $e->getMessage()
        );

        // Save PNG screenshot
        $screenshotPath = $savePath . $testIdentifier . '.png';
        $test->wd->takeScreenshot($screenshotPath);
        $test->appendTestLog('Screenshot saved to file "%s" ', $this->getSnapshotUrl($screenshotPath));

        // Save HTML snapshot of page
        $htmlPath = $savePath . $testIdentifier . '.html';
        file_put_contents($htmlPath, $test->wd->getPageSource());
        $test->appendTestLog('HTML snapshot saved to file "%s" ', $this->getSnapshotUrl($htmlPath));
    }

    /**
     * Get url based on relative path of specific snapshot.
     * In our implementation we prepend artifact's URL to given relative path to make it clickable in Jenkins output.
     *
     * @param $path
     * @return string<commit_msg>Simplify test output when taking page snapshot to not repeat same error message twice<commit_after><?php

namespace Lmc\Steward\Listener;

use Lmc\Steward\Test\AbstractTestCase;
use Nette\Utils\Strings;

/**
 * Listener to take snapshots of the page (screenshot and html snapshot) on each error or failure.
 */
class SnapshotListener extends \PHPUnit_Framework_BaseTestListener
{
    public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
    {
        $this->takeSnapshot($test);
    }

    public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
    {
        $this->takeSnapshot($test);
    }

    /**
     * Take screenshot and save it.
     *
     * @param AbstractTestCase $test
     */
    protected function takeSnapshot(AbstractTestCase $test)
    {
        $savePath = LOGS_DIR . '/';

        $testIdentifier = Strings::webalize(get_class($test), null, $lower = false)
            . '-'
            . Strings::webalize($test->getName(), null, $lower = false)
            . '-'
            . date('Y-m-d-H-i-s');

        if (!$test->wd instanceof \RemoteWebDriver) {
            $test->warn('WebDriver instance not found, cannot take screenshot.');
            return;
        }

        $test->appendTestLog(
            'Test failed on page "%s", taking page snapshots:',
            $test->wd->getCurrentURL()
        );

        // Save PNG screenshot
        $screenshotPath = $savePath . $testIdentifier . '.png';
        $test->wd->takeScreenshot($screenshotPath);
        $test->appendTestLog('Screenshot saved to file "%s" ', $this->getSnapshotUrl($screenshotPath));

        // Save HTML snapshot of page
        $htmlPath = $savePath . $testIdentifier . '.html';
        file_put_contents($htmlPath, $test->wd->getPageSource());
        $test->appendTestLog('HTML snapshot saved to file "%s" ', $this->getSnapshotUrl($htmlPath));
    }

    /**
     * Get url based on relative path of specific snapshot.
     * In our implementation we prepend artifact's URL to given relative path to make it clickable in Jenkins output.
     *
     * @param $path
     * @return string
{'fraction_non_alphanumeric': 0.12819926873857404, 'fraction_numerical': 0.0, 'mean_word_length': 2.596548890714873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4491913', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1241, 'n_words': 330}