Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock file maintenance, introduce test case for templatemap_generator.php #217

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions bin/templatemap_generator.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

$help = <<< EOH
$help = <<<'EOH'
gsteel marked this conversation as resolved.
Show resolved Hide resolved
Generate template maps.
Usage:
Expand Down Expand Up @@ -86,7 +86,7 @@
}

$basePath = $argv[1];
$files = ($argc < 3)
$files = $argc < 3
? findTemplateFilesInTemplatePath($basePath)
: array_slice($argv, 2);

Expand All @@ -99,14 +99,14 @@

$realPath = realpath($basePath);

$entries = array_map(function ($file) use ($basePath, $realPath) {
$entries = array_map(function (string $file) use ($basePath, $realPath) {
$file = str_replace('\\', '/', $file);

$template = (0 === strpos($file, $realPath))
$template = 0 === strpos($file, $realPath)
? substr($file, strlen($realPath))
: $file;

$template = (0 === strpos($template, $basePath))
$template = 0 === strpos($template, $basePath)
? substr($template, strlen($basePath))
: $template;

Expand All @@ -126,7 +126,8 @@
exit(0);

/**
* @psalm-return list<mixed>
* @param string $templatePath
* @return list<string>
*/
function findTemplateFilesInTemplatePath($templatePath): array
{
Expand All @@ -138,7 +139,8 @@ function findTemplateFilesInTemplatePath($templatePath): array

$files = [];
foreach ($rii as $file) {
if (strtolower($file->getExtension()) != 'phtml') {
assert($file instanceof SplFileInfo);
if (strtolower($file->getExtension()) !== 'phtml') {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"psr/container": "^1 || ^2"
},
"require-dev": {
"laminas/laminas-authentication": "^2.13",
"laminas/laminas-authentication": "^2.15",
"laminas/laminas-coding-standard": "~2.5.0",
"laminas/laminas-feed": "^2.20",
"laminas/laminas-feed": "^2.21",
"laminas/laminas-filter": "^2.32",
"laminas/laminas-http": "^2.18",
"laminas/laminas-i18n": "^2.23",
Expand All @@ -52,9 +52,9 @@
"laminas/laminas-permissions-acl": "^2.15",
"laminas/laminas-router": "^3.11.1",
"laminas/laminas-uri": "^2.10",
"phpunit/phpunit": "^10.1.3",
"phpunit/phpunit": "^10.3.2",
"psalm/plugin-phpunit": "^0.18.4",
"vimeo/psalm": "^5.12"
"vimeo/psalm": "^5.15"
},
"conflict": {
"container-interop/container-interop": "<1.2",
Expand Down
17 changes: 10 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions test/bin/TemplateMapGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace LaminasTest\View\bin;

use PHPUnit\Framework\TestCase;

use function chdir;
use function sprintf;

use const PHP_EOL;

final class TemplateMapGeneratorTest extends TestCase
{
private function commandOutput(string $args): string
{
chdir(__DIR__);

$command = sprintf(__DIR__ . '/../../bin/templatemap_generator.php %s', $args);

/** @psalm-suppress ForbiddenCode */
$output = `$command`; // phpcs:ignore

self::assertIsString($output);

return $output;
}

public function testThatTheTemplateMapGeneratorProducesTheExpectedOutput(): void
{
$output = $this->commandOutput('templates');

self::assertStringStartsWith('<?php' . PHP_EOL . 'return [', $output);
self::assertStringEndsWith('];', $output);
self::assertStringContainsString("'one' => __DIR__ . '/templates/one.phtml',", $output);
self::assertStringContainsString("'two' => __DIR__ . '/templates/two.phtml',", $output);

self::assertStringNotContainsString('ignored.txt', $output);
self::assertStringNotContainsString('ignored', $output);
}

public function testThatHelpTextWillBeOutputWhenRequested(): void
{
$output = $this->commandOutput('-h');
self::assertStringContainsString('Generate template maps.', $output);

$output = $this->commandOutput('--help');
self::assertStringContainsString('Generate template maps.', $output);
}

public function testThatTheMapGeneratorAcceptsAListOfFileNames(): void
{
$output = $this->commandOutput('. some/foo.txt bar.phtml');
self::assertStringStartsWith('<?php' . PHP_EOL . 'return [', $output);
self::assertStringEndsWith('];', $output);
self::assertStringContainsString("'some/foo' => __DIR__ . '/some/foo.txt',", $output);
self::assertStringContainsString("'bar' => __DIR__ . '/bar.phtml',", $output);
}
}
Empty file added test/bin/templates/ignored.txt
Empty file.
Empty file.
Empty file added test/bin/templates/one.phtml
Empty file.
Empty file added test/bin/templates/two.phtml
Empty file.