Skip to content

Commit

Permalink
Add Config
Browse files Browse the repository at this point in the history
  • Loading branch information
msvrtan committed Jul 28, 2017
1 parent a141ce1 commit 8adbd2d
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 0 deletions.
8 changes: 8 additions & 0 deletions nemesis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
paths:
code:
"src/": ""
tests:
"tests/": tests\
phpunit:
base_namespace: tests
base_test_class_name: PHPUnit_Framework_TestCase
34 changes: 34 additions & 0 deletions spec/NullDev/Nemesis/Config/ConfigFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace spec\NullDev\Nemesis\Config;

use NullDev\Nemesis\Config\Config;
use NullDev\Nemesis\Config\ConfigFactory;
use PhpSpec\ObjectBehavior;

class ConfigFactorySpec extends ObjectBehavior
{
public function let()
{
$input = [
'paths' => [
'code' => ['src/' => ''],
'tests' => ['tests/' => 'tests\\'],
],
];
$this->beConstructedWith($input);
}

public function it_is_initializable()
{
$this->shouldHaveType(ConfigFactory::class);
}

public function it_will_create_config_file()
{
$this->create()
->shouldReturnAnInstanceOf(Config::class);
}
}
35 changes: 35 additions & 0 deletions spec/NullDev/Nemesis/Config/ConfigSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace spec\NullDev\Nemesis\Config;

use NullDev\Nemesis\Config\Config;
use NullDev\Skeleton\Path\Psr4Path;
use NullDev\Skeleton\Path\TestPsr4Path;
use PhpSpec\ObjectBehavior;

class ConfigSpec extends ObjectBehavior
{
public function let(Psr4Path $psr4Path, TestPsr4Path $testPsr4Path)
{
$this->beConstructedWith([$psr4Path], [$testPsr4Path], 'tests', 'PHPUnit_Framework_TestCase');
}

public function it_is_initializable()
{
$this->shouldHaveType(Config::class);
}

public function it_exposes_source_code_paths(Psr4Path $psr4Path)
{
$this->getSourceCodePaths()
->shouldReturn([$psr4Path]);
}

public function it_exposes_test_paths(TestPsr4Path $testPsr4Path)
{
$this->getTestPaths()
->shouldReturn([$testPsr4Path]);
}
}
68 changes: 68 additions & 0 deletions src/NullDev/Nemesis/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace NullDev\Nemesis\Config;

use NullDev\Skeleton\Path\Psr4Path;
use NullDev\Skeleton\Path\TestPsr4Path;
use Webmozart\Assert\Assert;

/**
* @see ConfigSpec
* @see ConfigTest
*/
class Config
{
/** @var array */
private $sourceCodePaths = [];

/** @var array */
private $testPaths = [];

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

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

public function __construct(
array $sourceCodePaths,
array $testPaths,
string $testsNamespace,
string $baseTestClassName
) {
Assert::allIsInstanceOf($sourceCodePaths, Psr4Path::class);
Assert::allIsInstanceOf($testPaths, TestPsr4Path::class);

$this->sourceCodePaths = $sourceCodePaths;
$this->testPaths = $testPaths;
$this->testsNamespace = $testsNamespace;
$this->baseTestClassName = $baseTestClassName;
}

public function getSourceCodePaths(): array
{
return $this->sourceCodePaths;
}

public function getTestPaths(): array
{
return $this->testPaths;
}

public function getPaths(): array
{
return array_merge($this->testPaths, $this->sourceCodePaths);
}

public function getTestsNamespace(): string
{
return $this->testsNamespace;
}

public function getBaseTestClassName(): string
{
return $this->baseTestClassName;
}
}
98 changes: 98 additions & 0 deletions src/NullDev/Nemesis/Config/ConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace NullDev\Nemesis\Config;

use NullDev\Skeleton\Path\Psr4Path;
use NullDev\Skeleton\Path\TestPsr4Path;
use Symfony\Component\Yaml\Yaml;
use Webmozart\Assert\Assert;

/**
* @see ConfigFactorySpec
* @see ConfigFactoryTest
*/
class ConfigFactory
{
/** @var array */
private $sourceCodePaths = [];
/** @var array */
private $testPaths = [];
/** @var string */
private $phpunitBaseNamespace;
/** @var string */
private $phpunitBaseTestClassName;

public function __construct()
{
$path = getcwd().'/nemesis.yml';
$defaultConfig = $this->getDefaultConfig();

if (false === is_file($path)) {
$config = $defaultConfig;
} else {
$config = array_merge($defaultConfig, Yaml::parse(file_get_contents($path)));
}

Assert::isArray($config['paths']['code']);
Assert::isArray($config['paths']['tests']);

foreach ($config['paths']['code'] as $path => $namespacePrefix) {
$this->sourceCodePaths[] = new Psr4Path($path, $namespacePrefix);
}
foreach ($config['paths']['tests'] as $path => $namespacePrefix) {
$this->testPaths[] = new TestPsr4Path($path, $namespacePrefix);
}
$this->phpunitBaseNamespace = $config['phpunit']['base_namespace'];
$this->phpunitBaseTestClassName = $config['phpunit']['base_test_class_name'];
}

public function create(): Config
{
return new Config(
$this->getSourceCodePaths(),
$this->getTestPaths(),
$this->getPhpunitBaseNamespace(),
$this->getPhpunitBaseTestClassName()
);
}

private function getDefaultConfig()
{
return [
'paths' => [
'code' => [
'src/' => '',
],
'tests' => [
'tests/' => 'tests\\',
],
],
'phpunit' => [
'base_namespace' => 'tests',
'base_test_class_name' => 'PHPUnit_Framework_TestCase',
],
];
}

private function getSourceCodePaths(): array
{
return $this->sourceCodePaths;
}

private function getTestPaths(): array
{
return $this->testPaths;
}

private function getPhpunitBaseNamespace(): string
{
return $this->phpunitBaseNamespace;
}

private function getPhpunitBaseTestClassName(): string
{
return $this->phpunitBaseTestClassName;
}
}
7 changes: 7 additions & 0 deletions src/NullDev/Nemesis/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ services:
autowire: true
autoconfigure: true
public: true

NullDev\Nemesis\Config\ConfigFactory: ~

NullDev\Nemesis\Config\Config:
class: NullDev\Nemesis\Config\Config
factory: ['@NullDev\Nemesis\Config\ConfigFactory', create]

29 changes: 29 additions & 0 deletions tests/NullDev/Nemesis/Config/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace tests\NullDev\Nemesis\Config;

use NullDev\Nemesis\Config\Config;
use NullDev\Nemesis\Config\ConfigFactory;
use tests\NullDev\ContainerSupportedTestCase;

/**
* @covers \NullDev\Nemesis\Config\ConfigFactory
* @group integration
*/
class ConfigFactoryTest extends ContainerSupportedTestCase
{
/** @var ConfigFactory */
private $configFactory;

public function setUp(): void
{
$this->configFactory = $this->getService(ConfigFactory::class);
}

public function testItReturnsConfigOut(): void
{
self::assertInstanceOf(Config::class, $this->configFactory->create());
}
}
64 changes: 64 additions & 0 deletions tests/NullDev/Nemesis/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace tests\NullDev\Nemesis\Config;

use NullDev\Nemesis\Config\Config;
use NullDev\Skeleton\Path\Psr4Path;
use NullDev\Skeleton\Path\TestPsr4Path;
use PHPUnit_Framework_TestCase;
use tests\NullDev\ContainerSupportedTestCase;

/**
* @covers \NullDev\Nemesis\Config\Config
* @group integration
*/
class ConfigTest extends ContainerSupportedTestCase
{
/** @var Config */
private $config;

public function setUp(): void
{
$this->config = $this->getService(Config::class);
}

public function testGetSourceCodePaths(): void
{
$expected = [
new Psr4Path('src/', ''),
];

self::assertEquals($expected, $this->config->getSourceCodePaths());
}

public function testGetTestPaths(): void
{
$expected = [
new TestPsr4Path('tests/', 'tests\\'),
];

self::assertEquals($expected, $this->config->getTestPaths());
}

public function testGetPaths(): void
{
$expected = [
new TestPsr4Path('tests/', 'tests\\'),
new Psr4Path('src/', ''),
];

self::assertEquals($expected, $this->config->getPaths());
}

public function testGetTestsNamespace(): void
{
self::assertEquals('tests', $this->config->getTestsNamespace());
}

public function testGetBaseTestClassName(): void
{
self::assertEquals('PHPUnit_Framework_TestCase', $this->config->getBaseTestClassName());
}
}

0 comments on commit 8adbd2d

Please sign in to comment.