class PhpRequirementsTest

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Core/Utility/PhpRequirementsTest.php \Drupal\Tests\Core\Utility\PhpRequirementsTest
  2. 11.x core/tests/Drupal/Tests/Core/Utility/PhpRequirementsTest.php \Drupal\Tests\Core\Utility\PhpRequirementsTest

Tests the \Drupal\Core\Utility\PhpRequirements class.

@coversDefaultClass \Drupal\Core\Utility\PhpRequirements @group Utility

Hierarchy

Expanded class hierarchy of PhpRequirementsTest

File

core/tests/Drupal/Tests/Core/Utility/PhpRequirementsTest.php, line 14

Namespace

Drupal\Tests\Core\Utility
View source
class PhpRequirementsTest extends UnitTestCase {
    
    /**
     * Ensures that PHP EOL dates are valid.
     *
     * This ensures that that all of the PHP EOL Date items are valid ISO 8601
     * dates and are keyed by a valid version number.
     */
    public function testPhpEolDates() : void {
        $reflected = new \ReflectionClass(PhpRequirements::class);
        $php_eol_dates = $reflected->getStaticProperties()['phpEolDates'];
        foreach ($php_eol_dates as $version => $eol_date) {
            // Ensure that all of the version numbers are defined in a superset of
            // semver: 'major.minor.patch-modifier', where (unlike in semver) all
            // parts but the major are optional.
            // @see version_compare()
            $this->assertMatchesRegularExpression('/^([0-9]+)(\\.([0-9]+)(\\.([0-9]+)(-[A-Za-z0-9]+)?)?)?$/', $version);
            // Ensure that all of the EOL dates are defined using ISO 8601 format.
            $this->assertMatchesRegularExpression('/^([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])$/', $eol_date);
        }
        // Ensure that the EOL list is sorted in an ascending order by the date. If
        // there are multiple versions EOL on the same day, sort by the PHP
        // version.
        uksort($php_eol_dates, function ($a, $b) use ($php_eol_dates) {
            $a_date = strtotime($php_eol_dates[$a]);
            $b_date = strtotime($php_eol_dates[$b]);
            if ($a_date === $b_date) {
                return $a <=> $b;
            }
            return $a_date <=> $b_date;
        });
        $this->assertSame($php_eol_dates, $reflected->getStaticProperties()['phpEolDates']);
    }
    
    /**
     * Tests the minimum supported PHP for valid scenarios.
     *
     * @param string $date_string
     *   A valid PHP date string for the date to check.
     * @param string $drupal_minimum_php
     *   The PHP minimum version hard requirement for the Drupal version, below
     *   which Drupal cannot be installed or updated, typically
     *   \Drupal::MINIMUM_PHP.
     * @param string[] $php_eol_dates
     *   Associative array of PHP version EOL date strings, keyed by the PHP minor
     *   version.
     * @param string $expected_php_version
     *   The PHP version the test should recommend.
     *
     * @covers ::getMinimumSupportedPhp
     *
     * @dataProvider providerMinimumSupportedPhp
     */
    public function testMinimumSupportedPhp(string $date_string, string $drupal_minimum_php, array $php_eol_dates, string $expected_php_version) : void {
        $reflected = new \ReflectionClass(PhpRequirements::class);
        $prop = $reflected->getProperty('drupalMinimumPhp');
        $prop->setAccessible(TRUE);
        $prop->setValue($drupal_minimum_php);
        $prop = $reflected->getProperty('phpEolDates');
        $prop->setAccessible(TRUE);
        $prop->setValue($php_eol_dates);
        $date = new \DateTime($date_string);
        $this->assertSame($expected_php_version, PhpRequirements::getMinimumSupportedPhp($date));
    }
    
    /**
     * Data provider for ::testMinimumSupportedPhp().
     *
     * See the parameter documentation of testMinimumSupportedPhp() for the test
     * array structure. The last element is the expected minimum supported PHP.
     *
     * @return \Generator
     *   Test scenarios.
     */
    public function providerMinimumSupportedPhp() : \Generator {
        $eol_lists = [];
        // Test against the known valid data from 9.0.0 to 9.3.0.
        $eol_lists['d9_release'] = [
            '7.2' => '2020-11-30',
            '7.3' => '2021-12-06',
            '7.4' => '2022-11-28',
            '8.0' => '2023-11-26',
            '8.1' => '2024-11-25',
        ];
        // The actual situation the day of 9.0.0's release.
        (yield [
            '2020-06-03',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.3.0',
        ]);
        // If Drupal's MINIMUM_PHP had been 7.3.12 then.
        (yield [
            '2020-06-03',
            '7.3.12',
            $eol_lists['d9_release'],
            '7.3.12',
        ]);
        // If Drupal's MINIMUM_PHP had been 7.2.17 then.
        (yield [
            '2020-06-03',
            '7.2.17',
            $eol_lists['d9_release'],
            '7.2.17',
        ]);
        // If Drupal's MINIMUM_PHP had been 7.1.5 then.
        (yield [
            '2020-06-03',
            '7.1.5',
            $eol_lists['d9_release'],
            '7.2',
        ]);
        // If the PHP EOL date list were empty.
        (yield [
            '2020-06-03',
            '7.3.0',
            [],
            '7.3.0',
        ]);
        // Cases around PHP 7.2's EOL.
        (yield [
            '2020-11-29',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.3.0',
        ]);
        (yield [
            '2020-11-30',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.3.0',
        ]);
        (yield [
            '2020-12-01',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.3.0',
        ]);
        // Cases around PHP 7.3's EOL.
        (yield [
            '2021-12-05',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.3.0',
        ]);
        (yield [
            '2021-12-06',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.4',
        ]);
        (yield [
            '2021-12-07',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.4',
        ]);
        // Cases around PHP 7.4's EOL.
        (yield [
            '2022-11-27',
            '7.3.0',
            $eol_lists['d9_release'],
            '7.4',
        ]);
        (yield [
            '2022-11-28',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.0',
        ]);
        (yield [
            '2022-11-29',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.0',
        ]);
        // Cases around PHP 8.0's EOL.
        (yield [
            '2023-11-25',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.0',
        ]);
        (yield [
            '2023-11-26',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2023-11-27',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        // Cases around PHP 8.1's EOL, without any data for 8.2.
        (yield [
            '2024-11-24',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2024-11-25',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2024-11-26',
            '7.3.0',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        // Cases for Drupal 10, with its current 8.0.2 MINIMUM_PHP, prior to PHP
        // 8.0's EOL.
        (yield [
            '2021-12-05',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        (yield [
            '2021-12-06',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        (yield [
            '2021-12-07',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        (yield [
            '2022-11-27',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        (yield [
            '2022-11-28',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        (yield [
            '2022-11-29',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        // Cases for Drupal 10 around PHP 8.0's EOL.
        (yield [
            '2023-11-25',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.0.2',
        ]);
        (yield [
            '2023-11-26',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2023-11-27',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        // Cases for Drupal 10 around and after PHP 8.1's EOL, without any data
        // for 8.2.
        (yield [
            '2024-11-24',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2024-11-25',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2024-11-26',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        (yield [
            '2027-01-01',
            '8.0.2',
            $eol_lists['d9_release'],
            '8.1',
        ]);
        // Test against a hypothetical set of PHP versions that have an LTS
        // (supported longer than subsequent versions).
        $eol_lists['php_with_lts'] = $eol_lists['d9_release'];
        // Ensure that the PHP version with longest support is listed last.
        unset($eol_lists['php_with_lts']['7.4']);
        $eol_lists['php_with_lts']['7.4'] = '2025-11-28';
        (yield [
            '2021-12-05',
            '7.3',
            $eol_lists['php_with_lts'],
            '7.3',
        ]);
        (yield [
            '2021-12-06',
            '7.3',
            $eol_lists['php_with_lts'],
            '7.4',
        ]);
        (yield [
            '2022-11-28',
            '7.3',
            $eol_lists['php_with_lts'],
            '7.4',
        ]);
        (yield [
            '2023-11-26',
            '7.3',
            $eol_lists['php_with_lts'],
            '7.4',
        ]);
        (yield [
            '2024-11-25',
            '7.3',
            $eol_lists['php_with_lts'],
            '7.4',
        ]);
        (yield [
            '2025-12-01',
            '7.3',
            $eol_lists['php_with_lts'],
            '7.4',
        ]);
        // Case with multiple versions EOL on the same day.
        $eol_lists['same_eol_date'] = $eol_lists['d9_release'];
        $eol_lists['same_eol_date']['8.2'] = $eol_lists['same_eol_date']['8.1'];
        (yield [
            '2021-12-05',
            '7.3',
            $eol_lists['same_eol_date'],
            '7.3',
        ]);
        (yield [
            '2023-11-27',
            '8.0.2',
            $eol_lists['same_eol_date'],
            '8.1',
        ]);
        (yield [
            '2027-07-31',
            '8.0.2',
            $eol_lists['same_eol_date'],
            '8.2',
        ]);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
PhpRequirementsTest::providerMinimumSupportedPhp public function Data provider for ::testMinimumSupportedPhp().
PhpRequirementsTest::testMinimumSupportedPhp public function Tests the minimum supported PHP for valid scenarios.
PhpRequirementsTest::testPhpEolDates public function Ensures that PHP EOL dates are valid.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 338
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.