Skip to content

make PhpCompatUtil::getPhpVersion() public #970

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

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private function addOperation(string $targetPath, string $templateName, array $v
$variables['relative_path'] = $this->fileManager->relativizePath($targetPath);
$variables['use_attributes'] = $this->phpCompatUtil->canUseAttributes();
$variables['use_typed_properties'] = $this->phpCompatUtil->canUseTypedProperties();
$variables['use_union_types'] = $this->phpCompatUtil->canUseUnionTypes();

$templatePath = $templateName;
if (!file_exists($templatePath)) {
Expand Down
7 changes: 7 additions & 0 deletions src/Util/PhpCompatUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public function canUseTypedProperties(): bool
return version_compare($version, '7.4', '>=');
}

public function canUseUnionTypes(): bool
{
$version = $this->getPhpVersion();

return version_compare($version, '8alpha', '>=');
}

protected function getPhpVersion(): string
{
$rootDirectory = $this->fileManager->getRootDirectory();
Expand Down
116 changes: 54 additions & 62 deletions tests/Util/PhpVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,7 @@ class PhpVersionTest extends TestCase
*/
public function testUsesPhpPlatformFromComposerJsonFileForCanUseAttributes(string $version, bool $expectedResult): void
{
$json = sprintf('{"platform-overrides": {"php": "%s"}}', $version);

$mockFileManager = $this->createMock(FileManager::class);
$mockFileManager
->expects(self::once())
->method('getRootDirectory')
->willReturn('/test')
;

$mockFileManager
->expects(self::once())
->method('fileExists')
->with('/test/composer.lock')
->willReturn(true)
;

$mockFileManager
->expects(self::once())
->method('getFileContents')
->with('/test/composer.lock')
->willReturn($json)
;
$mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version));

$version = new PhpCompatUtil($mockFileManager);

Expand Down Expand Up @@ -110,28 +89,7 @@ public function testFallBackToPhpVersionWithoutLockFile(): void

public function testWithoutPlatformVersionSet(): void
{
$json = '{"platform-overrides": {}}';

$mockFileManager = $this->createMock(FileManager::class);
$mockFileManager
->expects(self::once())
->method('getRootDirectory')
->willReturn('/test')
;

$mockFileManager
->expects(self::once())
->method('fileExists')
->with('/test/composer.lock')
->willReturn(true)
;

$mockFileManager
->expects(self::once())
->method('getFileContents')
->with('/test/composer.lock')
->willReturn($json)
;
$mockFileManager = $this->mockFileManager('{"platform-overrides": {}}');

$util = new PhpCompatUtilTestFixture($mockFileManager);

Expand All @@ -145,8 +103,58 @@ public function testWithoutPlatformVersionSet(): void
*/
public function testCanUseTypedProperties(string $version, bool $expectedResult): void
{
$json = sprintf('{"platform-overrides": {"php": "%s"}}', $version);
$mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version));

$version = new PhpCompatUtil($mockFileManager);

$result = $version->canUseTypedProperties();

self::assertSame($expectedResult, $result);
}

public function phpVersionForTypedPropertiesDataProvider(): \Generator
{
yield ['8', true];
yield ['8.0.1', true];
yield ['8RC1', true];
yield ['7.4', true];
yield ['7.4.6', true];
yield ['7', false];
yield ['7.0', false];
yield ['5.7', false];
}

/**
* @dataProvider phpVersionForUnionTypesDataProvider
*/
public function testCanUseUnionTypes(string $version, bool $expectedResult): void
{
$mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version));

$version = new PhpCompatUtil($mockFileManager);

$result = $version->canUseUnionTypes();

self::assertSame($expectedResult, $result);
}

public function phpVersionForUnionTypesDataProvider(): \Generator
{
yield ['8', true];
yield ['8.0.1', true];
yield ['8RC1', true];
yield ['7.4', false];
yield ['7.4.6', false];
yield ['7', false];
yield ['7.0', false];
yield ['5.7', false];
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|FileManager
*/
private function mockFileManager(string $json)
{
$mockFileManager = $this->createMock(FileManager::class);
$mockFileManager
->expects(self::once())
Expand All @@ -168,23 +176,7 @@ public function testCanUseTypedProperties(string $version, bool $expectedResult)
->willReturn($json)
;

$version = new PhpCompatUtil($mockFileManager);

$result = $version->canUseTypedProperties();

self::assertSame($expectedResult, $result);
}

public function phpVersionForTypedPropertiesDataProvider(): \Generator
{
yield ['8', true];
yield ['8.0.1', true];
yield ['8RC1', true];
yield ['7.4', true];
yield ['7.4.6', true];
yield ['7', false];
yield ['7.0', false];
yield ['5.7', false];
return $mockFileManager;
}
}

Expand Down