-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: use boostrap composer dependency to get twbs version
- Loading branch information
Showing
5 changed files
with
139 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Documentation\Generator; | ||
|
||
class BootstrapVersionResolver | ||
{ | ||
/** | ||
* @var \Documentation\Generator\FileSystem\File | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $rootDirPath; | ||
|
||
public function __construct( | ||
\Documentation\Generator\FileSystem\File $file, | ||
string $rootDirPath, | ||
) { | ||
$this->file = $file; | ||
$this->rootDirPath = $rootDirPath; | ||
} | ||
|
||
public function getBootstrapVersion(): string | ||
{ | ||
$composerJsonPath = $this->rootDirPath . '/composer.json'; | ||
if (!$this->file->fileExists($composerJsonPath)) { | ||
throw new \LogicException( | ||
'composer.json file not found in root directory "' . $this->rootDirPath . '"' | ||
); | ||
} | ||
|
||
$composerJson = json_decode($this->file->readFile($composerJsonPath), true); | ||
|
||
foreach ($composerJson['require-dev'] as $packageName => $packageVersion) { | ||
if ('twbs/bootstrap' === $packageName) { | ||
return preg_replace('/^.*(\d+\.\d+).*$/', '$1', $packageVersion); | ||
} | ||
} | ||
|
||
throw new \LogicException('Bootstrap version not found in composer dev dependencies'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
tests/TestSuite/Documentation/Generator/BootstrapVersionResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace TestSuite\Documentation\Generator; | ||
|
||
class BootstrapVersionResolverTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \MockObject | ||
*/ | ||
protected $file; | ||
|
||
/** | ||
* @var \Documentation\Generator\BootstrapVersionResolver | ||
*/ | ||
protected $bootstrapVersionResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->file = $this->createMock(\Documentation\Generator\FileSystem\File::class); | ||
|
||
$this->bootstrapVersionResolver = new \Documentation\Generator\BootstrapVersionResolver( | ||
$this->file, | ||
'/tmp/test-dir' | ||
); | ||
} | ||
|
||
public function testGetBootstrapVersionShouldReturnPackageVersion() | ||
{ | ||
|
||
$this->file | ||
->expects($this->once()) | ||
->method('fileExists') | ||
->with('/tmp/test-dir/composer.json') | ||
->willReturn(true); | ||
|
||
$this->file | ||
->expects($this->once()) | ||
->method('readFile') | ||
->with('/tmp/test-dir/composer.json') | ||
->willReturn('{"require-dev": {"twbs/bootstrap": "^5.1"}}'); | ||
|
||
$this->assertEquals('5.1', $this->bootstrapVersionResolver->getBootstrapVersion()); | ||
} | ||
|
||
|
||
public function testGetBootstrapVersionShouldThrowExceptionWhenComposerJsonNotFound() | ||
{ | ||
$this->file | ||
->expects($this->once()) | ||
->method('fileExists') | ||
->with('/tmp/test-dir/composer.json') | ||
->willReturn(false); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('composer.json file not found in root directory "/tmp/test-dir"'); | ||
$this->bootstrapVersionResolver->getBootstrapVersion(); | ||
} | ||
|
||
public function testGetBootstrapVersionShouldThrowExceptionWhenPackageNotFound() | ||
{ | ||
$this->file | ||
->expects($this->once()) | ||
->method('fileExists') | ||
->with('/tmp/test-dir/composer.json') | ||
->willReturn(true); | ||
|
||
$this->file | ||
->expects($this->once()) | ||
->method('readFile') | ||
->with('/tmp/test-dir/composer.json') | ||
->willReturn('{"require-dev": {"another-package": "^1.0"}}'); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('Bootstrap version not found in composer dev dependencies'); | ||
$this->bootstrapVersionResolver->getBootstrapVersion(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters