diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..573a151 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index 52c8085..c207a9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store /vendor /composer.lock +/node_modules diff --git a/composer.json b/composer.json index ce679ed..ec3365f 100644 --- a/composer.json +++ b/composer.json @@ -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" + } } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..88f3461 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,33 @@ +{ + "name": "the-og", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "devDependencies": { + "pixelmatch": "^5.3.0" + } + }, + "node_modules/pixelmatch": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/pixelmatch/-/pixelmatch-5.3.0.tgz", + "integrity": "sha512-o8mkY4E/+LNUf6LzX96ht6k6CEDi65k9G2rjMtBe9Oo+VPKSvl+0GKHuH/AlG+GA5LPG/i5hrekkxUc3s2HU+Q==", + "dev": true, + "dependencies": { + "pngjs": "^6.0.0" + }, + "bin": { + "pixelmatch": "bin/pixelmatch" + } + }, + "node_modules/pngjs": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/pngjs/-/pngjs-6.0.0.tgz", + "integrity": "sha512-TRzzuFRRmEoSW/p1KVAmiOgPco2Irlah+bGFCeNfJXxxYGwSw7YwAOAcd7X28K/m5bjBWKsC29KyoMfHbypayg==", + "dev": true, + "engines": { + "node": ">=12.13.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..e0f07ff --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "pixelmatch": "^5.3.0" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..f7f5eab --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,11 @@ + + + + + tests/Integration + + + tests/Unit + + + diff --git a/tests/Integration/ImageTest.php b/tests/Integration/ImageTest.php new file mode 100644 index 0000000..73203c4 --- /dev/null +++ b/tests/Integration/ImageTest.php @@ -0,0 +1,98 @@ +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', + ]; + } +} diff --git a/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set basic with background url__1.png b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set basic with background url__1.png new file mode 100644 index 0000000..0dac904 Binary files /dev/null and b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set basic with background url__1.png differ diff --git a/tests/test1.png b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set basic__1.png similarity index 100% rename from tests/test1.png rename to tests/Integration/__snapshots__/ImageTest__test_basic_image with data set basic__1.png diff --git a/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set different theme with background url__1.png b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set different theme with background url__1.png new file mode 100644 index 0000000..e8d5e70 Binary files /dev/null and b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set different theme with background url__1.png differ diff --git a/tests/test2.png b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set different theme__1.png similarity index 100% rename from tests/test2.png rename to tests/Integration/__snapshots__/ImageTest__test_basic_image with data set different theme__1.png diff --git a/tests/test3.png b/tests/Integration/__snapshots__/ImageTest__test_basic_image with data set override some elements__1.png similarity index 100% rename from tests/test3.png rename to tests/Integration/__snapshots__/ImageTest__test_basic_image with data set override some elements__1.png diff --git a/tests/test.php b/tests/test.php deleted file mode 100644 index c98ec87..0000000 --- a/tests/test.php +++ /dev/null @@ -1,52 +0,0 @@ -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') - ->save(__DIR__.'/test1.png'); - -// 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') - ->save(__DIR__.'/test2.png'); - -// 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) - ->save(__DIR__.'/test3.png'); - -// Basic with BackgroundUrl -(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) - ->save(__DIR__ . '/test4.png'); - -// Different theme with BackgroundUrl -(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) - ->save(__DIR__ . '/test5.png'); diff --git a/tests/test4.png b/tests/test4.png deleted file mode 100644 index 7ed0d47..0000000 Binary files a/tests/test4.png and /dev/null differ diff --git a/tests/test5.png b/tests/test5.png deleted file mode 100644 index 60ab239..0000000 Binary files a/tests/test5.png and /dev/null differ