Skip to content

[WIP] Ajout des tests unitaires PHP #215

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

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ composer.json export-ignore
composer.lock export-ignore
grumphp.yml export-ignore
phpcs.xml export-ignore
psalm.xml export-ignore
psalm.xml export-ignore
tests export-ignore
17 changes: 12 additions & 5 deletions .github/workflows/php-quality.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: PHP Quality

on: [push, pull_request]
on: [ pull_request ]

jobs:
run:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-versions: [ 7.4' ]
php-versions: [ '7.4' ]
name: PHP ${{ matrix.php-versions }} Test
steps:
- name: Checkout
Expand All @@ -18,8 +18,6 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, intl #optional, setup extensions

- name: Check composer
run: composer validate

Expand All @@ -32,4 +30,13 @@ jobs:
- name: Check psalm
run: |
mkdir -p dist
composer psalm
composer psalm

- name: Check unit test
run: composer tests-cov -- --coverage-clover=./tests/coverage/xml/index.xml

- name: Run codacy-coverage-reporter
uses: codacy/codacy-coverage-reporter-action@v1
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
coverage-reports: tests/coverage/xml/index.xml
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ package-lock.json
### config
/vendor/
composer.lock

### Tests
.phpunit.result.cache
tests/coverage/
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"phpro/grumphp-shim": "^0.19.1",
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^3.13",
"wp-coding-standards/wpcs": "^2.3"
"wp-coding-standards/wpcs": "^2.3",
"10up/wp_mock": "^0.4.2"
},
"scripts": {
"cs": [
Expand All @@ -20,7 +21,9 @@
],
"psalm": [
"./vendor/bin/psalm"
]
],
"tests": "./vendor/bin/phpunit",
"tests-cov": "phpdbg -qrr ./vendor/bin/phpunit --coverage-html ./tests/coverage/"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 1 addition & 2 deletions inc/Services/Acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function warning(): void {
return;
}

wp_die( sprintf( __( 'This theme can\'t work without ACF plugin. <a href="%s">Please login to admin</a>, and activate it !', 'framework-textdomain' ), esc_url( wp_login_url() ) ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.Security.EscapeOutput.OutputNotEscaped
\wp_die( sprintf( __( 'This theme can\'t work without ACF plugin. <a href="%s">Please login to admin</a>, and activate it !', 'framework-textdomain' ), esc_url( wp_login_url() ) ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
Expand Down Expand Up @@ -147,7 +147,6 @@ public function init_acf(): void {
if ( ! is_file( get_theme_file_path( $this->path . $file . '.php' ) ) ) {
continue;
}

require_once get_theme_file_path( $this->path . $file . '.php' );
}
}
Expand Down
29 changes: 10 additions & 19 deletions inc/Services/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ public function register_assets(): void {

// Js theme
// Theme js dependencies
$scripts_dependencies = [ 'jquery' ];
$scripts_dependencies = [ 'jquery', 'global-polyfill' ];

// Async and footer
$file = $this->is_minified() ? $this->get_min_file( 'js' ) : 'app.js';
// Polyfill
\wp_register_script( 'global-polyfill', 'https://cdn.polyfill.io/v3/polyfill.min.js?features=es5,es6,fetch,Array.prototype.includes,CustomEvent,Element.prototype.closest,NodeList.prototype.forEach', null, null, true ); //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion

// Do not add version if minified
$version = $this->is_minified() ? null : $theme->get( 'Version' );
$this->assets_tools->register_script( 'scripts', 'dist/' . $file, $scripts_dependencies, $version, true );
// Async and footer
$file = ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) ? $this->get_min_file( 'js' ) : 'app.js';
$this->assets_tools->register_script( 'scripts', 'dist/' . $file, $scripts_dependencies, $theme->get( 'Version' ), true );

// CSS
wp_register_style( 'theme-style', get_stylesheet_uri(), [], $version );
\wp_register_style( 'theme-style', \get_stylesheet_uri(), [], $theme->get( 'Version' ) );
}

/**
Expand Down Expand Up @@ -97,7 +97,8 @@ public function enqueue_styles(): void {
* @author Nicolas Juen
*/
public function stylesheet_uri( string $stylesheet_uri ): string {
if ( $this->is_minified() ) {
if ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) {

$file = $this->get_min_file( 'css' );
if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) {
return \get_theme_file_uri( '/dist/' . $file );
Expand Down Expand Up @@ -167,21 +168,11 @@ public function get_min_file( string $type ): string {
return $file;
}

/**
* Check if we are on minified environment.
*
* @return bool
* @author Nicolas JUEN
*/
private function is_minified(): bool {
return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false );
}

/**
* Change login CSS URL
* @return string
*/
public function login_stylesheet_uri(): string {
return $this->is_minified() ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css';
return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css';
}
}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<exclude-pattern>dist/</exclude-pattern>
<exclude-pattern>src/</exclude-pattern>
<exclude-pattern>scripts/</exclude-pattern>
<exclude-pattern>tests/</exclude-pattern>

<!-- <<< EXCLUDE EXTERNAL (MU-)PLUGINS OR THEMES HERE >>> -->

Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit beStrictAboutTestsThatDoNotTestAnything="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="./tests/bootstrap.php" executionOrder="depends,defects" forceCoversAnnotation="false" beStrictAboutCoversAnnotation="false" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" colors="true" verbose="true">
<coverage processUncoveredFiles="true">
<include>
<file>functions.php</file>
<directory suffix=".php">inc</directory>
</include>
</coverage>
<testsuites>
<testsuite name="BFF test suite">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<directory name="src" />
<directory name="dist" />
<directory name="config" />
<directory name="tests" />
</ignoreFiles>
</projectFiles>

Expand Down
37 changes: 37 additions & 0 deletions tests/FrameworkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use BEA\Theme\Framework\Framework;
use BEA\Theme\Framework\Service_Container;
use BEA\Theme\Framework\Services\Acf;
use BEA\Theme\Framework\Services\Theme;
use PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use WP_Mock\Functions;

class FrameworkTest extends \WP_Mock\Tools\TestCase {
public function setUp(): void {
WP_Mock::setUp();
}

public function tearDown(): void {
WP_Mock::tearDown();
}

public function testNotAService() {
Framework::register_service( 'not-a-service' );

$this->assertFalse( Framework::get_container()->get_service( 'not-a-service' ) );
}

public function testSameContainer() {
$container = Framework::get_container();

$this->assertSame( $container, Framework::get_container() );
}

public function testServiceSet() {
Framework::register_service( Acf::class );

$this->assertNotEmpty( Framework::get_container()->get_service( Acf::class ) );
}
}
40 changes: 40 additions & 0 deletions tests/Helpers/Formatting/EscapeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Services;

use BEA\Theme\Framework\Service_Container;
use BEA\Theme\Framework\Services\Svg;
use WP_Mock;
use WP_Mock\Tools\TestCase;
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_attribute_value;
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
use function BEA\Theme\Framework\Helpers\Formatting\Text\get_the_text;
use function BEA\Theme\Framework\Helpers\Formatting\Text\the_text;
use function ob_get_clean;
use function ob_start;

class EscapeTest extends TestCase {
public function setUp(): void {
WP_Mock::setUp();
}

public function tearDown(): void {
WP_Mock::tearDown();
}

public function testAttribute() {
$value = escape_attribute_value( 'ok', '' );
$this->assertSame( 'ok', $value );

$value = escape_attribute_value( 'ok', 'test_escape' );
$this->assertSame( 'esc_ok', $value );
}
public function testContent() {
$value = escape_content_value( 'ok', '' );
$this->assertSame( 'ok', $value );

$value = escape_content_value( 'ok', 'test_escape' );
$this->assertSame( 'esc_ok', $value );
}
}

55 changes: 55 additions & 0 deletions tests/Helpers/Formatting/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Services;

use BEA\Theme\Framework\Service_Container;
use BEA\Theme\Framework\Services\Svg;
use WP_Mock;
use WP_Mock\Tools\TestCase;
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_attribute_value;
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
use function BEA\Theme\Framework\Helpers\Formatting\Image\get_the_image;
use function BEA\Theme\Framework\Helpers\Formatting\Text\get_the_text;
use function BEA\Theme\Framework\Helpers\Formatting\Text\the_text;
use function ob_get_clean;
use function ob_start;

class ImageTest extends TestCase {
public function setUp(): void {
WP_Mock::setUp();
}

public function tearDown(): void {
WP_Mock::tearDown();
}

public function testGetZeroImage() {
WP_Mock::userFunction( 'wp_get_attachment_image', [
'return' => '',
]
);

$this->assertSame( '', get_the_image( 0, [] ) );
}

public function testGetImageEmpty() {
WP_Mock::userFunction( 'wp_get_attachment_image', [
'return' => '',
]
);

$this->assertSame( '', get_the_image( 10, [ 'size' => '', 'data-location' => '' ] ) );
}

public function testImageBeforeAfter() {
WP_Mock::userFunction( 'wp_get_attachment_image', [
'return' => '<img>',
]
);

$this->assertSame( 'b<img>', get_the_image( 10, [], [ 'before' => 'b' ] ) );
$this->assertSame( '<img>a', get_the_image( 10, [], [ 'after' => 'a' ] ) );
$this->assertSame( 'b<img>a', get_the_image( 10, [], [ 'after' => 'a', 'before' => 'b' ] ) );
}
}

Loading