Skip to content

Commit

Permalink
docs: use boostrap composer dependency to get twbs version
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Sep 28, 2023
1 parent 6c45733 commit d0b3439
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 86 deletions.
6 changes: 5 additions & 1 deletion scripts/generateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
$testsDirPath = $rootDirPath . '/tests/TestSuite/Documentation/Tests';
$maxNestedDir = 2;

$file = new \Documentation\Generator\FileSystem\Local\File();

$bootstrapVersionResolver = new \Documentation\Generator\BootstrapVersionResolver($file, $rootDirPath);

$configuration = new \Documentation\Generator\Configuration(
$rootDirPath,
$testsDirPath,
$bootstrapVersionResolver->getBootstrapVersion(),
$maxNestedDir
);

$file = new \Documentation\Generator\FileSystem\Local\File();

$homePageGenerator = new \Documentation\Generator\HomePageGenerator($configuration, $file);
$homePageGenerator->generate();
Expand Down
44 changes: 44 additions & 0 deletions src/Documentation/Generator/BootstrapVersionResolver.php
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');
}
}
24 changes: 8 additions & 16 deletions src/Documentation/Generator/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Configuration
*/
private $testsDirPath;

/**
* @var string
*/
private $bootstrapVersion;

/**
* @var int
*/
Expand All @@ -22,10 +27,12 @@ class Configuration
public function __construct(
$rootDirPath,
$testsDirPath,
$bootstrapVersion,
$maxNestedDir
) {
$this->rootDirPath = $rootDirPath;
$this->testsDirPath = $testsDirPath;
$this->bootstrapVersion = $bootstrapVersion;
$this->maxNestedDir = $maxNestedDir;
}

Expand All @@ -41,22 +48,7 @@ public function getTestsDirPath()

public function getBootstrapVersion()
{
$composerJsonPath = $this->getRootDirPath() . '/composer.json';
if (!file_exists($composerJsonPath)) {
throw new \LogicException(
'composer.json file not found in root directory "' . $this->getRootDirPath() . '"'
);
}

$composerJson = json_decode(file_get_contents($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');
return $this->bootstrapVersion;
}

public function getMaxNestedDir()
Expand Down
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();
}
}
74 changes: 5 additions & 69 deletions tests/TestSuite/Documentation/Generator/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,21 @@

class ConfigurationTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Documentation\Generator\Configuration
*/
protected $configuration;

public function testGetRootDirPath()
{
$config = new \Documentation\Generator\Configuration('/path/to/root', '/path/to/tests', 3);
$config = new \Documentation\Generator\Configuration('/path/to/root', '/path/to/tests', 'x.x', 3);
$this->assertEquals('/path/to/root', $config->getRootDirPath());
}

public function testGetTestsDirPath()
{
$config = new \Documentation\Generator\Configuration('/path/to/root', '/path/to/tests', 3);
$config = new \Documentation\Generator\Configuration('/path/to/root', '/path/to/tests', 'x.x', 3);
$this->assertEquals('/path/to/tests', $config->getTestsDirPath());
}

public function testGetBootstrapVersionShouldReturnPackageVersion()
{
$root = \org\bovigo\vfs\vfsStream::setup(
'exemple-dir',
null,
[
'composer.json' => '{"require-dev": {"twbs/bootstrap": "^5.1"}}',
'tests' => []
],
);

$config = new \Documentation\Generator\Configuration(
$root->url(),
$root->url() . '/tests',
3
);

$this->assertEquals('5.1', $config->getBootstrapVersion());
}


public function testGetBootstrapVersionShouldThrowExceptionWhenComposerJsonNotFound()
{
$root = \org\bovigo\vfs\vfsStream::setup(
'exemple-dir',
null,
[
'tests' => []
],
);

$config = new \Documentation\Generator\Configuration(
$root->url(),
$root->url() . '/tests',
3
);

// Test case where composer.json file is not found
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('composer.json file not found in root directory "vfs://exemple-dir"');
$config->getBootstrapVersion();
}

public function testGetBootstrapVersionShouldThrowExceptionWhenPackageNotFound()
public function testGetBootstrapVersion()
{
$root = \org\bovigo\vfs\vfsStream::setup(
'exemple-dir',
null,
[
'composer.json' => '{"require-dev": {"other/package": "1.0.0"}}',
'tests' => []
],
);

$config = new \Documentation\Generator\Configuration(
$root->url(),
$root->url() . '/tests',
3
);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Bootstrap version not found in composer dev dependencies');
$config->getBootstrapVersion();
$config = new \Documentation\Generator\Configuration('/path/to/root', '/path/to/tests', 'x.x', 3);
$this->assertEquals('x.x', $config->getBootstrapVersion());
}
}

0 comments on commit d0b3439

Please sign in to comment.