From 02f9d4c2ba4133bf2f11fc90703a7fd6e0c10f70 Mon Sep 17 00:00:00 2001 From: Krzysztof Dyszczyk <krzysiek@inspirelabs.pl> Date: Mon, 19 Mar 2018 17:10:05 +0100 Subject: [PATCH] Seria usprawnien w testach i dokumentacji oraz wsparcie dla deployowania wtyczek do sklepu i repo --- .gitlab-ci.yml | 213 ++++++++++++++++++ apigen.neon | 28 +++ changelog.txt | 4 + composer.json | 6 +- composer.lock | 2 +- phpunit-integration.xml | 27 +++ phpunit-unit.xml | 21 ++ plugin-template.php | 1 + tests/integration/bootstrap.php | 22 ++ .../wpdesk/test-plugin-template-factory.php | 23 ++ tests/unit/bootstrap.php | 23 ++ .../wpdesk/test-plugin-template-factory.php | 38 ++++ 12 files changed, 406 insertions(+), 2 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 apigen.neon create mode 100644 changelog.txt create mode 100644 phpunit-integration.xml create mode 100644 phpunit-unit.xml create mode 100644 tests/integration/bootstrap.php create mode 100644 tests/integration/wpdesk/test-plugin-template-factory.php create mode 100644 tests/unit/bootstrap.php create mode 100644 tests/unit/wpdesk/test-plugin-template-factory.php diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..a187881 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,213 @@ +before_script: + - cd ${CI_PROJECT_DIR} + +variables: + MYSQL_ROOT_PASSWORD: mysql + MYSQL_DATABASE: wptest + MYSQL_USER: mysql + MYSQL_PASSWORD: mysql + MYSQL_INNODB_LOG_BUFFER_SIZE: 32M + PHP_ERROR_REPORTING: E_ALL + COMPOSER_ALLOW_SUPERUSER: 1 + GIT_STRATEGY: fetch + +cache: + untracked: true + paths: + - vendor + +stages: + - build + - tests + - pre-deploy + - deploy + +build php: + image: wpdesknet/phpunit-woocommerce:0-0 + stage: build + artifacts: + expire_in: 1 day + name: "dev vendor" + paths: + - vendor/ + script: + - ls /usr/local/bin + - composer install --no-progress + +#build js: +# image: node:slim +# stage: build +# script: +# - cd app +# - git clone git@gitlab.com:wpdesk/plugins-tests.git +# - cd plugins-tests +# - npm install +# - grunt dev-chrome + +code style test: + image: wpdesknet/phpunit-woocommerce:0-0 + stage: tests + allow_failure: true + dependencies: + - build php + script: + - ls -l + - php --version + - php composer.phar phpcs + +unit test 0: + image: wpdesknet/phpunit-woocommerce:0-0 + stage: tests + dependencies: + - build php + coverage: '/^\s*Lines:\s*\d+.\d+\%/' + script: + - ls -l + - php --version + - php composer.phar phpunit-unit + +integration test 0-0: + image: wpdesknet/phpunit-woocommerce:0-0 + services: + - mysql + stage: tests + coverage: '/^\s*Lines:\s*\d+.\d+\%/' + dependencies: + - build php + script: + - php --version + - php composer.phar phpunit-integration + +integration test 1-1: + image: wpdesknet/phpunit-woocommerce:1-1 + services: + - mysql + stage: tests + coverage: '/^\s*Lines:\s*\d+.\d+\%/' + dependencies: + - build php + script: + - php --version + - php composer.phar phpunit-integration + +integration test 2-2: + image: wpdesknet/phpunit-woocommerce:2-2 + services: + - mysql + stage: tests + coverage: '/^\s*Lines:\s*\d+.\d+\%/' + dependencies: + - build php + script: + - php --version + - php composer.phar phpunit-integration + +integration test 3-3: + image: wpdesknet/phpunit-woocommerce:3-3 + services: + - mysql + stage: tests + coverage: '/^\s*Lines:\s*\d+.\d+\%/' + dependencies: + - build php + script: + - php --version + - phpunit --configuration phpunit-integration.xml + +integration test 4-3: + image: wpdesknet/phpunit-woocommerce:4-3 + services: + - mysql + stage: tests + coverage: '/^\s*Lines:\s*\d+.\d+\%/' + dependencies: + - build php + script: + - php --version + - phpunit --configuration phpunit-integration.xml + +apigen docs: + image: + name: wpdesknet/apigen + stage: build + artifacts: + expire_in: 1 day + name: "docs" + paths: + - docs/ + script: + - ls -l + - /app/vendor/bin/apigen generate + - php /app/hooks-docs.php ${CI_PROJECT_DIR} + when: manual + +build to deploy: + image: wpdesknet/phpunit-woocommerce:0-0 + stage: pre-deploy + dependencies: + - build php + artifacts: + expire_in: 1 month + name: "production release" + paths: + - release + - release.zip + script: + - php --version + - ls -l + - rm -rf ${CI_PROJECT_DIR}/release + - rm -rf ${CI_PROJECT_DIR}/release.zip + - rm -rf /tmp/release + - mkdir /tmp/release + - mkdir ${CI_PROJECT_DIR}/release + - cp -rf ${CI_PROJECT_DIR}/* /tmp/release + - cp -rf /tmp/release/* ${CI_PROJECT_DIR}/release + - cd ${CI_PROJECT_DIR}/release + - php composer.phar install --no-dev --no-progress + - rm -rf build-coverage + - rm -rf release + - rm -rf tests + - rm -rf docs + - rm -rf .git + - rm -f .editorconfig + - rm -f .gitignore + - rm -f .gitlab-ci.yml + - rm -f apigen.neon + - rm -f composer.json + - rm -f composer.lock + - rm -f phpcs.xml.dist + - rm -f phpunit-integration.xml + - rm -f phpunit-unit.xml + - rm -f composer.phar + - rm -f wp-cli.phar + - zip -r -q ../release.zip ./ + only: + - tags + +deploy to shop: + image: wpdesknet/amazon-svn-deploy + stage: deploy + dependencies: + - build to deploy + retry: 2 + script: + - ls -l + - /tmp/deploy_shop.sh ${CI_PROJECT_NAME} release.zip ${CI_PROJECT_NAME}.zip + only: + - tags + when: manual + +deploy to repository: + image: wpdesknet/amazon-svn-deploy + stage: deploy + dependencies: + - build to deploy + retry: 2 + script: + - ls -l + - rm -rf /tmp/svn-repository + - mkdir /tmp/svn-repository + - /tmp/deploy_repository.sh flexible-pdf release /tmp/svn-repository + only: + - tags + when: manual \ No newline at end of file diff --git a/apigen.neon b/apigen.neon new file mode 100644 index 0000000..1ba3445 --- /dev/null +++ b/apigen.neon @@ -0,0 +1,28 @@ +destination: docs +extensions: [php] +source: + - classes + - plugin-template.php + - bootstrap.php +exclude: + - vendor + - tests + - languages + +charset: [UTF-8] +main: Wordpress plugin +title: Plugin template more info +baseUrl: "/" + +templateTheme: default +php: false +sourceCode: false +tree: true +deprecated: false +todo: false +download: false + +accessLevels: + - public + - private + - protected \ No newline at end of file diff --git a/changelog.txt b/changelog.txt new file mode 100644 index 0000000..5cdd438 --- /dev/null +++ b/changelog.txt @@ -0,0 +1,4 @@ +*** plugin-template Changelog *** + +1.0.0 - 2018-03-16 +* TEST \ No newline at end of file diff --git a/composer.json b/composer.json index df5efb6..644bbe7 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,10 @@ } }, "scripts": { - "phpcs": "vendor/bin/phpcs" + "test": "echo composer is alive", + "phpcs": "phpcs", + "phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never", + "phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never", + "docs": "apigen generate" } } diff --git a/composer.lock b/composer.lock index 3f35243..ba3b595 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "be2486cd1b6bdbc457d614783adfc639", + "hash": "ab79cfb4a1a3de35a971fb4fedc9b4a5", "content-hash": "12219163e8f6b69c0711dc250b74948b", "packages": [], "packages-dev": [ diff --git a/phpunit-integration.xml b/phpunit-integration.xml new file mode 100644 index 0000000..fe31963 --- /dev/null +++ b/phpunit-integration.xml @@ -0,0 +1,27 @@ +<phpunit bootstrap="tests/integration/bootstrap.php" + backupGlobals="false" + > + <testsuites> + <testsuite> + <directory prefix="test-" suffix=".php">./tests/integration</directory> + </testsuite> + </testsuites> + + <filter> + <whitelist> + <directory suffix=".php">classes</directory> + </whitelist> + </filter> + + <logging> + <log type="junit" target="build-coverage/report.junit.xml"/> + <log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/> + <log type="coverage-text" target="build-coverage/coverage.txt"/> + <log type="coverage-clover" target="build-coverage/clover.xml"/> + </logging> + <php> + <env name="WP_DEVELOP_DIR" value="/tmp/wordpress-develop"/> + <env name="WC_DEVELOP_DIR" value="/tmp/woocommerce"/> + </php> + +</phpunit> \ No newline at end of file diff --git a/phpunit-unit.xml b/phpunit-unit.xml new file mode 100644 index 0000000..627771b --- /dev/null +++ b/phpunit-unit.xml @@ -0,0 +1,21 @@ +<phpunit bootstrap="tests/unit/bootstrap.php"> + <testsuites> + <testsuite> + <directory prefix="test-" suffix=".php">./tests/unit/</directory> + </testsuite> + </testsuites> + + <filter> + <whitelist> + <directory suffix=".php">classes</directory> + </whitelist> + </filter> + + <logging> + <log type="junit" target="build-coverage/report.junit.xml"/> + <log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/> + <log type="coverage-text" target="build-coverage/coverage.txt"/> + <log type="coverage-clover" target="build-coverage/clover.xml"/> + </logging> + +</phpunit> \ No newline at end of file diff --git a/plugin-template.php b/plugin-template.php index 74f2d55..89d060f 100644 --- a/plugin-template.php +++ b/plugin-template.php @@ -28,6 +28,7 @@ */ + if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php new file mode 100644 index 0000000..9699913 --- /dev/null +++ b/tests/integration/bootstrap.php @@ -0,0 +1,22 @@ +<?php +// disable xdebug backtrace +if ( function_exists( 'xdebug_disable' ) ) { + xdebug_disable(); +} + +if ( getenv( 'PLUGIN_PATH' ) !== false ) { + define( 'PLUGIN_PATH', getenv( 'PLUGIN_PATH' ) ); +} else { + define( 'PLUGIN_PATH', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR ); +} + +require_once( getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit/includes/functions.php' ); + +tests_add_filter( 'muplugins_loaded', function () { + require PLUGIN_PATH . '/plugin-template.php'; + $plugins_to_active[] = 'plugin-template/plugin-template.php'; + update_option( 'active_plugins', $plugins_to_active ); +}, 100 ); + +putenv('WP_TESTS_DIR=' . getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit'); +require_once( getenv( 'WC_DEVELOP_DIR' ) . '/tests/bootstrap.php' ); diff --git a/tests/integration/wpdesk/test-plugin-template-factory.php b/tests/integration/wpdesk/test-plugin-template-factory.php new file mode 100644 index 0000000..07f6306 --- /dev/null +++ b/tests/integration/wpdesk/test-plugin-template-factory.php @@ -0,0 +1,23 @@ +<?php + +use PHPUnit\Framework\TestCase; + +require_once( PLUGIN_PATH . '/classes/wpdesk/class-plugin.php' ); +require_once( PLUGIN_PATH . '/classes/plugin-template-factory.php' ); + +class Test_WPDesk_Plugin_Template_Factory extends TestCase { + + public function testBuildForNotActivatedPlugin() { + $factory = new WPDesk_Plugin_Template_Factory(); + $this->assertInstanceOf( WPDesk_Plugin_Template_Plugin::class, $factory::build_plugin() ); + } + + public function testGetPluginAlwaysSingleInstance() { + $factory = new WPDesk_Plugin_Template_Factory(); + $instanceOne = $factory::get_plugin_instance(); + $instanceTwo = $factory::get_plugin_instance(); + + $this->assertInstanceOf( WPDesk_Plugin_Template_Plugin::class, $instanceOne ); + $this->assertSame($instanceOne, $instanceTwo); + } +} \ No newline at end of file diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php new file mode 100644 index 0000000..e55059f --- /dev/null +++ b/tests/unit/bootstrap.php @@ -0,0 +1,23 @@ +<?php +/** + * PHPUnit bootstrap file + */ + +require_once __DIR__ . '/../../vendor/autoload.php'; + +error_reporting( E_ALL ); + +if ( getenv( 'PLUGIN_PATH' ) !== false ) { + define( 'PLUGIN_PATH', getenv( 'PLUGIN_PATH' ) ); +} else { + define( 'PLUGIN_PATH', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR ); +} + +if ( getenv( 'ABSPATH' ) !== false ) { + define( 'ABSPATH', getenv( 'ABSPATH' ) ); +} else { + define( 'ABSPATH', PLUGIN_PATH . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR ); +} + +WP_Mock::setUsePatchwork( true ); +WP_Mock::bootstrap(); diff --git a/tests/unit/wpdesk/test-plugin-template-factory.php b/tests/unit/wpdesk/test-plugin-template-factory.php new file mode 100644 index 0000000..2ab9028 --- /dev/null +++ b/tests/unit/wpdesk/test-plugin-template-factory.php @@ -0,0 +1,38 @@ +<?php + +use PHPUnit\Framework\TestCase; + +require_once( PLUGIN_PATH . '/classes/wpdesk/class-plugin.php' ); +require_once( PLUGIN_PATH . '/classes/plugin-template-factory.php' ); + +class Test_WPDesk_Plugin_Template_Factory extends TestCase { + public function setUp() { + WP_Mock::setUp(); + } + + public function tearDown() { + WP_Mock::tearDown(); + } + + public function testBuildForNotActivatedPlugin() { + WP_Mock::userFunction( 'plugin_basename', [ + 'return' => 'some-plugin-name' + ] ); + WP_Mock::userFunction( 'admin_url', [ + 'return' => 'http://whatever.com' + ] ); + WP_Mock::userFunction( 'get_option', [ + 'args' => [ 'api_._activated', '0' ], + 'return' => 'not Activated' + ] ); + WP_Mock::userFunction( 'plugin_dir_url', [ + 'return' => 'whatever' + ] ); + WP_Mock::userFunction( 'trailingslashit', [ + 'return_arg' => 0 + ] ); + + $factory = new WPDesk_Plugin_Template_Factory(); + $this->assertInstanceOf( WPDesk_Plugin_Template_Plugin::class, $factory::build_plugin() ); + } +} \ No newline at end of file -- GitLab