Skip to content
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

feat: add unit test framework #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions .github/workflows/pre-merge-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# Workflow for running smoke tests for pull requests etc.

name: Pre merge tests

on:
pull_request:
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: '16'

- name: Setup PHP with composer v2
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: composer:v2

- name: Install composer dependencies.
run: composer install

- name: Run unit tests
run: composer test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ dist
# track these files, if they exist
!.gitignore
!.editorconfig
!.github

# PHPUnit
.phpunit.result.cache
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Built With](#built-with)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Tests](#tests)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -65,13 +66,31 @@ nano setup.json
```sh
php setup.php
```
4. Install and build NPM packages
4. Install composer packages
```sh
composer install
```
5. Install and build NPM packages
```sh
npm install && npm run build
```
5. Install composer packages
6. Build assets
```sh
composer install
npm run build
```
### Tests

1. Install composer packages
```sh
composer insall
```
2. Run unit tests
```
composer test
```
3. For code coverage
```
composer coverage
```

## Roadmap
Expand Down
10 changes: 7 additions & 3 deletions build.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

// Any command needed to run and build plugin assets when newly cheched out of repo.
$buildCommands = [
'npm install --no-progress',
'npm ci --no-progress',
'npx browserslist@latest --update-db',
'npm run build',
'composer install --prefer-dist --no-progress'
'composer install --prefer-dist --no-progress --no-dev',
'composer dump-autoload --no-dev --classmap-authoritative'
];

// Files and directories not suitable for prod to be removed.
Expand All @@ -24,7 +25,10 @@
'webpack.config.js',
'node_modules',
'package-lock.json',
'package.json'
'package.json',
'patchwork.json',
'phpunit.xml',
'source/tests'
];

$dirName = basename(dirname(__FILE__));
Expand Down
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
"description": "{{BPREPLACEDESCRIPTION}}",
"type": "wordpress-plugin",
"license": "MIT",
"scripts": {
"test": "./vendor/bin/phpunit --testdox",
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --testdox",
"minimal": "./vendor/bin/phpunit"
},
"authors": [
{
"name": "{{BPREPLACEAUTHOR}}",
"email": "{{BPREPLACEAUTHOREMAIL}}"
}
],
"autoload": {
"psr-4": {"{{BPREPLACENAMESPACE}}\\": "source/php/"}
},
"minimum-stability": "stable",
"require": {},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/{{BPREPLACEGITHUB}}/modularity-{{BPREPLACESLUG}}.git"
}
]
"require-dev": {
"brain/monkey": "^2.6",
"codedungeon/phpunit-result-printer": "^0.31.0",
"phpunit/phpunit": "^9.5"
}
}
10 changes: 3 additions & 7 deletions modularity-boilerplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
define('{{BPREPLACECAPSCONSTANT}}_URL', plugins_url('', __FILE__));
define('{{BPREPLACECAPSCONSTANT}}_TEMPLATE_PATH', {{BPREPLACECAPSCONSTANT}}_PATH . 'templates/');
define('{{BPREPLACECAPSCONSTANT}}_VIEW_PATH', {{BPREPLACECAPSCONSTANT}}_PATH . 'views/');
define('{{BPREPLACECAPSCONSTANT}}_MODULE_VIEW_PATH', plugin_dir_path(__FILE__) . 'source/php/Module/views');
define('{{BPREPLACECAPSCONSTANT}}_MODULE_VIEW_PATH', {{BPREPLACECAPSCONSTANT}}_PATH . 'source/php/Module/views');
define('{{BPREPLACECAPSCONSTANT}}_MODULE_PATH', {{BPREPLACECAPSCONSTANT}}_PATH . 'source/php/Module/');

load_plugin_textdomain('modularity-{{BPREPLACESLUG}}', false, plugin_basename(dirname(__FILE__)) . '/languages');

require_once {{BPREPLACECAPSCONSTANT}}_PATH . 'source/php/Vendor/Psr4ClassLoader.php';
require_once {{BPREPLACECAPSCONSTANT}}_PATH . 'Public.php';

// Instantiate and register the autoloader
$loader = new {{BPREPLACENAMESPACE}}\Vendor\Psr4ClassLoader();
$loader->addPrefix('{{BPREPLACENAMESPACE}}', {{BPREPLACECAPSCONSTANT}}_PATH);
$loader->addPrefix('{{BPREPLACENAMESPACE}}', {{BPREPLACECAPSCONSTANT}}_PATH . 'source/php/');
$loader->register();
// Register the autoloader
require __DIR__ . '/vendor/autoload.php';

// Acf auto import and export
$acfExportManager = new \AcfExportManager\AcfExportManager();
Expand Down
7 changes: 7 additions & 0 deletions patchwork.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"redefinable-internals": [
"file_exists",
"file_get_contents",
"function_exists"
]
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="./source/tests/includes/bootstrap.php"
backupGlobals="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer">

<coverage processUncoveredFiles="false">
<include>
<directory suffix=".php">./source/php</directory>
</include>
<report>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>

<testsuites>
<testsuite name="Plugin Test Suite">
<directory suffix="Test.php">./source/tests/php</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion setup.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"capsConstant": "MODULARITY_BOILERPLATE",
"author": "Firstname Lastname @ Company",
"authoremail": "[email protected]",
"github": "githubname"
"github": "helsingborg-stad"
}
31 changes: 15 additions & 16 deletions source/php/Helper/CacheBust.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ class CacheBust
/**
* Returns the revved/cache-busted file name of an asset.
* @param string $name Asset name (array key) from rev-mainfest.json
* @param boolean $returnName Returns $name if set to true while in dev mode
* @return string filename of the asset (including directory above)
*/
public static function name($name, $returnName = true)
public function name($name)
{
$revManifest = self::getRevManifest();
$jsonPath = {{BPREPLACECAPSCONSTANT}}_PATH . apply_filters(
'{{BPREPLACENAMESPACE}}/Helper/CacheBust/RevManifestPath',
'dist/manifest.json'
);

$revManifest = [];
if (file_exists($jsonPath)) {
$revManifest = json_decode(file_get_contents($jsonPath), true);
} elseif ($this->isDebug()) {
echo '<div style="color:red">Error: Assets not built. Go to ' . {{BPREPLACECAPSCONSTANT}}_PATH . ' and run gulp. See ' . {{BPREPLACECAPSCONSTANT}}_PATH . 'README.md for more info.</div>';
}

if (!isset($revManifest[$name])) {
return;
Expand All @@ -22,20 +31,10 @@ public static function name($name, $returnName = true)
}

/**
* Decode assets json to array
* @return array containg assets filenames
* Check if debug mode, Remove constant dependency in tests.
*/
public static function getRevManifest()
public function isDebug()
{
$jsonPath = {{BPREPLACECAPSCONSTANT}}_PATH . apply_filters(
'{{BPREPLACENAMESPACE}}/Helper/CacheBust/RevManifestPath',
'dist/manifest.json'
);

if (file_exists($jsonPath)) {
return json_decode(file_get_contents($jsonPath), true);
} elseif (WP_DEBUG) {
echo '<div style="color:red">Error: Assets not built. Go to ' . {{BPREPLACECAPSCONSTANT}}_PATH . ' and run gulp. See ' . {{BPREPLACECAPSCONSTANT}}_PATH . 'README.md for more info.</div>';
}
return defined('WP_DEBUG') && WP_DEBUG;
}
}
14 changes: 6 additions & 8 deletions source/php/Module/Boilerplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace {{BPREPLACENAMESPACE}}\Module;

use {{BPREPLACENAMESPACE}}\Helper\CacheBust;

/**
* Class {{BPREPLACESLUGCAMELCASE}}
* @package {{BPREPLACESLUGCAMELCASE}}\Module
Expand All @@ -18,6 +16,8 @@ public function init()
$this->nameSingular = __("{{BPREPLACESLUGCAMELCASE}}", 'modularity-{{BPREPLACESLUG}}');
$this->namePlural = __("{{BPREPLACESLUGCAMELCASE}}", 'modularity-{{BPREPLACESLUG}}');
$this->description = __("{{BPREPLACEDESCRIPTION}}", 'modularity-{{BPREPLACESLUG}}');

$this->cacheBust = new \{{BPREPLACENAMESPACE}}\Helper\CacheBust();
}

/**
Expand All @@ -26,12 +26,10 @@ public function init()
*/
public function data(): array
{
$data = array();

//Append field config
$data = array_merge($data, (array) \Modularity\Helper\FormatObject::camelCase(
$data = (array) \Modularity\Helper\FormatObject::camelCase(
get_fields($this->ID)
));
);

//Translations
$data['lang'] = (object) array(
Expand Down Expand Up @@ -62,7 +60,7 @@ public function style()
//Register custom css
wp_register_style(
'modularity-{{BPREPLACESLUG}}',
{{BPREPLACECAPSCONSTANT}}_URL . '/dist/' . CacheBust::name('css/modularity-{{BPREPLACESLUG}}.css'),
{{BPREPLACECAPSCONSTANT}}_URL . '/dist/' . $this->cacheBust->name('css/modularity-{{BPREPLACESLUG}}.css'),
null,
'1.0.0'
);
Expand All @@ -80,7 +78,7 @@ public function script()
//Register custom css
wp_register_script(
'modularity-{{BPREPLACESLUG}}',
{{BPREPLACECAPSCONSTANT}}_URL . '/dist/' . CacheBust::name('js/modularity-{{BPREPLACESLUG}}.js'),
{{BPREPLACECAPSCONSTANT}}_URL . '/dist/' . $this->cacheBust->name('js/modularity-{{BPREPLACESLUG}}.js'),
null,
'1.0.0'
);
Expand Down
Loading