Commit Graph

8 Commits

Author SHA1 Message Date
Nestor Soriano d55f7d10f8 Some small imrpovements in the dependency injection framework:
- camelCase methods changed to snake_case for consistency with WP.
- Added a check in `ExtendedContainer::get` that throws an informative
  exception if a non-namespaced class name is passed.
- `container->reset_resolved()` is called during unit testing bootstrap.
- Added some utility methods in `WC_Unit_Test_Case`.
2020-07-24 09:23:05 +02:00
Nestor Soriano ca9ed93b9a Added dependency injection infrastructure for unit tests:
- Added a new class `ExtendedContainer` that extends League's container.
  - `add` modified to reject classes not in the root Woo namespace.
  - Has two new methods, `replace` and `reset_resolved`.
  - It's used as the underlying container instead of League's one
    in `Container`, but the new methods are not exposed.
- At unit test bootstrap time the globally registered container is
  replaced with the extended one that `Container` stores
  (grabbed from private property using reflection).
- A new `MockableLegacyProxy` is added. It inherits from `LegacyProxy`
  and allows to mock functions, static methods and legacy classes.
  - The registeed `LegacyProxy` is replaced with the mockable version
    during unit test bootstrap.
- A PHPUnit hook is added to reset the mockable proxy to its initial
  state (so that nothing is mocked) before each test.
- `WC_Unit_Test_Case` gets helper methods to mock functions, static
  methods and classes without having to retrieve the proxy class.
2020-07-24 09:23:05 +02:00
Nestor Soriano 32dac2486f Update autoload-dev in composer.json to add tests/tools directory
The mapping of the "Automattic\WooCommerce\Testing\Tools\" namespace
to the "tests/tools" directory is moved from manual registration
inside the tests bootstrap constructor to a declaration inside the
autload-dev section in composer.json.
2020-06-03 09:28:54 +02:00
Nestor Soriano bfda3f9938 Code hacker resdesign for single-load of code files.
The code hacker as originally designed, as a mechanism that allowed
to enable hacks at the individual test level, is flawed because it
assumes that code files are loaded before each test, but actually
the PHP engine loads code files only once.

Therefore this commit redesigns it so that the two existing main hacks,
the functions mocker and the static methods hacker, are applied
to all the relevant functions and classes at bootstrap time, and
mocks for each individual function/method can be registered at the
beginning of each test. See README for the full details.
2020-06-02 19:06:53 +02:00
Nestor Soriano 2a68bb018d Move testing tools to the tests/Tools directory
The testing tools (only the code hacker at this time) have been moved
from 'src' to 'tests/Tools', since many opcode cache plugins
load the whole src folder in production.

Also, an extra autoloader is set in the tests bootstrap so that
the 'tests/Tools' directory corresponds, using PSR4, to the
'Automattic\WooCommerce\Testing\Tools' namespace.
2020-05-20 09:57:39 +02:00
Nestor Soriano 8a7d955253 Improvements on the code hacker.
- Add methods to temporarily disable and reenable the code hacker.

The code hacker is causing issues in some tests that perform
write operations to the local filesystem. Since this happens only
in a few cases, the easiest fix is to temporarily disable the
code hacker when that happens. This commit adds two new methods
for that in `WC_Unit_Test_Case`: `disable_code_hacker` and
`reenable_code_hacker`.

These methods use a disabling requests count so that the hacker
isn't enabled before it should. E.g. you call `disable`, then
a helper method that does `disable` and `enable`, then `enable` -
then only the last `enable` will have effect.

- `CodeHacker::add_hack` has now a boolean `persistent` parameter.
Persistent hacks won't be cleared by `clear_hacks`.

- `CodeHackerTestHook::executeAfterTest` will now disable the hacker
only if no persistent hacks are registered.

- The existing `file_copy` method is made static for consistency.

- `CodeHacker::restore` method renamed to `disable` for clarity.
2020-05-20 09:56:26 +02:00
Nestor Soriano 884fd08462 Add a workaround for code hacking static methods on already loaded files.
The unit testing bootstrap loads and initializes WooCommerce, this
loads a bunch of code files that can't then be hacked in the test hooks.

A workaround is provided in this commit for the case of hacking
static methods. A new StaticWrapper class is created that allows
defining mock methods after the code file has been loaded.
This is applied to all classes from a fixed list in the bootstrap,
before WooCommerce is initialized. The list should be kept up to date
with the list of classes that require such workaround.
2020-05-20 09:56:25 +02:00
Christopher Allford b5bd8225e8 Moved PHPUnit suite into tests/legacy 2020-04-24 13:53:40 -07:00