Skip to content

Commit

Permalink
Merge pull request #1 from thettunaung-carro/test/add-test-cases
Browse files Browse the repository at this point in the history
feat: add test cases
  • Loading branch information
tintnaingwinn authored Apr 24, 2024
2 parents 93c195a + e6e272c commit 1fac27a
Show file tree
Hide file tree
Showing 12 changed files with 503 additions and 27 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run tests
on:
push:
paths:
- "**.php"
- "phpunit.xml"
- "composer.json"
- "composer.lock"

jobs:
pest:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: composer:v2
coverage: none

- name: Install composer dependencies
uses: nick-fields/retry@v2
with:
timeout_minutes: 3
max_attempts: 5
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress

- name: Run pest
timeout-minutes: 2
run: ./vendor/bin/pest
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ php ./vendor/bin/vapor-ignore init

### vapor-ignore.yml

#### `ignore` directive
#### `ignore` directive

Ignore the specified file or directory.

#### `vendor` directive

Ignore the specified file or directory from the `vendor` directory.

- readme - Ignore the `readme.md` file from the `vendor` directory.
- changelog - Ignore the `changelog.md` file from the `vendor` directory.
- contributing - Ignore the `contributing.md` file from the `vendor` directory.
- upgrade - Ignore the `upgrade.md` file from the `vendor` directory.
- tests - Ignore the `tests` directory from the `vendor` directory.
- security - Ignore the `security.md` file from the `vendor` directory.
- license - Ignore the `license.md` file from the `vendor` directory.
- laravel-idea - Ignore the `laravel-idea` directory from the `vendor` directory.
- .github - Ignore the `.github` directory from the `vendor` directory.
- dotfiles - Ignore the dotfiles(`.editorconfig`, `.gitignore`, `.gitattributes` and `.php_cs.dist.php`) from the `vendor` directory.
- readme - Ignore the `readme.md` file from the `vendor` directory.
- changelog - Ignore the `changelog.md` file from the `vendor` directory.
- contributing - Ignore the `contributing.md` file from the `vendor` directory.
- upgrade - Ignore the `upgrade.md` file from the `vendor` directory.
- tests - Ignore the `tests` directory from the `vendor` directory.
- security - Ignore the `security.md` file from the `vendor` directory.
- license - Ignore the `license.md` file from the `vendor` directory.
- laravel-idea - Ignore the `laravel-idea` directory from the `vendor` directory.
- .github - Ignore the `.github` directory from the `vendor` directory.
- dotfiles - Ignore the dotfiles(`.editorconfig`, `.gitignore`, `.gitattributes` and `.php_cs.dist.php`) from the `vendor` directory.

## Testing
```bash
composer test
```

## Changelog

Expand All @@ -57,8 +64,8 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [Tint Naing Win](https://github.com/tintnaingwinn)
- [All Contributors](../../contributors)
- [Tint Naing Win](https://github.com/tintnaingwinn)
- [All Contributors](../../contributors)

## License

Expand Down
28 changes: 23 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "ageekdev/vapor-ignore",
"version": "1.1.0",
"description": "To clean unnecessary files for Laravel Vapor deployment",
"keywords": [
"laravel", "vapor-ignore", "vapor", "deployment", "serverless", "laravel-vapor", "ignore"
"laravel",
"vapor-ignore",
"vapor",
"deployment",
"serverless",
"laravel-vapor",
"ignore"
],
"homepage": "https://github.com/ageekdev/vapor-ignore",
"license": "MIT",
Expand All @@ -22,29 +29,40 @@
"laravel/vapor-cli": "^1.40"
},
"require-dev": {
"laravel/pint": "^1.10",
"larastan/larastan": "^2.4.1",
"laravel/pint": "^1.10",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"pestphp/pest": "^2.34",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-4": {
"AgeekDev\\VaporIgnore\\": "src/"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"test": "vendor/bin/pest"
},
"config": {
"sort-packages": true,
"optimize-autoloader": true,
"allow-plugins": {
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
15 changes: 10 additions & 5 deletions src/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ public static function isIgnoredVendor(string $name): bool
/**
* Write a fresh manifest file.
*/
public static function init(): void
public static function init(array $configuration = []): void
{
static::freshConfiguration();
static::createFreshManifestFile($configuration);
}

/**
* Write a fresh vapor-ignore manifest file.
*/
protected static function freshConfiguration(): void
protected static function createFreshManifestFile(array $configuration = []): void
{
static::write($configuration ?: static::freshConfiguration());
}

public static function freshConfiguration(): array
{
$vendor = [
'readme' => true,
Expand All @@ -74,10 +79,10 @@ protected static function freshConfiguration(): void
'/tests',
];

static::write([
return [
'vendor' => $vendor,
'ignore' => $ignore,
]);
];
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

if (! function_exists('getPackageName')) {
function getPackageName(): string
{
return 'Vapor Ignore';
}
}

if (! function_exists('getPackageVersion')) {
function getPackageVersion(): string
{
return Composer\InstalledVersions::getRootPackage()['pretty_version'];
}
}
33 changes: 33 additions & 0 deletions tests/Feature/CleanIgnoredFilesCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use AgeekDev\VaporIgnore\Commands\CleanIgnoredFilesCommand;
use AgeekDev\VaporIgnore\Manifest;
use AgeekDev\VaporIgnore\Path;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\TestCase;

use function PHPUnit\Framework\assertFileDoesNotExist;
use function PHPUnit\Framework\assertStringContainsString;

it('can run clean:ignored-files command', function (): void {
Manifest::init([
'vendor' => [],
]);

/**
* @var TestCase $this
*/
$command = $this->resolveCommand(CleanIgnoredFilesCommand::class);

$tester = new CommandTester($command);

$tester->execute([
'command' => $command->getName(),
]);

$tester->assertCommandIsSuccessful();

assertFileDoesNotExist(Path::defaultManifest());

assertStringContainsString('Total Removed Files Size:', $tester->getDisplay());
});
132 changes: 132 additions & 0 deletions tests/Feature/InitCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

use AgeekDev\VaporIgnore\Commands\InitCommand;
use AgeekDev\VaporIgnore\Manifest;
use AgeekDev\VaporIgnore\Path;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Yaml\Yaml;
use Tests\TestCase;

use function PHPUnit\Framework\assertFileExists;
use function PHPUnit\Framework\assertStringMatchesFormatFile;

it('can run init command', function (): void {
/**
* @var TestCase $this
*/
$command = $this->resolveCommand(InitCommand::class);

$tester = new CommandTester($command);

$tester->setInputs([
'no',
]);

$tester->execute([
'command' => $command->getName(),
]);

assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration()));
});

it('can run init command with force option', function (): void {
touch(Path::defaultManifest());

/**
* @var TestCase $this
*/
$command = $this->resolveCommand(InitCommand::class);

$tester = new CommandTester($command);

$tester->setInputs([
'no',
]);

$tester->execute([
'command' => $command->getName(),
'--force' => true,
]);

assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration()));
});

it('can run init command and override', function (): void {
touch(Path::defaultManifest());

/**
* @var TestCase $this
*/
$command = $this->resolveCommand(InitCommand::class);

$tester = new CommandTester($command);

$tester->setInputs([
'yes',
'no',
]);

$tester->execute([
'command' => $command->getName(),
]);

assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration()));
});

it('can run init command with custom manifest', function (): void {
$customManifest = getcwd().'/vapor-ignore-test.yml';

/**
* @var TestCase $this
*/
$command = $this->resolveCommand(InitCommand::class);

$tester = new CommandTester($command);

$tester->setInputs([
'no',
]);

$tester->execute([
'command' => $command->getName(),
'--manifest' => $customManifest,
]);

assertInitCommandRunPorperly($tester, $customManifest, Yaml::dump(Manifest::freshConfiguration()));

removeFile($customManifest);
});

it('can run init command and add clean command', function (): void {
createVaporManifest();

/**
* @var TestCase $this
*/
$command = $this->resolveCommand(InitCommand::class);

$tester = new CommandTester($command);

$tester->setInputs([
'yes',
]);

$tester->execute([
'command' => $command->getName(),
]);

assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration()));

assertFileExists($vaporPath = Path::vaporManifest());

assertStringMatchesFormatFile($vaporPath, vaporManifestContentWithCleanCommand());
});

function assertInitCommandRunPorperly(CommandTester $tester, string $path, string $format): void
{
$tester->assertCommandIsSuccessful();

assertFileExists($path);

assertStringMatchesFormatFile($path, $format);
}
Loading

0 comments on commit 1fac27a

Please sign in to comment.