first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# This file contains invalid YAML.
- test:
something: {
}
else: this

View File

@@ -0,0 +1,13 @@
deep-array:
second:
third:
fourth: hello world
flat-array:
- one
- two
- three
null-value: ~
inline-array: [ one, two, three ]
bool-value: true
int-value: 1
float-value: 1.0

View File

@@ -0,0 +1,39 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Grasmash\YamlCli\Tests\TestBase;
class ApplicationTest extends TestBase
{
/**
* Tests that all expected commands are available in the application.
*
* @dataProvider getValueProvider
*/
public function testApplication($expected)
{
$bin = realpath(__DIR__ . '/../../../bin/yaml-cli');
$output = shell_exec("$bin list");
$this->assertStringContainsString($expected, $output);
}
/**
* Provides values to testApplication().
*
* @return array
* An array of values to test.
*/
public function getValueProvider(): array
{
return [
['get:value'],
['get:type'],
['lint'],
['unset:key'],
['update:key'],
['update:value'],
];
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Grasmash\YamlCli\Command\GetTypeCommand;
use Grasmash\YamlCli\Tests\TestBase;
use Symfony\Component\Console\Tester\CommandTester;
class GetTypeCommandTest extends TestBase
{
/**
* Tests the 'get:value' command.
*
* @dataProvider getValueProvider
*/
public function testGetValue($file, $key, $expected_output, $expected_exit_code)
{
$this->application->add(new GetTypeCommand());
$command = $this->application->find('get:type');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'filename' => $file,
'key' => $key,
]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString($expected_output, $output);
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
}
/**
* Provides values to testGetValue().
*
* @return array
* An array of values to test.
*/
public function getValueProvider()
{
$file = 'tests/resources/good.yml';
return [
[$file, 'not-real', "The key not-real does not exist.", 1],
[
'missing.yml',
'not-real',
"The file missing.yml does not exist.",
1,
],
[$file, 'deep-array.second.third.fourth', 'string', 0],
[$file, 'flat-array', 'array', 0],
[$file, 'inline-array', 'array', 0],
[$file, 'null-value', 'NULL', 0],
[$file, 'bool-value', 'boolean', 0],
[$file, 'int-value', 'integer', 0],
[$file, 'float-value', 'double', 0],
];
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Grasmash\YamlCli\Command\GetValueCommand;
use Grasmash\YamlCli\Tests\TestBase;
use Symfony\Component\Console\Tester\CommandTester;
class GetValueCommandTest extends TestBase
{
/**
* Tests the 'get:value' command.
*
* @dataProvider getValueProvider
*/
public function testGetValue($file, $key, $expected_output, $expected_exit_code)
{
$this->application->add(new GetValueCommand());
$command = $this->application->find('get:value');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'filename' => $file,
'key' => $key,
]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString($expected_output, $output);
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
}
/**
* Provides values to testGetValue().
*
* @return array
* An array of values to test.
*/
public function getValueProvider()
{
$file = 'tests/resources/good.yml';
return [
[$file, 'not-real', "The key not-real does not exist.", 1],
[
'missing.yml',
'not-real',
"The file missing.yml does not exist.",
1,
],
[$file, 'deep-array.second.third.fourth', 'hello world', 0],
[
$file,
'flat-array',
'- one
- two
- three',
0,
],
[
$file,
'inline-array',
'- one
- two
- three',
0,
],
];
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Grasmash\YamlCli\Command\LintCommand;
use Grasmash\YamlCli\Tests\TestBase;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Tester\CommandTester;
class LintCommandTest extends TestBase
{
/**
* Tests the 'lint' command.
*
* @dataProvider getValueProvider
*/
public function testLint($file, $expected_output, $expected_exit_code)
{
$this->application->add(new LintCommand());
$command = $this->application->find('lint');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'filename' => $file,
], ['verbosity' => Output::VERBOSITY_VERBOSE]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString($expected_output, $output);
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
}
/**
* Provides values to testLint().
*
* @return array
* An array of values to test.
*/
public function getValueProvider()
{
return [
[
'tests/resources/good.yml',
"The file tests/resources/good.yml contains valid YAML.",
0,
],
[
'tests/resources/bad.yml',
"There was an error parsing tests/resources/bad.yml. The contents are not valid YAML.",
1,
],
['missing.yml', "The file missing.yml does not exist.", 1],
];
}
}

View File

@@ -0,0 +1,130 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Dflydev\DotAccessData\Data;
use Grasmash\YamlCli\Command\UnsetKeyCommand;
use Grasmash\YamlCli\Tests\TestBase;
use Symfony\Component\Console\Tester\CommandTester;
class UnsetKeyCommandTest extends TestBase
{
/** @var string */
protected $temp_file;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->setupTemporaryConfigFiles();
}
/**
* Tests the 'unset:key' command.
*
* @dataProvider getValueProvider
*/
public function testUnsetKey($filename, $key, $expected_output, $expected_exit_code)
{
$commandTester = $this->runCommand($filename, $key);
$output = $commandTester->getDisplay();
$this->assertStringContainsString($expected_output, $output);
$contents = $this->getCommand()->loadYamlFile($filename);
$data = new Data($contents);
$this->assertNotTrue($data->has($key), "The file $filename contains the old key $key. It should not.");
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
}
/**
* Tests that passing a missing file outputs expected error.
*/
public function testMissingFile()
{
$commandTester = $this->runCommand('missing.yml', 'not-real');
$this->assertStringContainsString("The file missing.yml does not exist.", $commandTester->getDisplay());
}
/**
* Gets the unset:key command.
*
* @return UnsetKeyCommand
*/
protected function getCommand()
{
$this->application->add(new UnsetKeyCommand());
$command = $this->application->find('unset:key');
return $command;
}
/**
* Runs the unset:key command.
*
* @param string $filename
* The filename.
* @param string $key
* The key to unset.
*
* @return \Symfony\Component\Console\Tester\CommandTester
*/
protected function runCommand($filename, $key)
{
$command = $this->getCommand();
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'filename' => $filename,
'key' => $key,
]);
return $commandTester;
}
/**
* Provides values to testUnsetKey().
*
* @return array
* An array of values to test.
*/
public function getValueProvider()
{
$filename = 'tests/resources/temp.yml';
return [
[
$filename,
'deep-array.second.third.fourth',
"The key 'deep-array.second.third.fourth' was removed from $filename.",
0,
],
[
$filename,
'flat-array.0',
"The key 'flat-array.0' was removed from $filename.",
0,
],
[
$filename,
'inline-array.0',
"The key 'inline-array.0' was removed from $filename.",
0,
],
[
$filename,
'null-value',
"The key 'null-value' was removed from $filename.",
0,
],
[
$filename,
'fake-value',
"The key 'fake-value' does not exist in $filename.",
1,
],
];
}
}

View File

@@ -0,0 +1,144 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Dflydev\DotAccessData\Data;
use Grasmash\YamlCli\Command\UpdateKeyCommand;
use Grasmash\YamlCli\Tests\TestBase;
use Symfony\Component\Console\Tester\CommandTester;
class UpdateKeyCommandTest extends TestBase
{
/** @var string */
protected $temp_file;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->setupTemporaryConfigFiles();
}
/**
* Tests the 'update:key' command.
*
* @dataProvider getValueProvider
*/
public function testUpdateKey($file, $key, $new_key, $expected_output, $expected_exit_code)
{
$contents = $this->getCommand()->loadYamlFile($file);
$data = new Data($contents);
$value = $data->get($key, null);
$commandTester = $this->runCommand($file, $key, $new_key);
$output = $commandTester->getDisplay();
$this->assertStringContainsString($expected_output, $output);
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
// If we expected the command to be successful, test the file contents.
if (!$expected_exit_code) {
$contents = $this->getCommand()->loadYamlFile($file);
$data = new Data($contents);
$this->assertTrue($data->has($new_key), "The file $file does not contain the new key $new_key. It should.");
$this->assertNotTrue($data->has($key), "The file $file contains the old key $key. It should not.");
$this->assertEquals(
$value,
$data->get($new_key),
"The value of key $new_key does not equal the value of the original key $key"
);
}
}
/**
* Tests that passing a missing file outputs expected error.
*/
public function testMissingFile()
{
$commandTester = $this->runCommand('missing.yml', 'not-real', 'still-not-real');
$this->assertStringContainsString("The file missing.yml does not exist.", $commandTester->getDisplay());
}
/**
* Gets the update:key command.
*
* @return UpdateKeyCommand
*/
protected function getCommand()
{
$this->application->add(new UpdateKeyCommand());
$command = $this->application->find('update:key');
return $command;
}
/**
* Runs the update:key command.
*
* @param string $file
* The filename.
* @param string $key
* The original key.
* @param string $new_key
* The new key.
*
* @return \Symfony\Component\Console\Tester\CommandTester
*/
protected function runCommand($file, $key, $new_key)
{
$command = $this->getCommand();
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'filename' => $file,
'key' => $key,
'new-key' => $new_key,
]);
return $commandTester;
}
/**
* Provides values to testUpdateKey().
*
* @return array
* An array of values to test.
*/
public function getValueProvider()
{
$file = 'tests/resources/temp.yml';
return [
[
$file,
'deep-array.second.third.fourth',
'deep-array.second.third.fifth',
"The key 'deep-array.second.third.fourth' was changed to 'deep-array.second.third.fifth' in $file.",
0,
],
[
$file,
'flat-array.0',
'flat-array.10',
"The key 'flat-array.0' was changed to 'flat-array.10' in $file.",
0,
],
[
$file,
'inline-array.0',
'inline-array.10',
"The key 'inline-array.0' was changed to 'inline-array.10' in $file.",
0,
],
[
$file,
'fake-key',
'new-key',
"The key 'fake-key' does not exist in $file.",
1,
],
];
}
}

View File

@@ -0,0 +1,269 @@
<?php
namespace Grasmash\YamlCli\Tests\Command;
use Dflydev\DotAccessData\Data;
use Grasmash\YamlCli\Command\UpdateValueCommand;
use Grasmash\YamlCli\Tests\TestBase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
class UpdateValueCommandTest extends TestBase
{
/** @var string */
protected $temp_file;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->setupTemporaryConfigFiles();
}
/**
* Tests the 'update:value' command.
*
* @dataProvider getValueProvider
*/
public function testUpdateValue($file, $key, $value, $type, $expected_value, $expected_output, $expected_exit_code)
{
$commandTester = $this->runCommand($file, $key, $value, $type);
$output = $commandTester->getDisplay();
$this->assertStringContainsString($expected_output, $output);
$contents = $this->getCommand()->loadYamlFile($file);
$data = new Data($contents);
$this->assertEquals($expected_value, $data->get($key));
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
}
/**
* Tests that passing a missing file outputs expected error.
*/
public function testMissingFile()
{
$commandTester = $this->runCommand('missing.yml', 'not-real', 'still-not-real', null);
$this->assertStringContainsString("The file missing.yml does not exist.", $commandTester->getDisplay());
}
/**
* Gets the update:value command.
*
* @return \Symfony\Component\Console\Command\Command
*/
protected function getCommand(): Command
{
$this->application->add(new UpdateValueCommand());
return $this->application->find('update:value');
}
/**
* Runs the update:value commnd.
*
* @param string $file
* The filename.
* @param string $key
* The key for which to update the value.
* @param string $value
* The new value.
* @param string $type
*
* @return \Symfony\Component\Console\Tester\CommandTester
*/
protected function runCommand(string $file, string $key, string $value, $type): CommandTester
{
$command = $this->getCommand();
$commandTester = new CommandTester($command);
$params = [
'command' => $command->getName(),
'filename' => $file,
'key' => $key,
'value' => $value,
];
if ($type) {
$params['--type'] = $type;
}
$commandTester->execute($params);
return $commandTester;
}
/**
* Provides values to testUpdateValue().
*
* @return array
* An array of values to test.
*/
public function getValueProvider(): array
{
$file = 'tests/resources/temp.yml';
return [
[
$file,
'deep-array.second.third.fourth',
'goodbye world',
null,
'goodbye world',
"The value for key 'deep-array.second.third.fourth' was set to 'goodbye world' (string) in $file.",
0,
],
[
$file,
'flat-array.0',
'goodbye world',
null,
'goodbye world',
"The value for key 'flat-array.0' was set to 'goodbye world' (string) in $file.",
0,
],
[
$file,
'inline-array.0',
'goodbye world',
null,
'goodbye world',
"The value for key 'inline-array.0' was set to 'goodbye world' (string) in $file.",
0,
],
[
$file,
'new-key.sub-key',
'hello world',
null,
'hello world',
"The value for key 'new-key.sub-key' was set to 'hello world' (string) in $file.",
0,
],
[
$file,
'integer.0',
'0',
'int',
0,
"The value for key 'integer.0' was set to '0' (integer) in $file.",
0,
],
[
$file,
'integer.1',
'1',
'integer',
1,
"The value for key 'integer.1' was set to '1' (integer) in $file.",
0,
],
[
$file,
'boolean.0',
'false',
null,
false,
"The value for key 'boolean.0' was set to 'false' (boolean) in $file.",
0,
],
[
$file,
'boolean.1',
'true',
null,
true,
"The value for key 'boolean.1' was set to 'true' (boolean) in $file.",
0,
],
[
$file,
'boolean.0',
'0',
'bool',
false,
"The value for key 'boolean.0' was set to '0' (boolean) in $file.",
0,
],
[
$file,
'boolean.1',
'1',
'boolean',
true,
"The value for key 'boolean.1' was set to '1' (boolean) in $file.",
0,
],
[
$file,
'string.0',
'false',
'string',
'false',
"The value for key 'string.0' was set to 'false' (string) in $file.",
0,
],
[
$file,
'string.1',
'true',
'string',
'true',
"The value for key 'string.1' was set to 'true' (string) in $file.",
0,
],
[
$file,
'string.2',
'null',
'string',
'null',
"The value for key 'string.2' was set to 'null' (string) in $file.",
0,
],
[
$file,
'null.0',
'null',
null,
null,
"The value for key 'null.0' was set to 'null' (NULL) in $file.",
0,
],
[
$file,
'null.0',
'~',
'null',
null,
"The value for key 'null.0' was set to '~' (NULL) in $file.",
0,
],
[
$file,
'float.0',
'1.0',
'float',
1.0,
"The value for key 'float.0' was set to '1.0' (double) in $file.",
0,
],
[
$file,
'float.1',
'1.0',
'double',
1.0,
"The value for key 'float.1' was set to '1.0' (double) in $file.",
0,
],
[
$file,
'float.2',
'1.0',
'real',
1.0,
"The value for key 'float.2' was set to '1.0' (double) in $file.",
0,
],
];
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Grasmash\YamlCli\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
/**
* Class BltTestBase.
*
* Base class for all tests that are executed for BLT itself.
*/
abstract class TestBase extends TestCase
{
/** @var Application */
protected $application;
/** @var string */
protected $temp_file = '';
/**
* {@inheritdoc}
*
* @see https://symfony.com/doc/current/console.html#testing-commands
*/
protected function setUp(): void
{
parent::setUp();
$this->application = new Application();
}
/**
* Removes temporary file.
*/
protected function tearDown(): void
{
parent::tearDown();
// This will only exist if a test called setupTemporaryConfigFiles().
if ($this->temp_file && file_exists($this->temp_file)) {
unlink($this->temp_file);
}
}
/**
* Creates a temporary copy of a file and assigns it to $this->temp_file.
*
* @param string $source
* The filename of the source file.
* @param string $destination
* The filename of the destination file.
*
* @return bool
* TRUE if the file was created. Otherwise, FALSE.
*/
protected function createTemporaryFile($source, $destination)
{
$source_path = realpath($source);
if (file_exists($source_path)) {
copy($source_path, $destination);
$destination_path = realpath($destination);
$this->temp_file = $destination_path;
return true;
}
return false;
}
/**
* Creates a temporary copy of config files so that they can be modified.
*/
protected function setupTemporaryConfigFiles()
{
// Make a temporary copy of good.yml so that we can update a value
// without destroying the original.
$this->createTemporaryFile(__DIR__ . '/../resources/good.yml', __DIR__ . '/../resources/temp.yml');
}
}