Skip to content

Commit

Permalink
expose var use_typed_properties to all templates
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Sep 14, 2021
1 parent 203699d commit 8a295a3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 62 deletions.
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

0 comments on commit 8a295a3

Please sign in to comment.