Skip to content

Commit

Permalink
fix phpunit deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
ragboyjr committed Nov 2, 2024
1 parent 24f7fa8 commit 5c363c3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ jobs:
strategy:
matrix:
php-versions: ["8.2", "8.3", "8.4"]
phpunit-versions: ["11.4"]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -19,8 +18,6 @@ jobs:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, intl
ini-values: post_max_size=256M, max_execution_time=180
coverage: xdebug
tools: php-cs-fixer, phpunit:${{ matrix.phpunit-versions }}
env:
GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build
/doc/.hugo_build.lock
.generated
.phpunit.result.cache
.phpunit.cache
50 changes: 16 additions & 34 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,36 +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/9.4/phpunit.xsd"
backupGlobals="false"
colors="true"
verbose="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
>
<testsuites>
<testsuite name="Plates tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>src</directory>
</include>
</coverage>

<logging>
<junit outputFile="build/report.junit.xml"/>
<testdoxHtml outputFile="build/coverage"/>
<testdoxText outputFile="build/coverage.txt"/>
<testdoxXml outputFile="build/logs/clover.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" failOnRisky="true" failOnWarning="true" stopOnFailure="false" executionOrder="random" resolveDependencies="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Plates tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
<testdoxHtml outputFile="build/coverage"/>
<testdoxText outputFile="build/coverage.txt"/>
</logging>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
59 changes: 36 additions & 23 deletions tests/Template/ThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use League\Plates\Engine;
use League\Plates\Template\Theme;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\{TestCase, Attributes\Test};

final class ThemeTest extends TestCase
{
Expand All @@ -16,16 +16,18 @@ final class ThemeTest extends TestCase
/** @var \Throwable */
private $exception;

/** @test */
public function engine_renders_with_single_themes() {
#[Test]
public function engine_renders_with_single_themes()
{
$this->given_a_directory_structure_is_setup_like('templates', ['main.php' => '<html></html>']);
$this->given_an_engine_is_created_with_theme(Theme::new($this->vfsPath('templates')));
$this->when_the_engine_renders('main');
$this->then_the_rendered_template_matches('<html></html>');
}

/** @test */
public function engine_renders_with_theme_hierarchy() {
#[Test]
public function engine_renders_with_theme_hierarchy()
{
$this->given_a_directory_structure_is_setup_like('templates', [
'parent' => [
'main.php' => '<?php $this->layout("layout") ?>parent',
Expand All @@ -43,9 +45,10 @@ public function engine_renders_with_theme_hierarchy() {
$this->then_the_rendered_template_matches('<html>child: parent</html>');
}

/** @test */
public function duplicate_theme_names_in_hierarchies_are_not_allowed() {
$this->when_a_theme_is_created_like(function() {
#[Test]
public function duplicate_theme_names_in_hierarchies_are_not_allowed()
{
$this->when_a_theme_is_created_like(function () {
Theme::hierarchy([
Theme::new('templates/a'),
Theme::new('templates/b'),
Expand All @@ -54,26 +57,29 @@ public function duplicate_theme_names_in_hierarchies_are_not_allowed() {
$this->then_an_exception_is_thrown_with_message('Duplicate theme names in hierarchies are not allowed. Received theme names: [Default, Default].');
}

/** @test */
public function nested_hierarchies_are_not_allowed() {
$this->when_a_theme_is_created_like(function() {
#[Test]
public function nested_hierarchies_are_not_allowed()
{
$this->when_a_theme_is_created_like(function () {
Theme::hierarchy([
Theme::hierarchy([Theme::new('templates', 'A'), Theme::new('templates', 'B')])
]);
});
$this->then_an_exception_is_thrown_with_message('Nested theme hierarchies are not allowed, make sure to use Theme::new when creating themes in your hierarchy. Theme B is already in a hierarchy.');
}

/** @test */
public function empty_hierarchies_are_not_allowed() {
$this->when_a_theme_is_created_like(function() {
#[Test]
public function empty_hierarchies_are_not_allowed()
{
$this->when_a_theme_is_created_like(function () {
Theme::hierarchy([]);
});
$this->then_an_exception_is_thrown_with_message('Empty theme hierarchies are not allowed.');
}

/** @test */
public function template_not_found_errors_reference_themes_checked() {
#[Test]
public function template_not_found_errors_reference_themes_checked()
{
$this->given_a_directory_structure_is_setup_like('templates', []);
$this->given_an_engine_is_created_with_theme(Theme::hierarchy([
Theme::new($this->vfsPath('templates/one'), 'One'),
Expand All @@ -83,44 +89,51 @@ public function template_not_found_errors_reference_themes_checked() {
$this->then_an_exception_is_thrown_with_message('The template "main" was not found in the following themes: Two:vfs://templates/two/main.php, One:vfs://templates/one/main.php');
}

private function given_a_directory_structure_is_setup_like(string $rootDir, array $directoryStructure) {
private function given_a_directory_structure_is_setup_like(string $rootDir, array $directoryStructure)
{
vfsStream::setup($rootDir);
vfsStream::create($directoryStructure);
}

private function given_an_engine_is_created_with_theme(Theme $theme) {
private function given_an_engine_is_created_with_theme(Theme $theme)
{
$this->engine = \League\Plates\Engine::fromTheme($theme);
}

private function when_a_theme_is_created_like(callable $fn) {
private function when_a_theme_is_created_like(callable $fn)
{
try {
$fn();
} catch (\Throwable $e) {
$this->exception = $e;
}
}

private function vfsPath(string $path): string {
private function vfsPath(string $path): string
{
return vfsStream::url($path);
}

private function when_the_engine_renders(string $templateName, array $data = []) {
private function when_the_engine_renders(string $templateName, array $data = [])
{
try {
$this->result = $this->engine->render($templateName, $data);
} catch (\Throwable $e) {
$this->exception = $e;
}
}

private function then_the_rendered_template_matches(string $expected) {
private function then_the_rendered_template_matches(string $expected)
{
if ($this->exception) {
throw $this->exception;
}

$this->assertEquals($expected, $this->result);
}

private function then_an_exception_is_thrown_with_message(string $expectedMessage) {
private function then_an_exception_is_thrown_with_message(string $expectedMessage)
{
$this->assertNotNull($this->exception, 'Expected an exception to be thrown with message: ' . $expectedMessage);
$this->assertEquals($expectedMessage, $this->exception->getMessage(), 'Expected an exception to be thrown with message: ' . $expectedMessage);
}
Expand Down

0 comments on commit 5c363c3

Please sign in to comment.