Merge pull request #414 from bolt/tests-for-config-parsers

Tests for config parsers
This commit is contained in:
Bob den Otter
2019-05-08 08:11:19 +02:00
committed by GitHub
17 changed files with 561 additions and 62 deletions
+1 -1
View File
@@ -124,7 +124,7 @@ class Config
$timestamps = [];
foreach ($configs as $config) {
foreach ($config->getFilenames() as $file) {
foreach ($config->getParsedFilenames() as $file) {
$timestamps[$file] = filemtime($file);
}
}
+36 -11
View File
@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Bolt\Configuration\Parser;
use Bolt\Configuration\PathResolver;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\Yaml\Yaml;
use Tightenco\Collect\Support\Collection;
@@ -18,30 +18,45 @@ abstract class BaseParser
/** @var PathResolver */
protected $pathResolver;
/** @var string[] */
protected $filenames = [];
/** @var string */
protected $initialFilename;
public function __construct()
/** @var string[] */
protected $parsedFilenames = [];
public function __construct(string $initialFilename)
{
$configDirectories = [dirname(dirname(dirname(__DIR__))) . '/config/bolt'];
$this->fileLocator = new FileLocator($configDirectories);
$this->pathResolver = new PathResolver(dirname(dirname(dirname(__DIR__))), []);
$this->initialFilename = $initialFilename;
}
/**
* Read and parse a YAML configuration file.
*
* If filename doesn't exist and/or isn't readable, we attempt to locate it
* in our config folder. This way you can pass in either an absolute
* filename or simply 'menu.yaml'.
*/
protected function parseConfigYaml(string $filename): Collection
protected function parseConfigYaml(string $filename, bool $ignoreMissing = false): Collection
{
try {
$filename = $this->fileLocator->locate($filename, null, true);
} catch (FileNotFoundException $e) {
return new Collection();
if (! is_readable($filename)) {
$filename = $this->fileLocator->locate($filename, null, true);
}
} catch (FileLocatorFileNotFoundException $e) {
if ($ignoreMissing) {
return new Collection([]);
}
// If not $ignoreMissing, we throw the exception regardless.
throw $e;
}
$yaml = Yaml::parseFile($filename);
$this->filenames[] = $filename;
$this->parsedFilenames[] = $filename;
// Unset the repeated nodes key after parse
unset($yaml['__nodes']);
@@ -49,9 +64,19 @@ abstract class BaseParser
return new Collection($yaml);
}
public function getFilenames(): array
public function getParsedFilenames(): array
{
return $this->filenames;
return $this->parsedFilenames;
}
public function getInitialFilename()
{
return $this->initialFilename;
}
public function getFilenameLocalOverrides()
{
return preg_replace('/([a-z0-9_-]+).(ya?ml)$/i', '$1_local.$2', $this->initialFilename);
}
abstract public function parse(): Collection;
@@ -18,10 +18,10 @@ class ContentTypesParser extends BaseParser
*/
private $generalConfig;
public function __construct(Collection $generalConfig)
public function __construct(Collection $generalConfig, string $filename = 'contenttypes.yaml')
{
$this->generalConfig = $generalConfig;
parent::__construct();
parent::__construct($filename);
}
/**
@@ -32,10 +32,12 @@ class ContentTypesParser extends BaseParser
public function parse(): Collection
{
$contentTypes = [];
$tempContentTypes = $this->parseConfigYaml('contenttypes.yaml');
$tempContentTypes = $this->parseConfigYaml($this->getInitialFilename());
foreach ($tempContentTypes as $key => $contentType) {
$contentType = $this->parseContentType($key, $contentType);
$contentTypes[$key] = $contentType;
if (is_array($contentType)) {
$contentType = $this->parseContentType($key, $contentType);
$contentTypes[$key] = $contentType;
}
}
return new Collection($contentTypes);
@@ -110,13 +112,6 @@ class ContentTypesParser extends BaseParser
$contentType['icon_many'] = str_replace('fa:', 'fa-', $contentType['icon_many']);
}
// Allow explicit setting of a Content Type's table name suffix. We default to slug if not present.
if (isset($contentType['tablename'])) {
$contentType['tablename'] = Str::slug($contentType['tablename'], '_');
} else {
$contentType['tablename'] = Str::slug($contentType['slug'], '_');
}
if (! isset($contentType['allow_numeric_slugs'])) {
$contentType['allow_numeric_slugs'] = false;
}
+8 -25
View File
@@ -12,14 +12,19 @@ use Webmozart\PathUtil\Path;
class GeneralParser extends BaseParser
{
public function __construct(string $initialFilename = 'config.yaml')
{
parent::__construct($initialFilename);
}
/**
* Read and parse the config.yaml and config_local.yaml configuration files.
*/
public function parse(): Collection
{
$defaultconfig = $this->getDefaultConfig();
$tempconfig = $this->parseConfigYaml('config.yaml');
$tempconfiglocal = $this->parseConfigYaml('config_local.yaml');
$tempconfig = $this->parseConfigYaml($this->getInitialFilename());
$tempconfiglocal = $this->parseConfigYaml($this->getFilenameLocalOverrides(), true);
$general = Arr::replaceRecursive($defaultconfig, Arr::replaceRecursive($tempconfig, $tempconfiglocal));
// Make sure Bolt's mount point is OK:
@@ -61,27 +66,12 @@ class GeneralParser extends BaseParser
'locale' => null,
'records_per_page' => 10,
'records_on_dashboard' => 5,
'systemlog' => [
'enabled' => true,
],
'changelog' => [
'enabled' => false,
],
'debuglog' => [
'enabled' => false,
'level' => 'DEBUG',
'filename' => 'bolt-debug.log',
],
'debug' => null,
'debug_show_loggedoff' => false,
'debug_error_level' => null,
'production_error_level' => null,
'debug_enable_whoops' => false, /* @deprecated. Deprecated since 3.2, to be removed in 4.0 */
'debug_error_use_symfony' => false,
'debug_permission_audit_mode' => false,
'debug_trace_argument_limit' => 4,
'strict_variables' => null,
'theme' => 'base-2016',
'theme' => 'base-2019',
'listing_template' => 'listing.html.twig',
'listing_records' => '5',
'listing_sort' => 'datepublish DESC',
@@ -115,13 +105,7 @@ class GeneralParser extends BaseParser
'filebrowserWindowHeight' => 480,
],
],
'liveeditor' => true,
'canonical' => null,
'developer_notices' => false,
'cookies_use_remoteaddr' => true,
'cookies_use_browseragent' => false,
'cookies_use_httphost' => true,
'cookies_lifetime' => 14 * 24 * 3600,
'enforce_ssl' => false,
'thumbnails' => [
'default_thumbnail' => [160, 120],
@@ -133,7 +117,6 @@ class GeneralParser extends BaseParser
'only_aliases' => false,
],
'accept_file_types' => explode(',', 'twig,html,js,css,scss,gif,jpg,jpeg,png,ico,zip,tgz,txt,md,doc,docx,pdf,epub,xls,xlsx,csv,ppt,pptx,mp3,ogg,wav,m4a,mp4,m4v,ogv,wmv,avi,webm,svg'),
'hash_strength' => 10,
'branding' => [
'name' => 'Bolt',
'path' => '/bolt',
+4 -5
View File
@@ -11,7 +11,7 @@ class MenuParser extends BaseParser
/** @var array */
private $itemBase = [];
public function __construct()
public function __construct(string $initialFilename = 'menu.yaml')
{
$this->itemBase = [
'label' => '',
@@ -23,7 +23,7 @@ class MenuParser extends BaseParser
'current' => false,
];
parent::__construct();
parent::__construct($initialFilename);
}
/**
@@ -31,8 +31,7 @@ class MenuParser extends BaseParser
*/
public function parse(): Collection
{
// @todo Allow setting of file/path. See Github issue https://github.com/bolt/four/issues/379
$menuYaml = $this->parseConfigYaml('menu.yaml');
$menuYaml = $this->parseConfigYaml($this->getInitialFilename());
$menu = [];
@@ -50,7 +49,7 @@ class MenuParser extends BaseParser
$menu = [];
foreach ($items as $item) {
$item = array_merge($this->itemBase, $item);
$item = array_merge($this->itemBase, (array) $item);
if (isset($item['submenu']) && is_array($item['submenu'])) {
$item['submenu'] = $this->parseItems($item['submenu']);
+30 -5
View File
@@ -9,17 +9,27 @@ use Tightenco\Collect\Support\Collection;
class TaxonomyParser extends BaseParser
{
public function __construct(string $initialFilename = 'taxonomy.yaml')
{
parent::__construct($initialFilename);
}
/**
* Read and parse the taxonomy.yml configuration file.
*/
public function parse(): Collection
{
$taxonomies = $this->parseConfigYaml('taxonomy.yaml');
$taxonomies = $this->parseConfigYaml($this->getInitialFilename());
foreach ($taxonomies as $key => $taxonomy) {
if (isset($taxonomy['name']) === false) {
$taxonomy['name'] = ucwords(str_replace('-', ' ', Str::humanize($taxonomy['slug'])));
}
if (isset($taxonomy['slug']) === false) {
$taxonomy['slug'] = Str::slug($taxonomy['name']);
}
if (isset($taxonomy['singular_name']) === false) {
if (isset($taxonomy['singular_slug'])) {
$taxonomy['singular_name'] = ucwords(str_replace('-', ' ', Str::humanize($taxonomy['singular_slug'])));
@@ -27,21 +37,35 @@ class TaxonomyParser extends BaseParser
$taxonomy['singular_name'] = ucwords(str_replace('-', ' ', Str::humanize($taxonomy['slug'])));
}
}
if (isset($taxonomy['slug']) === false) {
$taxonomy['slug'] = Str::slug($taxonomy['name']);
}
if (isset($taxonomy['singular_slug']) === false) {
$taxonomy['singular_slug'] = Str::slug($taxonomy['singular_name']);
}
if (isset($taxonomy['has_sortorder']) === false) {
$taxonomy['has_sortorder'] = false;
}
if (isset($taxonomy['allow_spaces']) === false) {
$taxonomy['allow_spaces'] = false;
}
if (isset($taxonomy['allow_empty']) === false) {
$taxonomy['allow_empty'] = true;
}
if (isset($taxonomy['behaves_like']) === false) {
$taxonomy['behaves_like'] = 'tags';
}
if (isset($taxonomy['prefix']) === false) {
$taxonomy['prefix'] = '';
}
if (isset($taxonomy['postfix']) === false) {
$taxonomy['postfix'] = '';
}
if ($taxonomy['behaves_like'] === 'grouping') {
$taxonomy['multiple'] = false;
} elseif ($taxonomy['behaves_like'] === 'tags' || (isset($taxonomy['multiple']) && $taxonomy['multiple'] === true)) {
@@ -69,11 +93,12 @@ class TaxonomyParser extends BaseParser
if (isset($taxonomy['behaves_like']) === false) {
$taxonomy['behaves_like'] = 'tags';
}
// If taxonomy is like tags, set 'tagcloud' to true by default.
if ($taxonomy['behaves_like'] === 'tags' && isset($taxonomy['tagcloud']) === false) {
$taxonomy['tagcloud'] = true;
} else {
$taxonomy += ['tagcloud' => false];
$taxonomy['tagcloud'] = false;
}
$taxonomies[$key] = $taxonomy;
+10
View File
@@ -0,0 +1,10 @@
# Random yaml, to test parsing a nonsense file.
foo: bar
what:
- is
- going
- on
- here?
+2
View File
@@ -0,0 +1,2 @@
foo: OverBar
pom: pidom
+6
View File
@@ -0,0 +1,6 @@
# Broken yaml, to test parsing a broken file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praeclare enim Plato:
Beatum, cui etiam in senectute contigerit, ut sapientiam verasque opiniones
assequi possit. Quamquam tu hanc copiosiorem etiam soles dicere. Duae sunt enim
res quoque, ne tu verba solum putes. Quae tamen a te agetur non melior.
+16
View File
@@ -0,0 +1,16 @@
# Minimal ContentTypes
foo:
name: Bars
singular_name: Bar
fields: []
qux:
slug: corges
singular_slug: corge
fields: []
# Infer fields, and determine both the `slug` and `name` get inferred from the key
grault:
singular_name: Waldo
fields: []
+7
View File
@@ -0,0 +1,7 @@
# Minimal Taxonomy
foo:
name: Bar
qux:
slug: corge
+59
View File
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Bolt\Tests\Configuration\Parser;
use Bolt\Configuration\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TraceableAdapter;
use Symfony\Component\Cache\Simple\Psr6Cache;
use Symfony\Component\Stopwatch\Stopwatch;
use Tightenco\Collect\Support\Collection;
class ConfigTest extends TestCase
{
public function testCanParse(): void
{
$projectDir = dirname(__DIR__);
$cache = new Psr6Cache(new TraceableAdapter(new FilesystemAdapter()));
$config = new Config(new Stopwatch(), $projectDir, $cache);
$this->assertInstanceOf(Config::class, $config);
}
public function testConfigGet()
{
$projectDir = dirname(__DIR__);
$cache = new Psr6Cache(new TraceableAdapter(new FilesystemAdapter()));
$config = new Config(new Stopwatch(), $projectDir, $cache);
$this->assertSame('Bolt Four Website', $config->get('general/sitename'));
}
public function testConfigHas()
{
$projectDir = dirname(__DIR__);
$cache = new Psr6Cache(new TraceableAdapter(new FilesystemAdapter()));
$config = new Config(new Stopwatch(), $projectDir, $cache);
$this->assertTrue($config->has('general/payoff'));
$this->assertFalse($config->has('general/payoffXXXXX'));
}
public function testConfigGetMediaTypes()
{
$projectDir = dirname(__DIR__);
$cache = new Psr6Cache(new TraceableAdapter(new FilesystemAdapter()));
$config = new Config(new Stopwatch(), $projectDir, $cache);
/** @var Collection $mediaTypes */
$mediaTypes = $config->getMediaTypes();
$this->assertCount(8, $mediaTypes);
$this->assertTrue($mediaTypes->contains('png'));
$this->assertFalse($mediaTypes->contains('docx'));
}
}
@@ -0,0 +1,141 @@
<?php
declare(strict_types=1);
namespace Bolt\Tests\Configuration\Parser;
use Bolt\Configuration\Parser\ContentTypesParser;
use Bolt\Configuration\Parser\GeneralParser;
use Bolt\Exception\ConfigurationException;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Yaml\Exception\ParseException;
use Tightenco\Collect\Support\Collection;
class ContentTypesParserTest extends ParserTestBase
{
const NUMBER_OF_CONTENT_TYPES_IN_MINIMAL_FILE = 3;
const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 20;
public function testCanParse(): void
{
$generalParser = new GeneralParser();
$contentTypesParser = new ContentTypesParser($generalParser->parse());
$config = $contentTypesParser->parse();
$this->assertInstanceOf(Collection::class, $config);
}
public function testIgnoreNonsensicalFileParse(): void
{
$file = self::getBasePath() . 'bogus.yaml';
$generalParser = new GeneralParser();
$contentTypesParser = new ContentTypesParser($generalParser->parse(), $file);
$this->expectException(ConfigurationException::class);
$contentTypesParser->parse();
}
public function testBreakOnInvalidFileParse(): void
{
$file = self::getBasePath() . 'broken.yaml';
$generalParser = new GeneralParser();
$contentTypesParser = new ContentTypesParser($generalParser->parse(), $file);
$this->expectException(ParseException::class);
$contentTypesParser->parse();
}
public function testBreakOnMissingFileParse(): void
{
$generalParser = new GeneralParser();
$contentTypesParser = new ContentTypesParser($generalParser->parse(), 'foo.yml');
$this->expectException(FileLocatorFileNotFoundException::class);
$contentTypesParser->parse();
}
public function testHasConfig(): void
{
$generalParser = new GeneralParser();
$contentTypesParser = new ContentTypesParser($generalParser->parse());
$config = $contentTypesParser->parse();
$this->assertCount(6, $config);
$this->assertArrayHasKey('homepage', $config);
$this->assertCount(self::AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE, $config['homepage']);
$this->assertSame('Homepage', $config['homepage']['name']);
$this->assertSame('Homepage', $config['homepage']['singular_name']);
$this->assertCount(6, $config['homepage']['fields']);
$this->assertCount(9, $config['homepage']['fields']['title']);
$this->assertSame('Title', $config['homepage']['fields']['title']['label']);
$this->assertSame('text', $config['homepage']['fields']['title']['type']);
$this->assertTrue($config['homepage']['fields']['title']['localize']);
$this->assertTrue($config['homepage']['viewless']);
$this->assertTrue($config['homepage']['singleton']);
$this->assertSame('published', $config['homepage']['default_status']);
$this->assertSame('fa-home', $config['homepage']['icon_many']);
$this->assertSame('fa-home', $config['homepage']['icon_one']);
$this->assertFalse($config['homepage']['allow_numeric_slugs']);
}
public function testInferContentTypeValues(): void
{
$file = self::getBasePath() . 'minimal_content_types.yaml';
$generalParser = new GeneralParser();
$contentTypesParser = new ContentTypesParser($generalParser->parse(), $file);
$config = $contentTypesParser->parse();
$this->assertCount(self::NUMBER_OF_CONTENT_TYPES_IN_MINIMAL_FILE, $config);
$this->assertArrayHasKey('foo', $config);
$this->assertCount(self::AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE, $config['foo']);
$this->assertSame('Bars', $config['foo']['name']);
$this->assertSame('foo', $config['foo']['slug']);
$this->assertSame('Bar', $config['foo']['singular_name']);
$this->assertSame('bar', $config['foo']['singular_slug']);
$this->assertTrue($config['foo']['show_on_dashboard']);
$this->assertTrue($config['foo']['show_in_menu']);
$this->assertFalse($config['foo']['sort']);
$this->assertFalse($config['foo']['viewless']);
$this->assertSame('fa-file', $config['foo']['icon_one']);
$this->assertSame('fa-copy', $config['foo']['icon_many']);
$this->assertFalse($config['foo']['allow_numeric_slugs']);
$this->assertFalse($config['foo']['singleton']);
$this->assertSame('published', $config['foo']['default_status']);
$this->assertSame('bar', $config['foo']['singular_slug']);
$this->assertSame('bar', $config['foo']['singular_slug']);
$this->assertSame(6, $config['foo']['listing_records']);
$this->assertSame(10, $config['foo']['records_per_page']);
$this->assertIsIterable($config['foo']['fields']);
$this->assertIsIterable($config['foo']['locales']);
$this->assertIsIterable($config['foo']['groups']);
$this->assertIsIterable($config['foo']['taxonomy']);
$this->assertIsIterable($config['foo']['relations']);
$this->assertArrayHasKey('qux', $config);
$this->assertCount(self::AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE, $config['qux']);
$this->assertSame('Corges', $config['qux']['name']);
$this->assertSame('corges', $config['qux']['slug']);
$this->assertSame('Corge', $config['qux']['singular_name']);
$this->assertSame('corge', $config['qux']['singular_slug']);
$this->assertArrayHasKey('grault', $config);
$this->assertCount(self::AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE, $config['grault']);
$this->assertSame('Grault', $config['grault']['name']);
$this->assertSame('grault', $config['grault']['slug']);
$this->assertSame('Waldo', $config['grault']['singular_name']);
$this->assertSame('waldo', $config['grault']['singular_slug']);
}
}
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Bolt\Tests\Configuration\Parser;
use Bolt\Configuration\Parser\GeneralParser;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Yaml\Exception\ParseException;
use Tightenco\Collect\Support\Collection;
class GeneralParserTest extends ParserTestBase
{
public function testCanParse(): void
{
$generalParser = new GeneralParser();
$config = $generalParser->parse();
$this->assertInstanceOf(Collection::class, $config);
}
public function testIgnoreNonsensicalFileParse(): void
{
$file = self::getBasePath() . 'bogus.yaml';
$generalParser = new GeneralParser($file);
$config = $generalParser->parse();
$this->assertInstanceOf(Collection::class, $config);
}
public function testBreakOnInvalidFileParse(): void
{
$file = self::getBasePath() . 'broken.yaml';
$generalParser = new GeneralParser($file);
$this->expectException(ParseException::class);
$generalParser->parse();
}
public function testBreakOnMissingFileParse(): void
{
$generalParser = new GeneralParser('foo.yml');
$this->expectException(FileLocatorFileNotFoundException::class);
$generalParser->parse();
}
public function testHasConfig(): void
{
$generalParser = new GeneralParser();
$config = $generalParser->parse();
// Something in the file
$this->assertSame('The amazing payoff goes here', $config['payoff']);
// Something inferred
$this->assertFalse($config['enforce_ssl']);
}
public function testFilenames()
{
$file = self::getBasePath() . 'bogus.yaml';
$generalParser = new GeneralParser($file);
$config = $generalParser->parse();
$this->assertCount(2, $generalParser->getParsedFilenames());
$this->assertSame($file, $generalParser->getInitialFilename());
$this->assertSame(self::getBasePath() . 'bogus_local.yaml', $generalParser->getFilenameLocalOverrides());
}
public function testLocalOverridesParse(): void
{
$file = self::getBasePath() . 'bogus.yaml';
$generalParser = new GeneralParser($file);
$config = $generalParser->parse();
$this->assertSame('OverBar', $config['foo']);
$this->assertSame('pidom', $config['pom']);
}
}
@@ -5,20 +5,49 @@ declare(strict_types=1);
namespace Bolt\Tests\Configuration\Parser;
use Bolt\Configuration\Parser\MenuParser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Yaml\Exception\ParseException;
use Tightenco\Collect\Support\Collection;
class MenuParserTest extends TestCase
class MenuParserTest extends ParserTestBase
{
public function testCanParse(): void
{
$menuParser = new MenuParser();
$config = $menuParser->parse();
// @todo Test breakage on corrupt files. See Github issue https://github.com/bolt/four/issues/379
$this->assertInstanceOf(Collection::class, $config);
}
public function testIgnoreNonsensicalFileParse(): void
{
$file = self::getBasePath() . 'bogus.yaml';
$menuParser = new MenuParser($file);
$config = $menuParser->parse();
$this->assertInstanceOf(Collection::class, $config);
}
public function testBreakOnInvalidFileParse(): void
{
$file = self::getBasePath() . 'broken.yaml';
$menuParser = new MenuParser($file);
$this->expectException(ParseException::class);
$menuParser->parse();
}
public function testBreakOnMissingFileParse(): void
{
$menuParser = new MenuParser('foo.yml');
$this->expectException(FileLocatorFileNotFoundException::class);
$menuParser->parse();
}
public function testHasMenu(): void
{
$menuParser = new MenuParser();
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Bolt\Tests\Configuration\Parser;
use PHPUnit\Framework\TestCase;
abstract class ParserTestBase extends TestCase
{
public static function getBasePath(): string
{
return dirname(dirname(dirname(__DIR__))) . '/fixtures/config/';
}
}
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace Bolt\Tests\Configuration\Parser;
use Bolt\Configuration\Parser\TaxonomyParser;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Yaml\Exception\ParseException;
use Tightenco\Collect\Support\Collection;
class TaxonomyParserTest extends ParserTestBase
{
public function testCanParse(): void
{
$taxonomyParser = new TaxonomyParser();
$config = $taxonomyParser->parse();
$this->assertInstanceOf(Collection::class, $config);
}
public function testBreakOnInvalidFileParse(): void
{
$file = self::getBasePath() . 'broken.yaml';
$taxonomyParser = new TaxonomyParser($file);
$this->expectException(ParseException::class);
$taxonomyParser->parse();
}
public function testBreakOnMissingFileParse(): void
{
$taxonomyParser = new TaxonomyParser('foo.yml');
$this->expectException(FileLocatorFileNotFoundException::class);
$taxonomyParser->parse();
}
public function testHasTaxonomies(): void
{
$taxonomyParser = new TaxonomyParser();
$config = $taxonomyParser->parse();
$this->assertCount(3, $config);
$this->assertArrayHasKey('tags', $config);
$this->assertCount(13, $config['tags']);
$this->assertSame('tags', $config['tags']['slug']);
$this->assertSame('tag', $config['tags']['singular_slug']);
$this->assertSame('tags', $config['tags']['behaves_like']);
$this->assertSame('Add some freeform tags. Start a new tag by typing a comma or space.', $config['tags']['postfix']);
$this->assertFalse($config['tags']['allow_spaces']);
$this->assertSame('Tags', $config['tags']['name']);
$this->assertSame('Tag', $config['tags']['singular_name']);
$this->assertFalse($config['tags']['has_sortorder']);
$this->assertTrue($config['tags']['allow_empty']);
$this->assertTrue($config['tags']['multiple']);
$this->assertEmpty($config['tags']['options']);
$this->assertTrue($config['tags']['tagcloud']);
$this->assertCount(8, $config['categories']['options']);
$this->assertArrayNotHasKey('foobar', $config['tags']);
$this->assertArrayNotHasKey('foo', $config);
}
public function testInferTaxonomyValues(): void
{
$file = self::getBasePath() . 'minimal_taxonomy.yaml';
$taxonomyParser = new TaxonomyParser($file);
$config = $taxonomyParser->parse();
$this->assertCount(2, $config);
$this->assertArrayHasKey('foo', $config);
$this->assertCount(13, $config['foo']);
$this->assertSame('Bar', $config['foo']['name']);
$this->assertSame('bar', $config['foo']['slug']);
$this->assertSame('Bar', $config['foo']['singular_name']);
$this->assertSame('bar', $config['foo']['singular_slug']);
$this->assertFalse($config['foo']['has_sortorder']);
$this->assertFalse($config['foo']['allow_spaces']);
$this->assertSame('tags', $config['foo']['behaves_like']);
$this->assertSame('', $config['foo']['prefix']);
$this->assertSame('', $config['foo']['postfix']);
$this->assertTrue($config['foo']['allow_empty']);
$this->assertTrue($config['foo']['multiple']);
$this->assertEmpty($config['foo']['options']);
$this->assertTrue($config['foo']['tagcloud']);
$this->assertCount(13, $config['qux']);
$this->assertSame('Corge', $config['qux']['name']);
$this->assertSame('corge', $config['qux']['slug']);
$this->assertSame('Corge', $config['qux']['singular_name']);
$this->assertSame('corge', $config['qux']['singular_slug']);
}
}