Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Jul 31, 2023
1 parent 1254c2f commit ab50a1d
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 177 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.gitattributes export-ignore
/.github export-ignore
125 changes: 125 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
name: Timber starter theme tests

on:
push:
branches:
- '*'
pull_request:
types:
- opened
- synchronize
- ready_for_review

# Cancel previous workflow run groups that have not completed.
concurrency:
# Group workflow runs by workflow name, along with the head branch ref of the pull request
# or otherwise the branch or tag ref.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
phpunit:
runs-on: ubuntu-latest

services:
mysql:
image: mariadb:latest
env:
MYSQL_USER: root
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_ROOT_PASSWORD: ''
MYSQL_DATABASE: wordpress_test
ports:
- 3306
options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3

continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
wp: ['latest']
multisite: ['0', '1']
dependency-version: ['highest', 'lowest']
webp: [false]
coverage: [false]
extensions: ['gd']
experimental: [false]
include:
# WP Trunk
- php: '7.4'
wp: 'trunk'
dependency-version: 'highest'
multisite: '0'
experimental: true
# PHP 8.0
- php: '8.0'
wp: 'latest'
dependency-version: 'prefer-stable'
multisite: '0'
experimental: false
# PHP 8.1
- php: '8.1'
wp: 'latest'
dependency-version: 'prefer-stable'
multisite: '0'
experimental: false
# PHP 8.1 / experimental
- php: '8.1'
wp: 'trunk'
dependency-version: 'highest'
multisite: '0'
experimental: true
# PHP 8.2 / experimental
- php: '8.2'
wp: 'trunk'
dependency-version: 'highest'
multisite: '0'
experimental: true

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: ${{ matrix.coverage && 'pcov' || 'none' }}
tools: composer:v2
extensions: curl, date, dom, iconv, json, libxml, mysql, ${{ matrix.extensions }}
ini-values: pcov.directory=src

- name: Shutdown default MySQL service
run: sudo service mysql stop

- name: Verify MariaDB connection
run: |
while ! mysqladmin ping -h"127.0.0.1" -P"${{ job.services.mysql.ports[3306] }}" --silent; do
sleep 1
done
- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency-version }}

- name: Install tests
run: bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1:${{ job.services.mysql.ports['3306'] }} ${{ matrix.wp }} true

# - name: Setup PCOV
# if: ${{ matrix.coverage == true }}
# run: |
# composer require --dev --ignore-platform-reqs pcov/clobber
# vendor/bin/pcov clobber

- name: Run tests
if: ${{ !matrix.webp }}
run: composer run test:no-cov
env:
WP_MULTISITE: ${{ matrix.multisite }}
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "Starter theme to build a Timber theme",
"type":"wordpress-theme",
"license": "MIT",
"minimum-stability" : "dev",
"authors": [
{
"email": "jared@upstatement.com",
Expand All @@ -20,9 +19,8 @@
"timber/timber": "2.x-dev"
},
"require-dev": {
"phpunit/phpunit": "7.*",
"roots/wordpress": "^5.4",
"automattic/wordbless": "^0.1.0"
"automattic/wordbless": "^0.4.2",
"yoast/wp-test-utils": "^1.0"
},
"extra": {
"installer-paths": {
Expand All @@ -31,5 +29,11 @@
]
},
"wordpress-install-dir": "wordpress"
},
"config": {
"allow-plugins": {
"roots/wordpress-core-installer": true,
"composer/installers": true
}
}
}
158 changes: 4 additions & 154 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,166 +2,16 @@
/**
* Timber starter-theme
* https://github.com/timber/starter-theme
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/

/**
* If you are installing Timber as a Composer dependency in your theme, you'll need this block
* to load your dependencies and initialize Timber. If you are using Timber via the WordPress.org
* plug-in, you can safely delete this block.
*/
$composer_autoload = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $composer_autoload ) ) {
require_once $composer_autoload;
Timber\Timber::init();
}
// Load Composer dependencies.
require_once __DIR__ . '/vendor/autoload.php';

/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
if ( ! class_exists( 'Timber' ) ) {

add_action(
'admin_notices',
function() {
echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
}
);

add_filter(
'template_include',
function( $template ) {
return get_stylesheet_directory() . '/static/no-timber.html';
}
);
return;
}
Timber\Timber::init();

/**
* Sets the directories (inside your theme) to find .twig files
*/
Timber::$dirname = array( '../views' );

/**
* By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
* No prob! Just set this value to true
*/
Timber::$autoescape = false;


/**
* We're going to configure our theme inside of a subclass of Timber\Site
* You can move this to its own file and include here via php's include("MySite.php")
*/
class StarterSite extends Timber\Site {
/** Add timber support. */
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
add_filter( 'timber/context', array( $this, 'add_to_context' ) );
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
add_action( 'init', array( $this, 'register_post_types' ) );
add_action( 'init', array( $this, 'register_taxonomies' ) );
parent::__construct();
}
/** This is where you can register custom post types. */
public function register_post_types() {

}
/** This is where you can register custom taxonomies. */
public function register_taxonomies() {

}

/** This is where you add some context
*
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context( $context ) {
$context['foo'] = 'bar';
$context['stuff'] = 'I am a value set in your functions.php file';
$context['notes'] = 'These values are available everytime you call Timber::context();';
$context['menu'] = Timber::get_menu();
$context['site'] = $this;
return $context;
}

public function theme_supports() {
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );

/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );

/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( 'post-thumbnails' );

/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
array(
'comment-form',
'comment-list',
'gallery',
'caption',
)
);

/*
* Enable support for Post Formats.
*
* See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support(
'post-formats',
array(
'aside',
'image',
'video',
'quote',
'link',
'gallery',
'audio',
)
);

add_theme_support( 'menus' );
}

/** This Would return 'foo bar!'.
*
* @param string $text being 'foo', then returned 'foo bar!'.
*/
public function myfoo( $text ) {
$text .= ' bar!';
return $text;
}

/** This is where you can add your own functions to twig.
*
* @param string $twig get extension.
*/
public function add_to_twig( $twig ) {
$twig->addExtension( new Twig\Extension\StringLoaderExtension() );
$twig->addFilter( new Twig\TwigFilter( 'myfoo', array( $this, 'myfoo' ) ) );
return $twig;
}

}
Timber::$dirname = [ 'templates', 'views' ];

new StarterSite();
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?php

class TestTimberStarterTheme extends \WorDBless\BaseTestCase {
use Timber\Timber;
use WorDBless\BaseTestCase;

function setUp() {
self::_setupStarterTheme();
class TestTimberStarterTheme extends BaseTestCase {

public function set_up() {
switch_theme( basename( dirname( __DIR__ ) ) . '/theme' );
require_once dirname( __DIR__ ) . '/functions.php';

require dirname( __DIR__ ) . '/functions.php';

Timber::$dirname = array_merge( (array) Timber::$dirname, [ '../views' ] );
Timber::$dirname = array_unique( Timber::$dirname );

// WorDBless includes wp-settings.php
do_action( 'after_setup_theme' );

parent::set_up();
}

function tearDown() {
switch_theme('twentythirteen');
function tear_down() {
parent::tear_down();
switch_theme('twentytwenty');
}

function testTimberExists() {
Expand All @@ -36,21 +46,11 @@ function testLoading() {
* Helper test to output current twig version
*/
function testTwigVersion() {
$str = Timber::compile_string("{{ constant('Twig_Environment::VERSION') }}");
//error_log('Twig version = '.$str);
// $version = Timber::compile_string("{{ version }}", [ 'version', Twig\Environment::VERSION ]);
}

function testTwigFilter() {
$str = Timber::compile_string('{{ "foo" | myfoo }}');
$str = Timber::compile_string('{{ "foo"|myfoo }}');
$this->assertEquals('foo bar!', $str);
}

static function _setupStarterTheme(){
$baseName = basename( dirname( __DIR__ ) );
$src = realpath( dirname( dirname( __DIR__ ) ) . '/' . $baseName );
$dest = WP_CONTENT_DIR . '/themes/' . $baseName;
if ( is_dir($src) && ! file_exists($dest) ) {
symlink($src, $dest);
}
}
}
Loading

0 comments on commit ab50a1d

Please sign in to comment.