From 4583cd949b2d89097adca638b41b27a5f19f040f Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 16 Sep 2019 17:21:33 +0200 Subject: [PATCH 1/4] Step 8 --- README.md | 126 ------------------------------------------------------ 1 file changed, 126 deletions(-) diff --git a/README.md b/README.md index b7f1649..3b2c525 100644 --- a/README.md +++ b/README.md @@ -8,131 +8,6 @@ _Optional, but you might need to [get docker](https://docs.docker.com/install/) --- -## Step 1 - -I assume you have composer installed. Let's install *PHPUnit* first: - - composer require --dev phpunit/phpunit ^8.3 - -Please, check also the [requirements](https://phpunit.readthedocs.io/en/8.3/installation.html#requirements)! - -> PHPUnit 8.3 requires at least PHP 7.2! By the way - security support for PHP 7.1 ends on 1st of December 2019. - -_Hint_: You don't have composer installed? Try this! - - docker run --rm -it -v $PWD:/app -u $(id -u):$(id -g) composer install - -## Step 2 - -There are at least two valid frameworks that come handy when you plan to test WordPress extensions: - -- [WP_Mock](https://github.com/10up/wp_mock) -- [Brain Monkey](https://brain-wp.github.io/BrainMonkey/) - -Let's try *Brain Monkey*: - -`composer require --dev brain/monkey:2.*` - -This will automatically install also [Mockery](http://docs.mockery.io/en/latest/) and [Patchwork](http://patchwork2.org/). Just execute `composer install` and you are good to go. - -## Step 3 - -Let's create a directory that will be the home for a small test-class named *WcctaTest.php*: - - mkdir -p tests/wccta - -Excellent! Now let's create a *phpunit.xml* configuration file in the root directory. - -> You could also decide to run your tests with the configuration parameters from the command-line. See the next part (hint: 'scripts')! - -Great! Now let's add some sections to *composer.json* file: - -- **name**: that's the project's name for packagist.org -- **description**: that's the description for packagist.org -- **type**: defines the code as WordPress plugin -- **autoload**: Let's use a PSR-4 autoloader! -- **scripts**: now you can just type `composer test` - -## Step 4 - -Lets create a directory that will give a home to our source-code. This is the place where you'll put a first class that you'll test soon. - - mkdir -p src/wccta && touch src/wccta/Plugin.php - rm -f tests/wccta/WcctaTest.php && touch tests/wccta/PluginTest.php - touch wordpress-plugins-phpunit.php - -We want to test some method of the class plugin. Imagine a method called `is_loaded` that returns `true` on success. When you are ready, execute: - - composer test - -_Hint_: Your system or PHP version is not up to date? You could just skip this step but let's try something [not so] new! - - docker run -it --rm -v $PWD:/app -w /app php:7.3-alpine php ./vendor/bin/phpunit - -You can probably imagine that some plugins will have lots of classes and that you can easily forget to test all the functionality that need tests. - -So, let's talk about __Coverage__! - -Just add a custom command to the scripts-section in your *composer.json*: - - "coverage": "./vendor/bin/phpunit --coverage-html ./reports/php/coverage" - -and a filter to your *phpunit.xml*: - - - - ./src - - - -Now just execute `composer coverage`! This will create a directory `./reports/php/coverage` together with some html-files. Well, not on all computers. Some will still get error-messages like: - - Error: No code coverage driver is available - -Let's fix that in our docker-image. I prepared a _Dockerfile_ so that you can just execute: - - docker build -t coverage . - -And after the build process has been finished: - - docker run -it --rm -v $PWD:/app -w /app coverage:latest php ./vendor/bin/phpunit --coverage-html ./reports/php/coverage - -_Now you know Kung Fu!_ Please, open the `./reports/php/coverage/index.html` in your browser! - -## Step 5 - -Let's wire our Plugin class to the plugin. Before we really go into testing, I just show you how to declare parts of your codes as _not to test_. - - @codeCoverageIgnore - -This is one of the important [annotations](https://phpunit.readthedocs.io/en/8.3/annotations.html) that are available. We will come to this later, but first: - -_Run the unittests with the coverage-report again!_ - -You did maybe notice the column `CRAP` in the coverage report. CRAP is an acronym for **change risk anti-patterns**. It indicates how risky a change of code in a class or method can be. You can lower the risk (and therefore the index) with less complex code **and** full coverage with tests. - -## Step 6 - -Let's start to test something. But what? There is still no further functionality written that needs testing. - -Here comes [TDD](https://it.wikipedia.org/wiki/Test_driven_development) (Test Driven Development) into the game. - -Even if you decide to *not* to use this technique, you should at least know what we are talking about. - -Let's first create a Test `CarTest` that shall test if the method `getPrice` returns the string `'€ 14.500'`. Then create a Class `Car` and write the method `getPrice` that **satisfies** the test. Don't start with the implementation. - -At this point let me introduce also the testing pattern AAA (Arrange Act Assert) which is widely accepted in TDD. It describes how to arrange a test and is very similar to GWT (Given When Then) from [BDD](https://en.wikipedia.org/wiki/Behavior-driven_development) (Behavior-driven Development). - -## Step 7 - -Let's now implement the getPrice-method. - -First of all let's assume that we can pass a JSON-object to our car class. This will give our class a bit more value. - -Now write an constructor that handles the JSON input and stores the object in a member-var `data`. The `getPrice`-method should take the price from the `data` var and take care of the formatted output. - -The member-variable `price` should be an integer. This is probably right now no problem because you can use the PHP-function `number_format` to create the correct output. But in a WordPress installation you'll expect very likely to have the locale set, to *Italian (it_IT)* for example. - ## Step 8 The correct way to format numbers in WordPress is the use of the function `number_format_i18n()`. @@ -158,4 +33,3 @@ Finally, let's change the parent of our test-classes. We are ready to mock our first WordPress-function with Functions\expect( $name_of_function )->andReturn( $value ); - From c37888f952e227677dd8664c7a201358b425eca0 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Tue, 17 Sep 2019 16:51:43 +0200 Subject: [PATCH 2/4] Step 8 reviewed --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3b2c525..2b852fc 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ _Optional, but you might need to [get docker](https://docs.docker.com/install/) ## Step 8 -The correct way to format numbers in WordPress is the use of the function `number_format_i18n()`. +The correct way to format numbers in _WordPress_ is the use of the function `number_format_i18n()`. -So let's change that and see what will happen: +So let's change that and see what happens: `Error: Call to undefined function wccta\number_format_i18n()` -We will fix this in a second, but let's prepare this a bit first. Brain Monkey uses the `setUp()` and `tearDown()` provided by PHPUnit. You can [override those methods](https://brain-wp.github.io/BrainMonkey/docs/wordpress-setup.html). Let's create a custom TestCase - let's name it WcctaCase - that we can extend because we'll do this probably in every test-class. +We will fix this in a second, but let's prepare this a bit first. **Brain Monkey** uses the `setUp()` and `tearDown()` provided by **PHPUnit**. You can [override those methods](https://brain-wp.github.io/BrainMonkey/docs/wordpress-setup.html). Let's create a custom `TestCase` - name it `WcctaCase` - that we can extend because we'll do this probably in every test-class. Now let's include the namespace for tests in the section autoload-dev: @@ -30,6 +30,6 @@ Finally, let's change the parent of our test-classes. class CarTest extends WcctaTestCase { // ... } -We are ready to mock our first WordPress-function with +We are ready to mock our first _WordPress_-function with Functions\expect( $name_of_function )->andReturn( $value ); From 4d15c0c60ff4f6209bea6971360705d55830446f Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 19 Sep 2019 14:57:07 +0200 Subject: [PATCH 3/4] Method call corrected --- src/wccta/Car.php | 2 +- tests/wccta/CarTest.php | 4 ++-- wordpress-plugins-phpunit.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wccta/Car.php b/src/wccta/Car.php index c2e3eed..a1cc8f7 100644 --- a/src/wccta/Car.php +++ b/src/wccta/Car.php @@ -10,7 +10,7 @@ public function __construct( string $json ) { $this->data = json_decode( $json ); } - public function getPrice() { + public function get_price() { $price = $this->data->price ?? 0; return sprintf( '€ %s', number_format_i18n( $price ) ); diff --git a/tests/wccta/CarTest.php b/tests/wccta/CarTest.php index fb79c20..e0f6502 100644 --- a/tests/wccta/CarTest.php +++ b/tests/wccta/CarTest.php @@ -8,7 +8,7 @@ class CarTest extends WcctaTestCase { - public function test_getPrice() { + public function test_get_price() { // Arrange Functions\expect( 'number_format_i18n' )->andReturn( '14.500' ); @@ -16,7 +16,7 @@ public function test_getPrice() { $sut = new Car( $json ); // Act - $actual = $sut->getPrice(); + $actual = $sut->get_price(); // Assert $this->assertEquals( '€ 14.500', $actual ); diff --git a/wordpress-plugins-phpunit.php b/wordpress-plugins-phpunit.php index 73db74a..e544175 100644 --- a/wordpress-plugins-phpunit.php +++ b/wordpress-plugins-phpunit.php @@ -19,4 +19,4 @@ require_once WCCTA_PLUGIN_DIR . 'vendor/autoload.php'; } -wccta\Plugin::init(); \ No newline at end of file +wccta\Plugin::create(); \ No newline at end of file From 0d7f382831e3ca67ed5083223b31e43d2f72a8c6 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 19 Sep 2019 15:05:22 +0200 Subject: [PATCH 4/4] Refactoring --- tests/wccta/CarTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/wccta/CarTest.php b/tests/wccta/CarTest.php index e0f6502..627d7d7 100644 --- a/tests/wccta/CarTest.php +++ b/tests/wccta/CarTest.php @@ -2,7 +2,6 @@ namespace tests\wccta; -use Brain\Monkey; use Brain\Monkey\Functions; use wccta\Car;