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

Add test suite #5

Merged
merged 8 commits into from
Jan 7, 2024
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
47 changes: 47 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
on:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should note that I haven't been able to test this workflow. So it might require some tweaking after it's been merged into main.

workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
run-tests:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.3]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, imagick
coverage: none

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Execute tests
run: |
npm install
vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.DS_Store
/vendor
/composer.lock
/node_modules
/phpunit.xml
/.phpunit.cache
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,9 @@ More layouts are coming.

## Testing

There's a basic test script in `tests/test.php`. You can execute this directly from the command line:

```shell
php tests/test.php
```

But a more robust test suite is needed and is coming.
The OG uses [snapshot testing](https://github.com/spatie/phpunit-snapshot-assertions). To run the integration tests,
install [NodeJS](https://nodejs.org/en) (version 20 or above) and install the dependencies with `npm install`. Lastly,
execute the tests with `./vendor/bin/phpunit`.

## Contributing

Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@
"SimonHamp\\TheOg\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"authors": [
{
"name": "Simon Hamp",
"email": "simon.hamp@me.com"
}
]
],
"require-dev": {
"spatie/phpunit-snapshot-assertions": "^5.1",
"phpunit/phpunit": "^10.5",
"spatie/pixelmatch-php": "^1.0"
}
}
33 changes: 33 additions & 0 deletions package-lock.json

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

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"pixelmatch": "^5.3.0"
}
}
11 changes: 11 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Integration">
<directory>tests/Integration</directory>
</testsuite>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
78 changes: 78 additions & 0 deletions tests/Integration/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Tests\Integration;

use PHPUnit\Framework\Attributes\DataProvider;
use SimonHamp\TheOg\Background;
use SimonHamp\TheOg\Image;
use SimonHamp\TheOg\Themes\Themes;
use Spatie\Snapshots\MatchesSnapshots;

class ImageTest extends IntegrationTestCase
{
use MatchesSnapshots;

#[DataProvider('snapshotImages')]
public function test_basic_image(Image $image, string $name): void
{
$path = self::TESTCASE_DIRECTORY.'/'.$name.'.png';

$image->save($path);

$this->assertMatchesImageSnapshot($path);
}

public static function snapshotImages(): iterable
{
yield 'basic' => [
(new Image())
->url('https://example.com/blog/some-blog-post-url')
->title('Some blog post title that is quite big and quite long')
->description('Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words'),
'basic',
];

yield 'different theme' => [
(new Image())
->theme(Themes::Dark)
->url('https://example.com/blog/some-blog-post-url')
->title('Some blog post title that is quite big and quite long')
->description('Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words'),
'different-theme',
];

yield 'override some elements' => [
(new Image())
->accentColor('#cc0000')
->url('https://example.com/blog/some-blog-post-url')
->title('Some blog post title that is quite big and quite long')
->description(<<<'TEXT'
Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words.

Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words.
TEXT
)
->background(Background::JustWaves, 0.2),
'override-some-elements',
];

yield 'basic with background url' => [
(new Image())
->url('https://example.com/blog/some-blog-post-url')
->title('Some blog post title that is quite big and quite long')
->description('Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words')
->backgroundUrl('https://www.goodfreephotos.com/albums/animals/mammals/african-bush-elephant-loxodonta-africana.jpg', 0.2),
'basic-with-background-url',
];

yield 'different theme with background url' => [
(new Image())
->theme(Themes::Dark)
->url('https://example.com/blog/some-blog-post-url')
->title('Some blog post title that is quite big and quite long')
->description('Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words')
->backgroundUrl('https://www.goodfreephotos.com/albums/animals/mammals/african-bush-elephant-loxodonta-africana.jpg', 0.2),
'different-theme-with-background-url',
];
}
}
27 changes: 27 additions & 0 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Integration;

use FilesystemIterator;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class IntegrationTestCase extends TestCase
{
protected const TESTCASE_DIRECTORY = __DIR__.'/../resources/testcases';

protected function tearDown(): void
{
TestCase::tearDown();

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(self::TESTCASE_DIRECTORY, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);

foreach ($files as $file) {
unlink($file->getRealPath());
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
38 changes: 38 additions & 0 deletions tests/Unit/Layout/LayoutsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tests\Unit\Layout;

use PHPUnit\Framework\Attributes\DataProvider;
use SimonHamp\TheOg\BorderPosition;
use SimonHamp\TheOg\Layout\Layouts\GitHubBasic;
use SimonHamp\TheOg\Layout\Layouts\Standard;
use Tests\Unit\UnitTestcase;

class LayoutsTest extends UnitTestCase
{
#[DataProvider('provideLayouts')]
public function test_layouts(
string $layoutClass,
BorderPosition $borderPosition,
int $borderWidth,
int $boxWidth,
int $boxHeight,
int $mountX,
int $mountY
): void {
$layout = new $layoutClass();

self::assertEquals($borderPosition, $layout->getBorderPosition());
self::assertEquals($borderWidth, $layout->getBorderWidth());
self::assertEquals($boxWidth, $layout->mountArea()->box->width());
self::assertEquals($boxHeight, $layout->mountArea()->box->height());
self::assertEquals($mountX, $layout->mountArea()->position->x());
self::assertEquals($mountY, $layout->mountArea()->position->y());
}

public static function provideLayouts(): iterable
{
yield 'standard' => [Standard::class, BorderPosition::All, 25, 1070, 500, 65, 65];
yield 'github basic' => [GitHubBasic::class, BorderPosition::Bottom, 25, 1190, 550, 45, 45];
}
}
10 changes: 10 additions & 0 deletions tests/Unit/UnitTestcase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class UnitTestcase extends TestCase
{
//
}
52 changes: 0 additions & 52 deletions tests/test.php

This file was deleted.

Binary file removed tests/test4.png
Binary file not shown.
Binary file removed tests/test5.png
Binary file not shown.