Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svenluijten committed Jan 6, 2024
1 parent 2221ff4 commit 409fef7
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 53 deletions.
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:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
run-tests:
runs-on: ${{ matrics.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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
/vendor
/composer.lock
/node_modules
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": "[email protected]"
}
]
],
"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>
98 changes: 98 additions & 0 deletions tests/Integration/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Tests\Integration;

use FilesystemIterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SimonHamp\TheOg\Background;
use SimonHamp\TheOg\Image;
use SimonHamp\TheOg\Themes\Themes;
use Spatie\Snapshots\MatchesSnapshots;

class ImageTest extends TestCase
{
use MatchesSnapshots;

private const TESTCASE_DIRECTORY = __DIR__.'/../resources/testcases';

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

$image->save($path);

$this->assertMatchesImageSnapshot($path);
}

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

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

foreach ($files as $file) {
unlink($file->getRealPath());
}
}

public static function snapshotImages()
{
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',
];
}
}
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
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.

0 comments on commit 409fef7

Please sign in to comment.