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

[Rock] Add PHP unit tests #1824

Merged
merged 16 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
72 changes: 41 additions & 31 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,18 @@ jobs:
- name: Run PHPCS
run: composer run lint

phpunit:
name: Phpunit
phpstan:
name: PHPStan
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Setup PHP version
uses: shivammathur/setup-php@v1
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: '7.4'
extensions: simplexml, mysql
tools: phpunit:7.5.15
- name: Checkout source code
uses: actions/checkout@v2
- name: Install WordPress Test Suite
run: |
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:${{ job.services.mysql.ports['3306'] }}
- name: Get Composer Cache Directory
id: composer-cache
run: |
Expand All @@ -70,33 +58,55 @@ jobs:
${{ runner.os }}-composer-
- name: Install composer
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run phpunit
run: phpunit
- name: PHPStan Static Analysis
run: composer phpstan

phpstan:
name: PHPStan
unit:
name: UnitTests
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
steps:
- name: Setup PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: simplexml, mysql
- name: Checkout source code
uses: actions/checkout@v2
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Setup Composer cache
uses: actions/cache@v1

# setup the composer cache (vendor) with github actions cache and the cache dir defined in the previous step
- uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-

# run composer install
- name: Install composer
run: composer install --prefer-dist --no-progress --no-suggest
- name: PHPStan Static Analysis
run: composer phpstan

- name: Create License File
run: |
printf "{\"key\":\"${{ secrets.OTTER_PRO_LICENSE }}\"}" > license.json

# setup the node cache (node_modules) with github actions cache
- name: Cache Node - npm
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-cache-

- name: npm ci
run: |
npm ci

- name: Setup WP Env
run: |
npm run test:unit:php:setup

- name: Run PHP Unit Tests
run: |
npm run test:unit:php:base
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ artifacts
*.results.json
trace.json
license.json
.phpunit.result.cache
.fleet
51 changes: 31 additions & 20 deletions inc/plugins/class-block-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,49 @@ public function init() {
*/
public function render_blocks( $block_content, $block ) {
if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && isset( $block['attrs']['otterConditions'] ) ) {
$display = true;
$display = $this->evaluate_condition_collection( $block['attrs']['otterConditions'] );

foreach ( $block['attrs']['otterConditions'] as $group ) {
if ( 0 === count( $group ) ) {
continue;
}
if ( false === $display ) {
return;
}
}

$visibility = true;
return $block_content;
}

foreach ( $group as $condition ) {
if ( ! $this->evaluate_condition( $condition ) ) {
$visibility = false;
}
}
/**
* Evaluate conditions
*
* @param array<array> $collection The conditions collection to evaluate.
* @return bool Whether the conditions are met.
*/
public function evaluate_condition_collection( $collection ) {
$display = true;

if ( true === $visibility ) {
$display = true;
break;
}
foreach ( $collection as $group ) {
if ( 0 === count( $group ) ) {
continue;
}

$visibility = true;

if ( false === $visibility ) {
$display = false;
foreach ( $group as $condition ) {
if ( ! $this->evaluate_condition( $condition ) ) {
$visibility = false;
}
}

if ( false === $display ) {
return;
if ( true === $visibility ) {
$display = true;
break;
}

if ( false === $visibility ) {
$display = false;
}
}

return $block_content;
return $display;
}

/**
Expand Down
43 changes: 41 additions & 2 deletions inc/plugins/class-dynamic-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,49 @@ public function apply_dynamic_content( $content ) {
return $content;
}

$matches = array();
$num = self::parse_dynamic_content_query( $content, $matches );

if ( isset( $num ) && 0 === $num ) {
return $content;
}

foreach ( $matches as $match ) {
$replacement = $this->apply_data( $match );
$string_to_replace = $match[0];
$position = strstr( $content, $string_to_replace, true );

if ( false === $position ) {
continue;
}

$position = strlen( $position );
$content = substr_replace( $content, $replacement, $position, strlen( $string_to_replace ) );
}

return $content;
}

/**
* Get the Dynamic Content regex.
*
* @return string
*/
public static function dynamic_content_regex() {
// Todo: Improve this Regex, it can't go on for like this. Soon it will be longer than the available space in the universe!!!
$re = '/<o-dynamic(?:\s+(?:data-type=["\'](?P<type>[^"\'<>]+)["\']|data-id=["\'](?P<id>[^"\'<>]+)["\']|data-before=["\'](?P<before>[^"\'<>]+)["\']|data-after=["\'](?P<after>[^"\'<>]+)["\']|data-length=["\'](?P<length>[^"\'<>]+)["\']|data-date-type=["\'](?P<dateType>[^"\'<>]+)["\']|data-date-format=["\'](?P<dateFormat>[^"\'<>]+)["\']|data-date-custom=["\'](?P<dateCustom>[^"\'<>]+)["\']|data-time-type=["\'](?P<timeType>[^"\'<>]+)["\']|data-time-format=["\'](?P<timeFormat>[^"\'<>]+)["\']|data-time-custom=["\'](?P<timeCustom>[^"\'<>]+)["\']|data-term-type=["\'](?P<termType>[^"\'<>]+)["\']|data-term-separator=["\'](?P<termSeparator>[^"\'<>]+)["\']|data-meta-key=["\'](?P<metaKey>[^"\'<>]+)["\']|data-parameter=["\'](?P<parameter>[^"\'<>]+)["\']|data-format=["\'](?P<format>[^"\'<>]+)["\']|data-context=["\'](?P<context>[^"\'<>]+)["\']|data-taxonomy=["\'](?P<taxonomy>[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?<default>[^ $].*?)<\s*\/\s*o-dynamic>/';
return '/<o-dynamic(?:\s+(?:data-type=["\'](?P<type>[^"\'<>]+)["\']|data-id=["\'](?P<id>[^"\'<>]+)["\']|data-before=["\'](?P<before>[^"\'<>]+)["\']|data-after=["\'](?P<after>[^"\'<>]+)["\']|data-length=["\'](?P<length>[^"\'<>]+)["\']|data-date-type=["\'](?P<dateType>[^"\'<>]+)["\']|data-date-format=["\'](?P<dateFormat>[^"\'<>]+)["\']|data-date-custom=["\'](?P<dateCustom>[^"\'<>]+)["\']|data-time-type=["\'](?P<timeType>[^"\'<>]+)["\']|data-time-format=["\'](?P<timeFormat>[^"\'<>]+)["\']|data-time-custom=["\'](?P<timeCustom>[^"\'<>]+)["\']|data-term-type=["\'](?P<termType>[^"\'<>]+)["\']|data-term-separator=["\'](?P<termSeparator>[^"\'<>]+)["\']|data-meta-key=["\'](?P<metaKey>[^"\'<>]+)["\']|data-parameter=["\'](?P<parameter>[^"\'<>]+)["\']|data-format=["\'](?P<format>[^"\'<>]+)["\']|data-context=["\'](?P<context>[^"\'<>]+)["\']|data-taxonomy=["\'](?P<taxonomy>[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?<default>[^ $].*?)<\s*\/\s*o-dynamic>/';
}

return preg_replace_callback( $re, array( $this, 'apply_data' ), $content );
/**
* Parse dynamic content query.
*
* @param string $content The content to parse.
* @param array $matches The matches.
* @return mixed
*/
public static function parse_dynamic_content_query( $content, &$matches ) {
$re = self::dynamic_content_regex();
return preg_match_all( $re, $content, $matches, PREG_SET_ORDER, 0 );
}

/**
Expand Down
5 changes: 3 additions & 2 deletions inc/plugins/class-stripe-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ThemeIsle\GutenbergBlocks\Plugins;

use Stripe\Collection;
use Stripe\StripeClient;

/**
Expand Down Expand Up @@ -108,7 +109,7 @@ public function build_error_response( $error ) {
* @param string $path Request path.
* @param array|string $args Request arguments.
*
* @return mixed
* @return mixed|\WP_Error|Collection
* @access public
*/
public function create_request( $path, $args = array() ) {
Expand Down Expand Up @@ -237,7 +238,7 @@ public function save_customer_data( $session_id ) {

array_push( $data, $object );

if ( defined( 'COOKIEPATH' ) && defined( 'COOKIE_DOMAIN' ) && ! $user_id ) {
if ( defined( 'COOKIEPATH' ) && defined( 'COOKIE_DOMAIN' ) && ! headers_sent() && ! $user_id ) {
setcookie( 'o_stripe_data', wp_json_encode( $data ), strtotime( '+1 week' ), COOKIEPATH, COOKIE_DOMAIN, false ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.cookies_setcookie
return;
}
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
"test:e2e:blockId": "wp-scripts test-e2e uniq-id-checking.spec.js",
"test:e2e:playwright": "npx playwright test --config src/blocks/test/e2e/playwright.config.js",
"test:e2e:playwright-ui": "npx playwright test --config src/blocks/test/e2e/playwright.config.js --ui",
"test:unit:php:setup": "wp-env start",
"test:unit:php:setup:debug": "wp-env start --xdebug",
"test:unit:php:base": "wp-env run --env-cwd='wp-content/plugins/otter-blocks' tests-wordpress vendor/bin/phpunit -c phpunit.xml --verbose",
"test:unit:php": "npm-run-all test:unit:php:setup test:unit:php:base",
"test:unit:php:debug": "npm-run-all test:unit:php:setup:debug test:unit:php:base",
"test:unit:php:multisite:base": "wp-env run --env-cwd='wp-content/plugins/otter-blocks' tests-wordpress vendor/bin/phpunit -c phpunit/multisite.xml --verbose",
"test:unit:php:multisite": "npm-run-all test:unit:php:setup test:unit:php:multisite:base",
"test:unit:php:multisite:debug": "npm-run-all test:unit:php:setup:debug test:unit:php:multisite:base",
"test:php": "npm-run-all lint:php test:unit:php",
"test:php:watch": "wp-env run --env-cwd='wp-content/plugins/otter-blocks' tests-cli composer run-script test:watch",
"lint:php": "wp-env run --env-cwd='wp-content/plugins/otter-blocks' cli composer run-script lint",
"format:php": "wp-env run --env-cwd='wp-content/plugins/otter-blocks' cli composer run-script format",
"wp-env": "wp-env"
},
"repository": {
Expand Down
5 changes: 5 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';

require dirname( dirname( __FILE__ ) ) . '/tests/stripe-http-client-mock.php';
require dirname( dirname( __FILE__ ) ) . '/inc/css/class-css-utility.php';
require dirname( dirname( __FILE__ ) ) . '/inc/plugins/class-block-conditions.php';
require dirname( dirname( __FILE__ ) ) . '/inc/plugins/class-dynamic-content.php';
require dirname( dirname( __FILE__ ) ) . '/plugins/otter-pro/inc/plugins/class-block-conditions.php';
require dirname( dirname( __FILE__ ) ) . '/plugins/otter-pro/inc/plugins/class-dynamic-content.php';

global $current_user;
$current_user = new WP_User( 1 );
Expand Down
Loading
Loading