Skip to content

Commit

Permalink
Merge pull request #24 from hokoo/local-wp-tests
Browse files Browse the repository at this point in the history
Dev local tests
  • Loading branch information
hokoo authored Oct 20, 2024
2 parents 2965023 + 6ca06e7 commit 7ad6b9f
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/wp-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PHP WP Unit Tests

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

permissions: {}

Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/.idea

/vendor
/wordpress-develop
.phpunit.result.cache
.phpcs.cache
.phpcs.cache
18 changes: 18 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests.init:
cd ./tests/dev/ && bash ./init.sh

tests.docker.up:
cd ./tests/dev/ && docker-compose -p wpc-tests up -d

tests.docker.down:
cd ./tests/dev/ && docker-compose -p wpc-tests down

tests.docker.build:
cd ./tests/dev/ && docker-compose -p wpc-tests up -d --build php

tests.docker.connect:
cd ./tests/dev/ && docker-compose -p wpc-tests exec php bash

tests.run:
cd ./tests/dev/ && \
docker-compose -p wpc-tests exec php sh -c 'vendor/bin/phpunit -c phpunit.xml && vendor/bin/phpunit -c php-wp-unit.xml'
2 changes: 2 additions & 0 deletions php-wp-unit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
>
<php>
<const name="DOING_TESTS" value="1" />
<const name="CLIENT_NAME" value="client-test" />
<const name="RELATION_NAME" value="relation-test" />
</php>
<testsuites>
<testsuite name="wpConnections creating for WordPress test suite">
Expand Down
5 changes: 5 additions & 0 deletions src/Helpers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

final class Database
{
public static function normalize_table_name($name): string
{
return str_replace('-', '_', sanitize_title($name));
}

public static function register_table($key, $name = false)
{
global $wpdb;
Expand Down
28 changes: 21 additions & 7 deletions src/WPStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@

use iTRON\wpConnections\Exceptions\ConnectionWrongData;
use iTRON\wpConnections\Helpers\Database;
use iTRON\wpConnections\Query;
use iTRON\wpConnections\Query\MetaCollection;

class WPStorage extends Abstracts\Storage
{
use ClientInterface;

private string $connections_table = 'post_connections_';
private string $meta_table = 'post_connections_meta_';
public const CONNECTIONS_TABLE_PREFIX = 'post_connections_';
public const META_TABLE_PREFIX = 'post_connections_meta_';

private string $connections_table;
private string $meta_table;

/**
* @param Client $client wpConnections Client
*/
public function __construct(Client $client)
{
$this->client = $client;
$postfix = str_replace('-', '_', sanitize_title($client->getName()));
$this->connections_table = $this->connections_table . $postfix;
$this->meta_table = $this->meta_table . $postfix;
$postfix = Database::normalize_table_name($client->getName());
$this->connections_table = self::CONNECTIONS_TABLE_PREFIX . $postfix;
$this->meta_table = self::META_TABLE_PREFIX . $postfix;

$this->init();
}

public function get_connections_table(): string
{
return $this->connections_table;
Expand All @@ -41,6 +42,14 @@ private function init()
{
Database::register_table($this->get_connections_table());
Database::register_table($this->get_meta_table());

$install_on_init = apply_filters('wpConnections/storage/installOnInit', false, $this->client);

if (true !== $install_on_init) {
return;
}

$this->install();
}

private function install()
Expand Down Expand Up @@ -340,7 +349,12 @@ public function createConnection(Query\Connection $connectionQuery): int

$attempt = 0;
do {
// Suppress errors when table does not exist.
do_action('iTRON/wpConnections/storage/createConnection/attempt', $attempt);
$suppress = $wpdb->suppress_errors();
$result = $wpdb->insert($wpdb->prefix . $this->get_connections_table(), $data);
$wpdb->suppress_errors($suppress);
do_action('iTRON/wpConnections/storage/createConnection/attempt/result', $result, $wpdb->last_error);

if (false === $result && 0 === $attempt) {
// Try to create tables
Expand Down
12 changes: 12 additions & 0 deletions tests/dev/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PROJECT_NAME=WPC_TESTS
DB_NAME=wpc-tests
DB_USER=wordpress
DB_PASSWORD=wordpress
DB_ROOT_PASSWORD=wordpress
DB_HOST=mysql

PHP_VERSION=8.2
XDEBUG_PORT=9020
XDEBUG_MODE=debug
XDEBUG_IDE_KEY=PHPSTORM
EXTENSIONS_DISABLE=""
12 changes: 12 additions & 0 deletions tests/dev/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PROJECT_NAME=WPC_TESTS
DB_NAME=wpc-tests
DB_USER=wordpress
DB_PASSWORD=wordpress
DB_ROOT_PASSWORD=wordpress
DB_HOST=mysql

PHP_VERSION=8.2
XDEBUG_PORT=9020
XDEBUG_MODE=debug
XDEBUG_IDE_KEY=PHPSTORM
EXTENSIONS_DISABLE=""
50 changes: 50 additions & 0 deletions tests/dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: "3"

services:
mysql:
container_name: "${PROJECT_NAME}_mysql"
image: mysql:5.7
stop_grace_period: 30s
volumes:
- ~/mysql-data/iTRON/wpConnections:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
ports:
- "3066:3306"
networks:
- itron-network

php:
build:
context: ./php
args:
PHP_VER: $PHP_VERSION
container_name: "${PROJECT_NAME}_php"
working_dir: /srv/web/
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: $DB_HOST
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_ROOT_PASSWORD: $DB_ROOT_PASSWORD
DB_NAME: $DB_NAME
PHP_FPM_USER: wodby
PHP_FPM_GROUP: wodby
PHP_XDEBUG: 1
PHP_XDEBUG_DEFAULT_ENABLE: 1
PHP_XDEBUG_CLIENT_PORT: $XDEBUG_PORT
PHP_XDEBUG_MODE: $XDEBUG_MODE
PHP_XDEBUG_IDEKEY: $XDEBUG_IDE_KEY
PHP_EXTENSIONS_DISABLE: $EXTENSIONS_DISABLE
volumes:
- ../../:/srv/web/
- ./php.ini:/usr/local/etc/php/conf.d/php-sp-overrides.ini
networks:
- itron-network

networks:
itron-network:
driver: bridge
24 changes: 24 additions & 0 deletions tests/dev/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# if .env file does not exist, copy the template
[ -f ./.env ] || cp ./dev/.env.template ./.env

# Read the .env file
source ./.env

WP_DEV_DIR_NAME=wordpress-develop
WP_DEV_DIR=../../${WP_DEV_DIR_NAME}

# if wordpress-develop directory does not exist, clone the repository
[ -d $WP_DEV_DIR ] || git clone https://github.com/WordPress/wordpress-develop $WP_DEV_DIR

if [ ! -f $WP_DEV_DIR/wp-tests-config.php ]; then
# copy the sample file
cp "$WP_DEV_DIR"/wp-tests-config-sample.php $WP_DEV_DIR/wp-tests-config.php

# remove all forward slashes in the end
sed -i "s/youremptytestdbnamehere/$DB_NAME/" $WP_DEV_DIR/wp-tests-config.php
sed -i "s/yourusernamehere/$DB_USER/" $WP_DEV_DIR/wp-tests-config.php
sed -i "s/yourpasswordhere/$DB_PASSWORD/" $WP_DEV_DIR/wp-tests-config.php
sed -i "s|localhost|${DB_HOST}|" $WP_DEV_DIR/wp-tests-config.php
fi
14 changes: 14 additions & 0 deletions tests/dev/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error_log = /var/log/php/error.log
display_errors = On
display_startup_errors = On
log_errors = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
html_errors = On

xdebug.log = /var/log/php/xdebug.log
xdebug.client_host = host.docker.internal
xdebug.start_with_request = yes

opcache.jit=0
opcache.jit_buffer_size=0
opcache.jit_debug=0
14 changes: 14 additions & 0 deletions tests/dev/php/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ARG PHP_VER

FROM wodby/wordpress-php:$PHP_VER

LABEL maintainer="Igor Tron <[email protected]>"

USER root

RUN mkdir -p /var/log/php && chown -R wodby:wodby /var/log/php

RUN echo 'alias ll="ls -la"' >> /etc/bash.bashrc
RUN echo 'alias ll="ls -la"' >> /home/wodby/.bashrc

USER wodby
35 changes: 35 additions & 0 deletions tests/drop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
// Drop the database tables.
// Remove tables created by WPC Client.
global $wpdb;

use iTRON\wpConnections\Helpers\Database;
use iTRON\wpConnections\WPStorage;

$client_name = $argv[1];

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../wordpress-develop/wp-tests-config.php';
require_once ABSPATH . 'wp-settings.php';
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
require_once ABSPATH . 'wp-includes/class-wpdb.php';



$wpdb->select( DB_NAME, $wpdb->dbh );

$q = "DROP TABLE IF EXISTS " . $wpdb->prefix .
WPStorage::CONNECTIONS_TABLE_PREFIX .
Database::normalize_table_name( $client_name );

$result = $wpdb->query( $q );

echo PHP_EOL . "QUERY: " . $q . PHP_EOL;
echo "Dropped tables: ";
var_dump( $result );
echo PHP_EOL;

// Close transaction.
$wpdb->query( 'COMMIT;' );

// exit;
17 changes: 6 additions & 11 deletions tests/iTRON/wpConnections/WP/WPUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@

class WPUnitTest extends TestCase
{
const CLIENT_NAME = 'client-test';
const RELATION_NAME = 'relation-test';

protected Client $client;
protected int $post_id;
protected int $page_id;


protected function setUp(): void
{
// Create client
$this->client = new Client( self::CLIENT_NAME );
$this->client = new Client( CLIENT_NAME );
$relation = new Relation();
$relation->set( 'name', self::RELATION_NAME );
$relation->set( 'name', RELATION_NAME );
$relation->set( 'from', 'page' );
$relation->set( 'to', 'post' );
$relation->set( 'cardinality', 'm-m' );
Expand All @@ -47,6 +43,7 @@ protected function setUp(): void
self::assertIsInt( $this->page_id );

// Echo the post and page IDs
echo PHP_EOL;
echo 'Post ID: ' . $this->post_id . PHP_EOL;
echo 'Page ID: ' . $this->page_id . PHP_EOL;
}
Expand All @@ -58,20 +55,18 @@ public function testCreateConnection()
$this->post_id
);

$created_connection = $this->client->getRelation( self::RELATION_NAME )->createConnection( $connection_query );

var_dump( $created_connection );
$created_connection = $this->client->getRelation( RELATION_NAME )->createConnection( $connection_query );

self::assertEquals( $this->page_id, $created_connection->from );
self::assertEquals( $this->post_id, $created_connection->to );

self::assertTrue(
$this->client->getRelation( self::RELATION_NAME )->hasConnectionID(
$this->client->getRelation( RELATION_NAME )->hasConnectionID(
$created_connection->id
)
);

$connection = $this->client->getRelation( self::RELATION_NAME )->findConnections( $connection_query )->first();
$connection = $this->client->getRelation( RELATION_NAME )->findConnections( $connection_query )->first();

self::assertEquals( $connection_query->id, $connection->id );
self::assertEquals( $connection_query->from, $connection->from );
Expand Down
35 changes: 34 additions & 1 deletion tests/wp_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
<?php

// Define constants for the IDE. Never fired.
if ( defined( 'PHPSTORM_META' ) ) {
define( 'DOING_TESTS', 1 );
define( 'CLIENT_NAME', 'client-test' );
define( 'RELATION_NAME', 'relation-test' );
}

use iTRON\wpConnections\Helpers\Database;
use iTRON\wpConnections\WPStorage;

$test_root = getenv( 'WP_TESTS_DIR' ) ? : dirname( __FILE__ ) . '/../wordpress-develop/tests/phpunit';
require_once $test_root . '/includes/functions.php';

tests_add_filter( 'muplugins_loaded', function() {
// Remove tables created by WPC Client.
global $wpdb;

$q = "DROP TABLE IF EXISTS " . $wpdb->prefix .
WPStorage::CONNECTIONS_TABLE_PREFIX .
Database::normalize_table_name( CLIENT_NAME );

$wpdb->query( $q );

$q = "DROP TABLE IF EXISTS " . $wpdb->prefix .
WPStorage::META_TABLE_PREFIX .
Database::normalize_table_name( CLIENT_NAME );

$wpdb->query( $q );
} );

tests_add_filter( 'wpConnections/storage/installOnInit', function ( $installOnInit ) {
return true;
}, 10, 1 );

require $test_root . '/includes/bootstrap.php';
require_once $test_root . '/includes/bootstrap.php';

0 comments on commit 7ad6b9f

Please sign in to comment.