<commit_before><?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Runner;

use const PHP_EOL;
use function file_put_contents;
use function sys_get_temp_dir;
use function touch;
use function unlink;
use PHPUnit\Event\Facade;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\PHP\AbstractPhpProcess;

/**
 * @medium
 */
final class PhptTestCaseTest extends TestCase
{
    private const EXPECT_CONTENT = <<<'EOF'
--TEST--
EXPECT test
--FILE--
<?php echo "Hello PHPUnit!"; ?>
--EXPECT--
Hello PHPUnit!
EOF;
    private string $filename;
    private ?PhptTestCase $testCase;
    private AbstractPhpProcess|MockObject|null $phpProcess;

    protected function setUp(): void
    {
        $this->filename = sys_get_temp_dir() . '/phpunit.phpt';

        touch($this->filename);

        $this->phpProcess = $this->getMockForAbstractClass(AbstractPhpProcess::class, [], '', false);
        $this->testCase   = new PhptTestCase($this->filename, $this->phpProcess);
    }

    protected function tearDown(): void
    {
        @unlink($this->filename);

        $this->phpProcess = null;
        $this->testCase   = null;
    }

    public function testAlwaysReportsNumberOfAssertionsIsOne(): void
    {
        $this->assertSame(1, $this->testCase->numberOfAssertionsPerformed());
    }

    public function testAlwaysReportsItDoesNotUseADataprovider(): void
    {
        $this->assertFalse($this->testCase->usesDataProvider());
    }

    public function testShouldNotRunTestSectionIfSkipifSectionReturnsOutputWithSkipWord(): void
    {
        $skipifSection = '<?php echo "skip: Reason"; ?>' . PHP_EOL;

        $phptContent = self::EXPECT_CONTENT . PHP_EOL;
        $phptContent .= '--SKIPIF--' . PHP_EOL;
        $phptContent .= $skipifSection;

        $this->setPhpContent($phptContent);

        $this->phpProcess
             ->expects($this->once())
             ->method('runJob')
             ->with($skipifSection)
             ->willReturn(['stdout' => 'skip: Reason', 'stderr' => '']);

        $this->testCase->run(new TestResult);
    }

    public function testErrorIsTriggeredForEmptyPhptFile(): void
    {
        $this->setPhpContent('');

        $result = new TestResult;

        Facade::suspend();
        $this->testCase->run($result);
        Facade::resume();

        $this->assertCount(1, $result->errors());
        $this->assertSame('Invalid PHPT file', $result->errors()[0]->thrownException()->getMessage());
    }

    public function testErrorIsTriggeredWhenFileSectionIsMissing(): void
    {
        $this->setPhpContent(
            <<<'EOF'
--TEST--
Something to describe it
--EXPECT--
Something
EOF<commit_msg>Disable event dispatching for this test<commit_after><?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Runner;

use const PHP_EOL;
use function file_put_contents;
use function sys_get_temp_dir;
use function touch;
use function unlink;
use PHPUnit\Event\Facade;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\PHP\AbstractPhpProcess;

/**
 * @medium
 */
final class PhptTestCaseTest extends TestCase
{
    private const EXPECT_CONTENT = <<<'EOF'
--TEST--
EXPECT test
--FILE--
<?php echo "Hello PHPUnit!"; ?>
--EXPECT--
Hello PHPUnit!
EOF;
    private string $filename;
    private ?PhptTestCase $testCase;
    private AbstractPhpProcess|MockObject|null $phpProcess;

    protected function setUp(): void
    {
        $this->filename = sys_get_temp_dir() . '/phpunit.phpt';

        touch($this->filename);

        $this->phpProcess = $this->getMockForAbstractClass(AbstractPhpProcess::class, [], '', false);
        $this->testCase   = new PhptTestCase($this->filename, $this->phpProcess);
    }

    protected function tearDown(): void
    {
        @unlink($this->filename);

        $this->phpProcess = null;
        $this->testCase   = null;
    }

    public function testAlwaysReportsNumberOfAssertionsIsOne(): void
    {
        $this->assertSame(1, $this->testCase->numberOfAssertionsPerformed());
    }

    public function testAlwaysReportsItDoesNotUseADataprovider(): void
    {
        $this->assertFalse($this->testCase->usesDataProvider());
    }

    public function testShouldNotRunTestSectionIfSkipifSectionReturnsOutputWithSkipWord(): void
    {
        $skipifSection = '<?php echo "skip: Reason"; ?>' . PHP_EOL;

        $phptContent = self::EXPECT_CONTENT . PHP_EOL;
        $phptContent .= '--SKIPIF--' . PHP_EOL;
        $phptContent .= $skipifSection;

        $this->setPhpContent($phptContent);

        $this->phpProcess
             ->expects($this->once())
             ->method('runJob')
             ->with($skipifSection)
             ->willReturn(['stdout' => 'skip: Reason', 'stderr' => '']);

        Facade::suspend();
        $this->testCase->run(new TestResult);
        Facade::resume();
    }

    public function testErrorIsTriggeredForEmptyPhptFile(): void
    {
        $this->setPhpContent('');

        $result = new TestResult;

        Facade::suspend();
        $this->testCase->run($result);
        Facade::resume();

        $this->assertCount(1, $result->errors());
        $this->assertSame('Invalid PHPT file', $result->errors()[0]->thrownException()->getMessage());
    }

    public function testErrorIsTriggeredWhenFileSectionIsMissing(): void
    {
        $this->setPhpContent(
            <<<'EOF'
--TEST--
Something to describe it
--EXPECT--
Something
EOF
{'fraction_non_alphanumeric': 0.13915693245302183, 'fraction_numerical': 0.0013543253766717454, 'mean_word_length': 3.1547116736990155, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 87, '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': '1443647', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1748, 'n_words': 372}