Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Double Code Generator does not work when PHPUnit is used from PHAR on PHP 8.4 #6093

Closed
jankal opened this issue Jan 2, 2025 · 14 comments
Labels
feature/test-doubles Test Stubs and Mock Objects installation/phar type/bug Something is broken version/11 Something affects PHPUnit 11 version/12 Something affects PHPUnit 12

Comments

@jankal
Copy link

jankal commented Jan 2, 2025

Q A
PHPUnit version 11.5.1
PHP version 8.4.2
Installation Method PHAR

Summary

Tests using the createMock() method run flawlessly with phpunit installed as a composer package.
They do not run when using the PHAR.

Current behavior

Each test using createMock() outputs assert(class_exists(PropertyHookType::class)) - which looks a lot like an AssertionError.

How to reproduce

Run a test-suite using createMock() with the PHAR version of phpunit.

Expected behavior

Tests should run as expected. No assertion fails.

@jankal jankal added the type/bug Something is broken label Jan 2, 2025
@jankal
Copy link
Author

jankal commented Jan 2, 2025

This error currently happens even if property hooks are not used.
My best guess about why this happens: the PHAR building tool aliases the global classPropertyHookType to a internal PHAR class which does not exist.

image

I first thought #6044 might remedy this for mocks where no property hooks are involved... but nope. We would need to figure out if property hooks are used without touching PropertyHookType - which is not possible.

@jankal jankal changed the title assert(class_exists(PropertyHookType::class)) breaks PHAR when using createMock() assert(class_exists(PropertyHookType::class)) breaks PHAR when using createMock() with property hooks Jan 2, 2025
@jankal
Copy link
Author

jankal commented Jan 2, 2025

relates to humbug/php-scoper#1092

@jankal jankal changed the title assert(class_exists(PropertyHookType::class)) breaks PHAR when using createMock() with property hooks assert(class_exists(PropertyHookType::class)) breaks PHAR when using createMock() Jan 2, 2025
@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Jan 2, 2025

Thank you for your report. Unfortunately, I am not able to reproduce it:

Issue6093Test.php

<?php declare(strict_types=1);
namespace PHPUnit\TestFixture\Issue6093;

use PHPUnit\Framework\TestCase;

interface I
{
    public function m(): bool;
}

final class Issue6093Test extends TestCase
{
    public function testOne(): void
    {
        $i = $this->createMock(I::class);
        
        $i->expects($this->once())
          ->method('m');

        $i->m();
    }
}
❯ php phpunit-11.5.2.phar Issue6093Test.php
PHPUnit 11.5.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.2

.                                                                   1 / 1 (100%)

Time: 00:00.002, Memory: 25.34 MB

OK (1 test, 1 assertion)

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@sebastianbergmann sebastianbergmann added status/waiting-for-feedback Waiting for feedback from original reporter feature/test-doubles Test Stubs and Mock Objects installation/phar labels Jan 2, 2025
@sebastianbergmann sebastianbergmann changed the title assert(class_exists(PropertyHookType::class)) breaks PHAR when using createMock() Test Double Code Generator does not work when PHPUnit is used from PHAR on PHP 8.4 Jan 2, 2025
@sebastianbergmann sebastianbergmann added version/11 Something affects PHPUnit 11 version/12 Something affects PHPUnit 12 labels Jan 2, 2025
@sebastianbergmann
Copy link
Owner

relates to humbug/php-scoper#1092

Not really, as the issue you reference is about the php-scoper not being able to run on PHP 8.4 which means that PHPUnit's PHAR cannot currently be built on PHP 8.4.

The issue you are reporting here seems to be rooted in PHP-Scoper wrongly transforming PHP code when PHPUnit's PHAR is built.

@sebastianbergmann
Copy link
Owner

@theofidry What do you think?

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Jan 2, 2025

Because we transform PHPUnit's code using PHP-Scoper on PHP 8.3, PHP-Scoper does not know about the built-in PropertyHookType class and therefore wrongly prefixes it with PHPUnitPHAR\.

Please disregard what I wrote in #6093 (comment) and sorry for the noise.

@jankal
Copy link
Author

jankal commented Jan 2, 2025

@sebastianbergmann here is my reproduction:

<?php

namespace App\Tests;

use PHPUnit\Framework\TestCase;

class Abc {
    public int $a;
}

class MinimalTest extends TestCase
{
    public function testMinimal() {
        $mock = $this->createMock(Abc::class);
        $this->assertTrue($mock instanceof Abc);
    }
}

Using the binary:

vendor/bin/phpunit tests/MinimalTest.php
PHPUnit 11.5.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.2
Configuration: /Users/jankal/projects/internal/app/phpunit.xml.dist

.                                                                   1 / 1 (100%)

Time: 00:00.004, Memory: 10.00 MB

OK (1 test, 1 assertion)

Using the PHAR:

./phpunit-11.5.1.phar tests/MinimalTest.php
PHPUnit 11.5.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.2
Configuration: /Users/jankal/projects/internal/app/phpunit.xml.dist

F                                                                   1 / 1 (100%)

Time: 00:00.001, Memory: 27.34 MB

There was 1 failure:

1) App\Tests\MinimalTest::testMinimal
assert(class_exists(PropertyHookType::class))

/Users/jankal/projects/internal/app/tests/MinimalTest.php:14

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
                 
  exit status 1  
                 

My phpunit.xml.dist:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
>
    <php>
        <ini name="display_errors" value="1" />
        <ini name="error_reporting" value="-1" />
        <server name="APP_ENV" value="test" force="true" />
        <server name="SHELL_VERBOSITY" value="-1" />
    </php>

    <source>
        <include>
            <directory>src</directory>
        </include>
    </source>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <extensions>
    </extensions>
</phpunit>

@jankal
Copy link
Author

jankal commented Jan 2, 2025

@sebastianbergmann I had mentioned the relationship to php-scoper in #6093 (comment) because the issue title there reads "PHP-Scoper does not work in PHP 8.4" which is a fact discovered in this case as well - even though the causes are different ones.

We might need to track this in separate issues with php-scoper.

@sebastianbergmann
Copy link
Owner

We might need to track this in separate issues with php-scoper.

I do not think so, see #6093 (comment).

@sebastianbergmann
Copy link
Owner

Blocked by humbug/php-scoper#1099.

@sebastianbergmann
Copy link
Owner

@jankal
Copy link
Author

jankal commented Jan 2, 2025

@sebastianbergmann the snapshot version works.

> ./phpunit-snapshot.phar tests/MinimalTest.php
PHPUnit 11.5.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.2
Configuration: /Users/jankal/projects/internal/app/phpunit.xml.dist

.                                                                   1 / 1 (100%)

Time: 00:00.001, Memory: 27.34 MB

OK (1 test, 1 assertion)

@jankal
Copy link
Author

jankal commented Jan 13, 2025

@sebastianbergmann humbug/php-scoper#1100 was merged thus we can move forwared here.
humbug/php-scoper:0.18.16 contains the fix. - https://github.com/humbug/php-scoper/commits/0.18.16

@jankal
Copy link
Author

jankal commented Jan 13, 2025

Ah, it seems you have already updated the dep in bbd266e - nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Test Stubs and Mock Objects installation/phar type/bug Something is broken version/11 Something affects PHPUnit 11 version/12 Something affects PHPUnit 12
Projects
None yet
Development

No branches or pull requests

2 participants