From 3421a9aaa04ada9dfad6f2be9b3d53c0f6c8cc41 Mon Sep 17 00:00:00 2001 From: Igor Tron Date: Sun, 20 Oct 2024 18:57:15 +0400 Subject: [PATCH 1/4] dev local tests --- .gitignore | 4 +- makefile | 18 ++++++++ php-wp-unit.xml | 2 + src/Helpers/Database.php | 4 ++ src/WPStorage.php | 28 +++++++++--- tests/dev/.env | 12 +++++ tests/dev/.env.template | 12 +++++ tests/dev/docker-compose.yml | 50 +++++++++++++++++++++ tests/dev/init.sh | 24 ++++++++++ tests/dev/php.ini | 14 ++++++ tests/dev/php/dockerfile | 14 ++++++ tests/drop.php | 35 +++++++++++++++ tests/iTRON/wpConnections/WP/WPUnitTest.php | 17 +++---- tests/wp_bootstrap.php | 36 ++++++++++++++- 14 files changed, 248 insertions(+), 22 deletions(-) create mode 100644 makefile create mode 100644 tests/dev/.env create mode 100644 tests/dev/.env.template create mode 100644 tests/dev/docker-compose.yml create mode 100644 tests/dev/init.sh create mode 100644 tests/dev/php.ini create mode 100644 tests/dev/php/dockerfile create mode 100644 tests/drop.php diff --git a/.gitignore b/.gitignore index 955b479..3e78ca6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /.idea - /vendor +/wordpress-develop .phpunit.result.cache -.phpcs.cache \ No newline at end of file +.phpcs.cache diff --git a/makefile b/makefile new file mode 100644 index 0000000..d72f996 --- /dev/null +++ b/makefile @@ -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 php-wp-unit.xml' \ No newline at end of file diff --git a/php-wp-unit.xml b/php-wp-unit.xml index b801dfd..d9c07e2 100644 --- a/php-wp-unit.xml +++ b/php-wp-unit.xml @@ -10,6 +10,8 @@ > + + diff --git a/src/Helpers/Database.php b/src/Helpers/Database.php index 8ee9bd5..c6f5d49 100644 --- a/src/Helpers/Database.php +++ b/src/Helpers/Database.php @@ -4,6 +4,10 @@ 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; diff --git a/src/WPStorage.php b/src/WPStorage.php index ef594e1..a75839d 100644 --- a/src/WPStorage.php +++ b/src/WPStorage.php @@ -4,15 +4,17 @@ 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_'; + const CONNECTIONS_TABLE_PREFIX = 'post_connections_'; + const META_TABLE_PREFIX = 'post_connections_meta_'; + + private string $connections_table; + private string $meta_table; /** * @param Client $client wpConnections Client @@ -20,13 +22,12 @@ class WPStorage extends Abstracts\Storage 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; @@ -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() @@ -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 diff --git a/tests/dev/.env b/tests/dev/.env new file mode 100644 index 0000000..2e69890 --- /dev/null +++ b/tests/dev/.env @@ -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="" diff --git a/tests/dev/.env.template b/tests/dev/.env.template new file mode 100644 index 0000000..2e69890 --- /dev/null +++ b/tests/dev/.env.template @@ -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="" diff --git a/tests/dev/docker-compose.yml b/tests/dev/docker-compose.yml new file mode 100644 index 0000000..ad047b4 --- /dev/null +++ b/tests/dev/docker-compose.yml @@ -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 diff --git a/tests/dev/init.sh b/tests/dev/init.sh new file mode 100644 index 0000000..f0a7c3e --- /dev/null +++ b/tests/dev/init.sh @@ -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 diff --git a/tests/dev/php.ini b/tests/dev/php.ini new file mode 100644 index 0000000..83c7a47 --- /dev/null +++ b/tests/dev/php.ini @@ -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 diff --git a/tests/dev/php/dockerfile b/tests/dev/php/dockerfile new file mode 100644 index 0000000..fabb018 --- /dev/null +++ b/tests/dev/php/dockerfile @@ -0,0 +1,14 @@ +ARG PHP_VER + +FROM wodby/wordpress-php:$PHP_VER + +LABEL maintainer="Igor Tron " + +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 diff --git a/tests/drop.php b/tests/drop.php new file mode 100644 index 0000000..6dcf967 --- /dev/null +++ b/tests/drop.php @@ -0,0 +1,35 @@ +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; diff --git a/tests/iTRON/wpConnections/WP/WPUnitTest.php b/tests/iTRON/wpConnections/WP/WPUnitTest.php index 1b8de13..fa1e194 100644 --- a/tests/iTRON/wpConnections/WP/WPUnitTest.php +++ b/tests/iTRON/wpConnections/WP/WPUnitTest.php @@ -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' ); @@ -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; } @@ -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 ); diff --git a/tests/wp_bootstrap.php b/tests/wp_bootstrap.php index 759e808..b3da035 100644 --- a/tests/wp_bootstrap.php +++ b/tests/wp_bootstrap.php @@ -1,4 +1,36 @@ 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_once dirname( __FILE__ ) . "/../wordpress-develop/tests/phpunit/includes/bootstrap.php"; From 3d0ffffb81ab40de608264c914c81fe95374bc2e Mon Sep 17 00:00:00 2001 From: Igor Tron Date: Sun, 20 Oct 2024 19:01:06 +0400 Subject: [PATCH 2/4] both tests to run --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index d72f996..253ad8f 100644 --- a/makefile +++ b/makefile @@ -15,4 +15,4 @@ tests.docker.connect: tests.run: cd ./tests/dev/ && \ - docker-compose -p wpc-tests exec php sh -c 'vendor/bin/phpunit -c php-wp-unit.xml' \ No newline at end of file + docker-compose -p wpc-tests exec php sh -c 'vendor/bin/phpunit -c phpunit.xml && vendor/bin/phpunit -c php-wp-unit.xml' \ No newline at end of file From 355644aec5105717bf727d3ea8b7c27c3cb59574 Mon Sep 17 00:00:00 2001 From: Igor Tron Date: Sun, 20 Oct 2024 19:09:56 +0400 Subject: [PATCH 3/4] php cs --- src/Helpers/Database.php | 7 ++++--- src/WPStorage.php | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Helpers/Database.php b/src/Helpers/Database.php index c6f5d49..ae8dad9 100644 --- a/src/Helpers/Database.php +++ b/src/Helpers/Database.php @@ -4,9 +4,10 @@ final class Database { - public static function normalize_table_name($name): string { - return str_replace('-', '_', sanitize_title($name)); - } + public static function normalize_table_name($name): string + { + return str_replace('-', '_', sanitize_title($name)); + } public static function register_table($key, $name = false) { diff --git a/src/WPStorage.php b/src/WPStorage.php index a75839d..8d64168 100644 --- a/src/WPStorage.php +++ b/src/WPStorage.php @@ -10,8 +10,8 @@ class WPStorage extends Abstracts\Storage { use ClientInterface; - const CONNECTIONS_TABLE_PREFIX = 'post_connections_'; - const META_TABLE_PREFIX = '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; @@ -43,13 +43,13 @@ 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 ); + $install_on_init = apply_filters('wpConnections/storage/installOnInit', false, $this->client); - if ( true !== $install_on_init ) { - return; - } + if (true !== $install_on_init) { + return; + } - $this->install(); + $this->install(); } private function install() @@ -349,12 +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(); + // 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 ); + $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 From 6ca06e78d6cbdf4347596feebb8a4ea3dd5b93d5 Mon Sep 17 00:00:00 2001 From: Igor Tron Date: Sun, 20 Oct 2024 19:13:23 +0400 Subject: [PATCH 4/4] init tests on GA --- .github/workflows/wp-unit-tests.yml | 2 +- tests/wp_bootstrap.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/wp-unit-tests.yml b/.github/workflows/wp-unit-tests.yml index 54cb4e6..1080bfe 100644 --- a/.github/workflows/wp-unit-tests.yml +++ b/.github/workflows/wp-unit-tests.yml @@ -1,6 +1,6 @@ name: PHP WP Unit Tests -on: [ push, pull_request, workflow_dispatch ] +on: [ pull_request, workflow_dispatch ] permissions: {} diff --git a/tests/wp_bootstrap.php b/tests/wp_bootstrap.php index b3da035..1c066f5 100644 --- a/tests/wp_bootstrap.php +++ b/tests/wp_bootstrap.php @@ -10,7 +10,8 @@ use iTRON\wpConnections\Helpers\Database; use iTRON\wpConnections\WPStorage; -require_once dirname( __FILE__ ) . '/../wordpress-develop/tests/phpunit/includes/functions.php'; +$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. @@ -33,4 +34,4 @@ return true; }, 10, 1 ); -require_once dirname( __FILE__ ) . "/../wordpress-develop/tests/phpunit/includes/bootstrap.php"; +require_once $test_root . '/includes/bootstrap.php';