Shardj / zf1-future

PHP 8.1 compatible version of ZF1
BSD 3-Clause "New" or "Revised" License
436 stars 187 forks source link

PHPUnit >= 5 compatibility #44

Closed francescozanoni closed 4 years ago

francescozanoni commented 4 years ago

Hi, man. Currently only PHPUnit < 5 is supported out-of-the-box. My VERY DIRTY solution is the following script, executed after each composer install and composer update:

<?php
/**
 * Zend_Test_PHPUnit fix for PHPUnit > 4
 *
 * This fix is required because PHPUnit > 4 uses namespaces.
 *
 * @author Francesco Zanoni
 * @version 2020-01-05
 */

$basePath = realpath(__DIR__ . "/../vendor/shardj/zf1-future/library/Zend/Test/PHPUnit");

// ########################################################################

$filePath = $basePath . "/ControllerTestCase.php";

file_put_contents(
    $filePath,
    str_replace(
        [
            "/** @see Zend_Controller_Front */",
            "require_once 'Zend/Controller/Front.php';",
            "/** @see Zend_Controller_Action_HelperBroker */",
            "require_once 'Zend/Controller/Action/HelperBroker.php';",
            "/** @see Zend_Layout */",
            "require_once 'Zend/Layout.php';",
            "/** @see Zend_Session */",
            "require_once 'Zend/Session.php';",
            "/** @see Zend_Registry */",
            "require_once 'Zend/Registry.php';",
            "require_once 'Zend/Exception.php';",
            "require_once 'Zend/Loader.php';",
            "require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';",
            "require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';",
            "require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';",
            "require_once 'Zend/Controller/Request/HttpTestCase.php';",
            "require_once 'Zend/Dom/Query.php';",
        ],
        '',
        file_get_contents($filePath)
    )
);

file_put_contents(
    $filePath,
    str_replace(
        ' extends PHPUnit_Framework_TestCase',
        ' extends \\PHPUnit\\Framework\\TestCase',
        file_get_contents($filePath)
    )
);

file_put_contents(
    $filePath,
    str_replace(
        ' instanceof PHPUnit_Framework_TestCase',
        ' instanceof \\PHPUnit\\Framework\\TestCase',
        file_get_contents($filePath)
    )
);

file_put_contents(
    $filePath,
    str_replace(
        'PHPUnit_Runner_Version',
        '\\PHPUnit\\Runner\\Version',
        file_get_contents($filePath)
    )
);

file_put_contents(
    $filePath,
    str_replace(
        'new Zend_Test_PHPUnit_Constraint_DomQuery(',
        'new Zend_Test_PHPUnit_Constraint_DomQuery41(',
        file_get_contents($filePath)
    )
);

file_put_contents(
    $filePath,
    str_replace(
        'new Zend_Test_PHPUnit_Constraint_Redirect(',
        'new Zend_Test_PHPUnit_Constraint_Redirect41(',
        file_get_contents($filePath)
    )
);

file_put_contents(
    $filePath,
    str_replace(
        'new Zend_Test_PHPUnit_Constraint_ResponseHeader(',
        'new Zend_Test_PHPUnit_Constraint_ResponseHeader41(',
        file_get_contents($filePath)
    )
);

// ########################################################################

$filePath = $basePath . "/Constraint/DomQuery41.php";

file_put_contents(
    $filePath,
    str_replace(
        'extends PHPUnit_Framework_Constraint',
        'extends \\PHPUnit\\Framework\\Constraint\\Constraint',
        file_get_contents($filePath)
    )
);

// ########################################################################

$filePath = $basePath . "/Constraint/Redirect41.php";

file_put_contents(
    $filePath,
    str_replace(
        'extends PHPUnit_Framework_Constraint',
        'extends \\PHPUnit\\Framework\\Constraint\\Constraint',
        file_get_contents($filePath)
    )
);

// ########################################################################

$filePath = $basePath . "/Constraint/ResponseHeader41.php";

file_put_contents(
    $filePath,
    str_replace(
        'extends PHPUnit_Framework_Constraint',
        'extends \\PHPUnit\\Framework\\Constraint\\Constraint',
        file_get_contents($filePath)
    )
);

// ########################################################################

$filePath = $basePath . "/Constraint/Exception.php";

file_put_contents(
    $filePath,
    str_replace(
        'extends PHPUnit_Framework_ExpectationFailedException',
        'extends \\PHPUnit\\Framework\\ExpectationFailedException',
        file_get_contents($filePath)
    )
);

Couldn't this be achieved in a more elegant and convenient way? Any advice is welcome.

Thanks in advance!

Francesco

Shardj commented 4 years ago

We just bootstrap with this file:

<?php

define('TEST_EXECUTION', true);

require_once dirname(__FILE__) . '/../..' . '/app-loader.php';
class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {}

I'm not really sure as to what you're doing with all of that to be honest.

francescozanoni commented 4 years ago

Thank you soooooooooo much, I only needed this:

class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {}
class PHPUnit_Runner_Version extends \PHPUnit\Runner\Version {}
abstract class PHPUnit_Framework_Constraint extends \PHPUnit\Framework\Constraint\Constraint {}
abstract class PHPUnit_Framework_ExpectationFailedException extends \PHPUnit\Framework\ExpectationFailedException {}

:-)

ikari-pl commented 3 years ago

@francescozanoni I'll just leave a note that in PHPUnit 8:

Fatal error: Class PHPUnit_Runner_Version may not inherit from final class (PHPUnit\Runner\Version) 
Fatal error: Class PHPUnit_Framework_ExpectationFailedException may not inherit from final class (PHPUnit\Framework\ExpectationFailedException) 

as I'm trying to find a combination of patches and placeholders for me to work :(