Skip to content

Commit

Permalink
Refactor tests to pest (#99)
Browse files Browse the repository at this point in the history
* install Pest

* wip

* change directory structure

* cleanup

* cleanup

* change test suite in Github action and `composer.json`

* Fix styling

---------

Co-authored-by: alexmanase <[email protected]>
Co-authored-by: Ruben Van Assche <[email protected]>
  • Loading branch information
3 people authored Feb 10, 2023
1 parent a26ddbd commit 8569775
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ jobs:
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
"symfony/psr-http-message-bridge": "^2.1"
},
"require-dev": {
"pestphp/pest-plugin-laravel": "^1.3",
"orchestra/testbench": "^6.23|^7.0|^8.0",
"phpunit/phpunit": "^9.5"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand All @@ -50,15 +54,15 @@
"Spatie\\Export\\": "src"
}
},
"minimum-stability" : "dev",
"prefer-stable" : true,
"minimum-stability": "dev",
"prefer-stable": true,
"autoload-dev": {
"psr-4": {
"Spatie\\Export\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage-html coverage"
}
}
}
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
99 changes: 99 additions & 0 deletions tests/ExportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

use Illuminate\Support\Facades\Route;

use function PHPUnit\Framework\assertEquals;

use function PHPUnit\Framework\assertFileExists;

use Spatie\Export\Exporter;

const HOME_CONTENT = '<a href="feed/blog.atom" title="all blogposts">Feed</a>Home <a href="about">About</a>';
const ABOUT_CONTENT = 'About';
const FEED_CONTENT = 'Feed';

function assertHomeExists(): void
{
assertExportedFile(__DIR__ . '/dist/index.html', HOME_CONTENT);
}

function assertAboutExists(): void
{
assertExportedFile(__DIR__ . '/dist/about/index.html', ABOUT_CONTENT);
}

function assertFeedBlogAtomExists(): void
{
assertExportedFile(__DIR__ . '/dist/feed/blog.atom', FEED_CONTENT);
}

function assertExportedFile(string $path, string $content): void
{
assertFileExists($path);
assertEquals($content, file_get_contents($path));
}

beforeEach(function () {
$this->distDirectory = __DIR__ . DIRECTORY_SEPARATOR . 'dist';

if (file_exists($this->distDirectory)) {
exec(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'
? 'del ' . $this->distDirectory . ' /q'
: 'rm -r ' . $this->distDirectory);
}

Route::get('/', function () {
return HOME_CONTENT;
});

Route::get('about', function () {
return ABOUT_CONTENT;
});

Route::get('feed/blog.atom', function () {
return FEED_CONTENT;
});
});

afterEach(function () {
assertHomeExists();
assertAboutExists();
assertFeedBlogAtomExists();
});

it('crawls and exports routes', function () {
app(Exporter::class)->export();
});

it('exports paths', function () {
app(Exporter::class)
->crawl(false)
->paths(['/', '/about', '/feed/blog.atom'])
->export();
});

it('exports urls', function () {
app(Exporter::class)
->crawl(false)
->urls([url('/'), url('/about'), url('/feed/blog.atom')])
->export();
});

it('exports mixed', function () {
app(Exporter::class)
->crawl(false)
->paths('/')
->urls(url('/about'), url('/feed/blog.atom'))
->export();
});

it('exports included files', function () {
app(Exporter::class)
->includeFiles([__DIR__ . '/stubs/public' => ''])
->export();

assertFileExists(__DIR__ . '/dist/favicon.ico');
assertFileExists(__DIR__ . '/dist/media/image.png');

expect(file_exists(__DIR__ . '/dist/index.php'))->toBeFalse();
});
1 change: 0 additions & 1 deletion tests/Feature/.gitignore

This file was deleted.

144 changes: 0 additions & 144 deletions tests/Feature/ExportTest.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

uses(Spatie\Export\Tests\TestCase::class)->in('.');
25 changes: 25 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\Export\Tests;

use Orchestra\Testbench\TestCase as BaseTestCase;
use Spatie\Export\ExportServiceProvider;

class TestCase extends BaseTestCase
{
protected function getPackageProviders($app)
{
return [ExportServiceProvider::class];
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('filesystems.disks.export', [
'driver' => 'local',
'root' => __DIR__ . '/dist',
]);

$app['config']->set('export.disk', 'export');
$app['config']->set('export.include_files', []);
}
}
1 change: 1 addition & 0 deletions tests/dist/about/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
About
1 change: 1 addition & 0 deletions tests/dist/feed/blog.atom
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feed
1 change: 1 addition & 0 deletions tests/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="feed/blog.atom" title="all blogposts">Feed</a>Home <a href="about">About</a>

0 comments on commit 8569775

Please sign in to comment.