woocommerce/tests
Nestor Soriano 9de1306c21 Fix counters in nav filtering widgets for variable products.
After the change that registers variation attributes as terms
(in addition to reigstering them as post meta) it is now time
to modify the get_filtered_term_product_counts methods in
WC_Widget_Layered_Nav so that it works consistently for both
variable and non-variable products. The logic for the counters
is now as follows:

with OR operator:
- Simple products: count the attributes of all visible products
  (unchanged behavior).
- Variable products: count attributes corresponding to
  visible variations.

with AND operator:
- Simple products: count the attributes of visible products but only
  for products that have all the selected (unchanged behavior).
- Variable products: find all the products for which all the variations
  corresponding to the selected attributes exist and are visible,
  then count the attributes corresponding to the visible variations
  of those products.

A product is "visible" if it's published, not excluded for catalog,
and has stock. Additionally, a variable product will not be considered
visible if the parent product is not.
2020-07-28 08:55:55 +02:00
..
Tools Replace array_merge_recursive in MockableLegacyProxy with custom code 2020-07-24 14:51:37 +02:00
bin restore travis e2e setup script 2020-05-01 10:18:21 -03:00
cli Removed the default `id` field added to all CLI commands 2020-07-10 14:16:30 -07:00
e2e Adjusting the shipping task order in the E2E test for setup tasks 2020-07-27 15:18:27 -07:00
legacy Record attribute terms for product variations in wp_term_relationships. 2020-07-28 08:55:55 +02:00
php Merge pull request #26731 from woocommerce/feature/introduce-dependency-injection 2020-07-24 15:12:16 +02:00
unit-tests Fix counters in nav filtering widgets for variable products. 2020-07-28 08:55:55 +02:00
README.md Add documentation about the container and good coding practices. 2020-07-24 09:25:12 +02:00

README.md

WooCommerce Tests

This document discusses unit tests. See the e2e README to learn how to setup testing environment for running e2e tests and run them.

Table of contents

Initial Setup

From the WooCommerce root directory (if you are using VVV you might need to vagrant ssh first), run the following:

  1. Install PHPUnit via Composer by running:

    $ composer install
    
  2. Install WordPress and the WP Unit Test lib using the install.sh script:

    $ tests/bin/install.sh <db-name> <db-user> <db-password> [db-host]
    

You may need to quote strings with backslashes to prevent them from being processed by the shell or other programs.

Example:

$ tests/bin/install.sh woocommerce_tests root root

#  woocommerce_tests is the database name and root is both the MySQL user and its password.

Important: The <db-name> database will be created if it doesn't exist and all data will be removed during testing.

Running Tests

Change to the plugin root directory and type:

$ vendor/bin/phpunit

The tests will execute and you'll be presented with a summary.

You can run specific tests by providing the path and filename to the test class:

$ vendor/bin/phpunit tests/legacy/unit-tests/importer/product.php

A text code coverage summary can be displayed using the --coverage-text option:

$ vendor/bin/phpunit --coverage-text

Writing Tests

There are three different unit test directories:

  • tests/legacy/unit-tests contains tests for code in the includes directory. No new tests should be added here, ever; existing test classes shouldn't get new tests either. Fixing faulty existing tests is allowed.
  • tests/php/includes is where all the new tests for code in the includes directory should be written.
  • tests/php/src is where all the tests for code in the src directory should be written.

Each test file should correspond to an associated source file and be named accordingly: * For src code: The base namespace for tests is Automattic\WooCommerce\Tests. A class named Automattic\WooCommerce\TheNamespace\TheClass should have a test named Automattic\WooCommerce\Tests\TheNamespace\TheClassTest. * For includes code: * When testing classes: use the same approach as for src except that namespaces are not used. So a WC_Something class in includes/somefolder/class-wc-something.php should have its tests in tests/src/internal/somefolder/class-wc-something-test.php. * When testing functions: use one test file per functions group file, for example wc-formatting-functions-test.php for code in the wc-formatting-functions.php file.

See also the guidelines for writing unit tests for src code and the guidelines for includes code.

General guidelines for all the unit tests:

  • Each test method should cover a single method or function with one or more assertions
  • A single method or function can have multiple associated test methods if it's a large or complex method
  • Use the test coverage HTML report (under tmp/coverage/index.html) to examine which lines your tests are covering and aim for 100% coverage
  • For code that cannot be tested (e.g. they require a certain PHP version), you can exclude them from coverage using a comment: // @codeCoverageIgnoreStart and // @codeCoverageIgnoreEnd. For example, see wc_round_tax_total()
  • In addition to covering each line of a method/function, make sure to test common input and edge cases.
  • Prefer assertSame() where possible as it tests both type and value
  • Remember that only methods prefixed with test will be run so use helper methods liberally to keep test methods small and reduce code duplication. If there is a common helper method used in multiple test files, consider adding it to the WC_Unit_Test_Case class so it can be shared by all test cases
  • Filters persist between test cases so be sure to remove them in your test method or in the tearDown() method.
  • Use data providers where possible. Be sure that their name is like data_provider_function_to_test (i.e. the data provider for test_is_postcode would be data_provider_test_is_postcode). Read more about data providers here.

Automated Tests

Tests are automatically run with Travis-CI for each commit and pull request.

Code Coverage

Code coverage is available on Codecov which receives updated data after each Travis build.