Skip to content

Commit

Permalink
test: introduce snapshot testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Mar 21, 2021
1 parent 760c555 commit 39a5a5c
Show file tree
Hide file tree
Showing 58 changed files with 1,434 additions and 500 deletions.
26 changes: 26 additions & 0 deletions tests/LibraryStarterKit/SnapshotsTool.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Ramsey\Test\Dev\LibraryStarterKit;

use ReflectionClass;
use Spatie\Snapshots\MatchesSnapshots;

use function preg_replace;

trait SnapshotsTool
{
use MatchesSnapshots;

protected function getSnapshotId(): string
{
$snapshotId = (new ReflectionClass($this))->getShortName()
. '__'
. $this->getName()
. '__'
. $this->snapshotIncrementor;

return (string) preg_replace('/[^0-9a-z]/i', '_', $snapshotId);
}
}
179 changes: 10 additions & 169 deletions tests/LibraryStarterKit/Task/Builder/UpdateComposerJsonTest.php
Expand Up @@ -10,14 +10,20 @@
use Ramsey\Dev\LibraryStarterKit\Setup;
use Ramsey\Dev\LibraryStarterKit\Task\Build;
use Ramsey\Dev\LibraryStarterKit\Task\Builder\UpdateComposerJson;
use Ramsey\Test\Dev\LibraryStarterKit\SnapshotsTool;
use Ramsey\Test\Dev\LibraryStarterKit\TestCase;
use Ramsey\Test\Dev\LibraryStarterKit\WindowsSafeTextDriver;
use RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

use function file_get_contents;

class UpdateComposerJsonTest extends TestCase
{
use SnapshotsTool;

public function testBuild(): void
{
$console = $this->mockery(SymfonyStyle::class);
Expand All @@ -29,10 +35,7 @@ public function testBuild(): void
->once()
->withArgs(function (string $path, string $contents) {
$this->assertSame('/path/to/app/composer.json', $path);
$this->assertJsonStringEqualsJsonString(
$this->composerContentsExpected(),
$contents,
);
$this->assertMatchesSnapshot($contents, new WindowsSafeTextDriver());

return true;
});
Expand Down Expand Up @@ -159,10 +162,7 @@ public function testBuildWithMinimalComposerJson(): void
->once()
->withArgs(function (string $path, string $contents) {
$this->assertSame('/path/to/app/composer.json', $path);
$this->assertJsonStringEqualsJsonString(
$this->composerContentsExpectedMinimal(),
$contents,
);
$this->assertMatchesSnapshot($contents, new WindowsSafeTextDriver());

return true;
});
Expand Down Expand Up @@ -208,170 +208,11 @@ public function testBuildWithMinimalComposerJson(): void

private function composerContentsOriginal(): string
{
return <<<'EOD'
{
"name": "ramsey/php-library-starter-kit",
"type": "project",
"description": "A starter kit for quickly setting up a new PHP library package.",
"keywords": [
"builder",
"library",
"package",
"skeleton",
"template"
],
"license": "MIT",
"authors": [
{
"name": "Ben Ramsey",
"email": "ben@benramsey.com",
"homepage": "https://benramsey.com"
}
],
"require": {
"php": "^7.4 || ^8",
"ext-json": "*",
"symfony/console": "^5.1",
"symfony/filesystem": "^5.1",
"symfony/finder": "^5.1",
"symfony/process": "^5.1",
"twig/twig": "^3.1"
},
"require-dev": {
"ramsey/devtools": "^1.5",
"spatie/phpunit-snapshot-assertions": "^4.2"
},
"suggest": {
"ext-foobar": "Foo bar"
},
"config": {
"sort-packages": true
},
"extra": {
"ramsey/conventional-commits": {
"configFile": "conventional-commits.json"
},
"ramsey/devtools": {
"command-prefix": "dev"
}
},
"autoload": {
"psr-4": {
"Ramsey\\Dev\\LibraryStarterKit\\": "src/LibraryStarterKit/",
"Vendor\\SubNamespace\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Ramsey\\Test\\Dev\\LibraryStarterKit\\": "tests/LibraryStarterKit/",
"Vendor\\Test\\SubNamespace\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-root-package-install": "git init",
"post-create-project-cmd": "Ramsey\\Dev\\LibraryStarterKit\\Wizard::start",
"starter-kit": "Ramsey\\Dev\\LibraryStarterKit\\Wizard::start"
},
"scripts-descriptions": {
"starter-kit": "Runs the PHP Library Starter Kit wizard."
}
}
EOD;
}

private function composerContentsExpected(): string
{
return <<<'EOD'
{
"name": "a-vendor/package-name",
"type": "library",
"description": "This is a test package.",
"keywords": [
"test",
"package"
],
"license": "Apache-2.0",
"authors": [
{
"name": "Jane Doe",
"email": "jdoe@example.com",
"homepage": "https://example.com/jane"
}
],
"require": {
"php": "^7.4 || ^8"
},
"require-dev": {
"ramsey/devtools": "^1.5"
},
"config": {
"sort-packages": true
},
"extra": {
"ramsey/conventional-commits": {
"configFile": "conventional-commits.json"
},
"ramsey/devtools": {
"command-prefix": "dev"
}
},
"autoload": {
"psr-4": {
"Vendor\\SubNamespace\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Vendor\\Test\\SubNamespace\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
EOD;
return (string) file_get_contents(__DIR__ . '/fixtures/composer-full.json');
}

private function composerContentsOriginalMinimal(): string
{
return <<<'EOD'
{
"name": "ramsey/php-library-starter-kit",
"type": "project",
"description": "A tool to quickly set up the base files of a PHP library package.",
"keywords": [
"skeleton",
"package",
"library"
],
"license": "MIT",
"authors": [
{
"name": "Ben Ramsey",
"email": "ben@benramsey.com",
"homepage": "https://benramsey.com"
}
]
}
EOD;
}

private function composerContentsExpectedMinimal(): string
{
return <<<'EOD'
{
"name": "a-vendor/package-name",
"type": "library",
"description": "This is a test package.",
"keywords": [],
"license": "MPL-2.0",
"authors": [
{
"name": "Jane Doe"
}
]
}
EOD;
return (string) file_get_contents(__DIR__ . '/fixtures/composer-minimal.json');
}
}
84 changes: 13 additions & 71 deletions tests/LibraryStarterKit/Task/Builder/UpdateNamespaceTest.php
Expand Up @@ -10,15 +10,19 @@
use Ramsey\Dev\LibraryStarterKit\Setup;
use Ramsey\Dev\LibraryStarterKit\Task\Build;
use Ramsey\Dev\LibraryStarterKit\Task\Builder\UpdateNamespace;
use Ramsey\Test\Dev\LibraryStarterKit\SnapshotsTool;
use Ramsey\Test\Dev\LibraryStarterKit\TestCase;
use Ramsey\Test\Dev\LibraryStarterKit\WindowsSafeTextDriver;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

use function str_replace;
use function file_get_contents;

class UpdateNamespaceTest extends TestCase
{
use SnapshotsTool;

/**
* @dataProvider provideNamespaceTestValues
*/
Expand Down Expand Up @@ -73,25 +77,14 @@ public function testBuild(
$filesystem = $this->mockery(Filesystem::class);

$filesystem
->expects()
->dumpFile(
'/path/to/app/src/Foo.php',
$this->getFileContentsExpected($packageName, $namespace, $testNamespace),
);
->shouldReceive('dumpFile')
->times(3)
->withArgs(function (string $path, string $contents) {
$this->assertMatchesSnapshot($path, new WindowsSafeTextDriver());
$this->assertMatchesSnapshot($contents, new WindowsSafeTextDriver());

$filesystem
->expects()
->dumpFile(
'/path/to/app/src/Bar.php',
$this->getFileContentsExpected($packageName, $namespace, $testNamespace),
);

$filesystem
->expects()
->dumpFile(
'/path/to/app/composer.json',
$this->getFileContentsExpected($packageName, $namespace, $testNamespace),
);
return true;
});

$environment = $this->mockery(Setup::class, [
'getAppPath' => '/path/to/app',
Expand Down Expand Up @@ -145,57 +138,6 @@ public function provideNamespaceTestValues(): array

private function getFileContents(): string
{
return <<<'EOD'
<?php
/**
* File header comment for ramsey/php-library-starter-kit
*/
declare(strict_types=1);
namespace Vendor\SubNamespace;
use Vendor\Test\SubNamespace\Bar;
class Foo
{
public const CLASS_NAMES = [
'Vendor\\SubNamespace',
'Vendor\\Test\\SubNamespace',
];
}
EOD;
}

private function getFileContentsExpected(
string $packageName,
string $namespace,
string $testNamespace
): string {
$namespaceEscaped = str_replace('\\', '\\\\', $namespace);
$testNamespaceEscaped = str_replace('\\', '\\\\', $testNamespace);

return <<<EOD
<?php
/**
* File header comment for {$packageName}
*/
declare(strict_types=1);
namespace {$namespace};
use {$testNamespace}\\Bar;
class Foo
{
public const CLASS_NAMES = [
'{$namespaceEscaped}',
'{$testNamespaceEscaped}',
];
}
EOD;
return (string) file_get_contents(__DIR__ . '/fixtures/update-namespace-test.php');
}
}

0 comments on commit 39a5a5c

Please sign in to comment.