Skip to content

Commit

Permalink
more tests, more work on generatePhpEnum()
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsizemore committed Apr 25, 2024
1 parent 26f3147 commit 550a046
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/Mapping/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public function generateMapping(): array
public function generatePhpEnum(string $classname = 'MimeType', string $namespace = 'Esi\Mimey'): string
{
$values = [
'namespace' => $namespace,
'classname' => $classname,
'interface_usage' => $namespace !== __NAMESPACE__ ? ('use ' . MimeTypeInterface::class . ";\n") : '',
'cases' => '',
'type2ext' => '',
'ext2type' => '',
'%namespace%' => $namespace,
'%classname%' => $classname,
'%interface_usage%' => $namespace !== __NAMESPACE__ ? ('use ' . MimeTypeInterface::class . ";\n") : '',
'%cases%' => '',
'%type2ext%' => '',
'%ext2type%' => '',
];

$stubContents = (string) file_get_contents(\dirname(__DIR__, 2) . '/stubs/mimeType.php.stub');
Expand All @@ -160,19 +160,19 @@ public function generatePhpEnum(string $classname = 'MimeType', string $namespac
foreach ($mapping['extensions'] as $mime => $extensions) {
$nameMap[$mime] = $this->convertMimeTypeToCaseName($mime);

$values['cases'] .= sprintf(Generator::spaceIndent(4, "case %s = '%s';\n"), $nameMap[$mime], $mime);
$values['type2ext'] .= sprintf(Generator::spaceIndent(12, "self::%s => '%s',\n"), $nameMap[$mime], $extensions[0]);
$values['%cases%'] .= sprintf(Generator::spaceIndent(4, "case %s = '%s';\n"), $nameMap[$mime], $mime);
$values['%type2ext%'] .= sprintf(Generator::spaceIndent(12, "self::%s => '%s',\n"), $nameMap[$mime], $extensions[0]);
}

foreach ($mapping['mimes'] as $extension => $mimes) {
$values['ext2type'] .= sprintf(Generator::spaceIndent(12, "'%s' => self::%s,\n"), $extension, $nameMap[$mimes[0]]);
$values['%ext2type%'] .= sprintf(Generator::spaceIndent(12, "'%s' => self::%s,\n"), $extension, $nameMap[$mimes[0]]);
}

foreach ($values as $name => $value) {
$stubContents = str_replace("%$name%", $value, $stubContents);
}

return $stubContents;
return str_replace(
array_keys($values),
array_values($values),
$stubContents
);
}

/**
Expand All @@ -188,7 +188,7 @@ protected function convertMimeTypeToCaseName(string $mimeType): string
}

/**
* Helper function for self::generateMapping()
* Helper function for self::generateMapping().
*
* @param list<string> $parts
*/
Expand Down
109 changes: 109 additions & 0 deletions tests/src/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,85 @@ public static function tryFromExtension(string \$extension): ?MimeType
);
}

/**
* Test generating the PHP Enum from the given mime.types text.
*/
public function testGeneratePhpEnumDefault(): void
{
$generator = new Generator(
<<<EOF
#ignore
application/json\tjson
image/jpeg\tjpeg jpg
EOF
);

$phpEnum = $generator->generatePhpEnum();

self::assertSame(
<<<EOF
<?php
/**
* @generated enum generated using bin/generate.php, please DO NOT EDIT!
*
* @codeCoverageIgnore
*/
declare(strict_types=1);
namespace Esi\Mimey;
use InvalidArgumentException;
use Esi\Mimey\Interface\MimeTypeInterface;
enum MimeType: string implements MimeTypeInterface
{
case ApplicationJson = 'application/json';
case ImageJpeg = 'image/jpeg';
#[\Override]
public function getExtension(): string
{
return match(\$this) {
self::ApplicationJson => 'json',
self::ImageJpeg => 'jpeg',
};
}
#[\Override]
public function getValue(): string
{
return \$this->value;
}
public static function fromExtension(string \$extension): MimeType
{
\$type = self::tryFromExtension(\$extension);
if (\$type === null) {
throw new InvalidArgumentException('Unknown extension: ' . \$extension);
}
return \$type;
}
public static function tryFromExtension(string \$extension): ?MimeType
{
return match(\$extension) {
'json' => self::ApplicationJson,
'jpeg' => self::ImageJpeg,
'jpg' => self::ImageJpeg,
default => null,
};
}
}
EOF,
$phpEnum
);
}

/**
* Test generating the PHP Enum when given invalid mime.types text.
*/
Expand All @@ -217,6 +296,36 @@ public function testGeneratePhpEnumInvalid(): void
$generator->generatePhpEnum('TestMimeClass', 'TestMimeNamespace');
}

/**
* Test generating the PHP Enum when given invalid mime.types text.
*
* @psalm-suppress InvalidArgument
*/
public function testGeneratePhpEnumInvalidNoParam(): void
{
$generator = new Generator('');

Check failure on line 306 in tests/src/GeneratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis 8.2

Parameter #1 $mimeTypesText of class Esi\Mimey\Mapping\Generator constructor expects non-empty-string, '' given.

Check failure on line 306 in tests/src/GeneratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis 8.3

Parameter #1 $mimeTypesText of class Esi\Mimey\Mapping\Generator constructor expects non-empty-string, '' given.

$this->expectException(RuntimeException::class);
$generator->generatePhpEnum('TestMimeClass', 'TestMimeNamespace');
}

/**
* Test generating the PHP Enum when given invalid mime.types text.
*/
public function testGeneratePhpEnumInvalidNoTab(): void
{
$generator = new Generator(
<<<'EOF'
#ignore
application/jsonjson
image/jpegjpegjpg
EOF
);

$this->expectException(RuntimeException::class);
$generator->generatePhpEnum('TestMimeClass', 'TestMimeNamespace');
}

public function testSpaceIndent(): void
{
$spaceIndent = new ReflectionMethod(Generator::class, 'spaceIndent');
Expand Down

0 comments on commit 550a046

Please sign in to comment.