Skip to content

Instantly share code, notes, and snippets.

@robertito13
Created December 2, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertito13/fd2ca8d31b0ded3fe77f5a1fff70dc7b to your computer and use it in GitHub Desktop.
Save robertito13/fd2ca8d31b0ded3fe77f5a1fff70dc7b to your computer and use it in GitHub Desktop.
Idea de tests para un Theme de WordPress
<?php
require "vendor/autoload.php";
{
"require-dev": {
"phpunit/phpunit": "^7",
"querypath/querypath": "^3.0"
}
}
<?php
use PHPUnit\Framework\TestCase;
final class FaviconTest extends TestCase {
public function testFaviconIncluded() {
$icons = array();
$qp = html5qp( SITE_URL );
$qp->find('link[rel="icon"]')->each( function( $i, $node ) use ( &$icons ) {
$icons[] = parse_url( $node->getAttribute( 'href' ), PHP_URL_PATH );
} );
$this->assertGreaterThan(
0,
count( $icons )
);
return $icons;
}
/**
* @depends testFaviconIncluded
*/
public function testFaviconFilesExists( $icons ) {
foreach( $icons as $file ) {
$this->assertTrue( file_exists( WP_PATH . "/$file" ), $file );
}
}
}
<?php
use PHPUnit\Framework\TestCase;
final class MissingFilesTest extends TestCase {
public function testIndexFunctionsExists() {
$this->assertTrue( file_exists( THEME_PATH . '/functions.php' ) );
}
public function testScreenshotFileExists() {
$result = file_exists( THEME_PATH . '/screenshot.png' );
$result |= file_exists( THEME_PATH . '/screenshot.jpg' );
$result |= file_exists( THEME_PATH . '/screenshot.jpeg' );
$this->assertTrue( (bool)$result );
}
public function testStyleFileExists() {
$this->assertTrue( file_exists( THEME_PATH . '/style.css' ) );
}
}
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false">
<testsuites>
<testsuite name="Wordpress Theme Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<const name="WP_PATH" value="/mnt/c/Websites/anred.dev.cc"/>
<const name="THEME_PATH" value="/mnt/c/Websites/anred.dev.cc/wp-content/themes/anred/build"/>
<const name="SITE_URL" value="http://anred.dev.cc"/>
<const name="SKIP_REMOTE_IMPORT" value="true"/>
<const name="SKIP_CSS_IMAGE_SIZE" value="true"/>
</php>
</phpunit>
<?php
use PHPUnit\Framework\TestCase;
final class StylesheetTest extends TestCase {
protected $required;
public function setUp() {
$this->required = [
'.alignleft',
'.aligncenter',
'.alignright',
'.alignleft',
'.aligncenter',
'.alignright',
'.wp-caption'
];
if ( !SKIP_CSS_IMAGE_SIZE ) {
array_merge(
$this->required,
[
'.size-full',
'.size-large',
'.size-medium',
'.size-thumbnail'
]
);
}
}
public function testStylesheetIncluded() {
$stylesheets = [];
$qp = html5qp( SITE_URL );
$site_host = parse_url( SITE_URL, PHP_URL_HOST );
$qp->find('link[rel="stylesheet"]')->each(
function( $i, $node ) use ( &$stylesheets, $site_host ) {
$parsed_url = parse_url( $node->getAttribute( 'href' ) );
if ( $parsed_url['host'] == $site_host ) {
$stylesheets[] = $parsed_url['path'];
}
}
);
$this->assertGreaterThan(
0,
count( $stylesheets )
);
return $stylesheets;
}
/**
* @depends testStylesheetIncluded
*/
public function testStylesheetsFilesExists( $stylesheets ) {
foreach( $stylesheets as $file ) {
$this->assertTrue( file_exists( WP_PATH . "/$file" ), $file );
}
}
/**
* @depends testStylesheetIncluded
*/
public function testImportUsage( $stylesheets ) {
foreach( $stylesheets as $file ) {
$stylesheet = file_get_contents( WP_PATH . "/$file" );
if ( SKIP_REMOTE_IMPORT ) {
$expr = '/@import.*\"(?!http[s]?:\/\/)(.+)\"/i';
} else {
$expr = '/@import.*\"(.+)\"/i';
}
$result = preg_match( $expr, $stylesheet, $matches );
$this->assertEquals(0, $result, $file);
}
}
/**
* @depends MissingFilesTest::testStyleFileExists
*/
public function testChildTheme() {
$stylesheet = file_get_contents( THEME_PATH . '/style.css' );
$result = preg_match( '/\/\*[\s\S]*Template:\s*(\w*)[\s\S]*\*\//im', $stylesheet, $matches );
if ( $result ) {
$path = realpath( WP_PATH . '/wp-content/themes/' . $matches[1] . '/style.css' );
$this->assertTrue( file_exists( $path ) );
return $matches[1];
} else {
$this->markTestSkipped('Theme is not child theme');
}
}
/**
* @depends testChildTheme
* @depends testStylesheetIncluded
*/
public function testParentThemeIncluded( $parent, $stylesheets ) {
$path = "/wp-content/themes/$parent/style.css";
$this->assertTrue( in_array($path, $stylesheets) );
}
/**
* @depends testStylesheetIncluded
*/
public function testRequiredCSSClasses( $stylesheets ) {
$required = $this->required;
foreach( $stylesheets as $file ) {
if ( count( $required ) == 0 ) {
break;
}
$stylesheet = file_get_contents( WP_PATH . "/$file" );
foreach( $required as $key => $name ) {
if ( false !== stripos( $stylesheet, $name ) ) {
unset( $required[$key] );
}
}
}
$this->assertEquals( 0, count( $required ), implode( $required, PHP_EOL) );
}
}
@robertito13
Copy link
Author

Ideas para algunos tests para un theme de WordPress sin utilizar la suite de Automatic Testing oficial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment