Commit Graph

35735 Commits

Author SHA1 Message Date
Nestor Soriano c2e28b2b05 Replace array_merge_recursive in MockableLegacyProxy with custom code
test_static_mocks_can_be_used_via_injected_legacy_proxy_and_woocommerce_object
was failing in PHP 7.0, this replacement fixes it.
Suspected cause: https://bugs.php.net/bug.php?id=76505
2020-07-24 14:51:37 +02:00
Peter Fabian 03230ecf01
Merge pull request #26642 from woocommerce/fix/26607
Limit stock changes for order items to status methods for consistency.
2020-07-24 13:23:53 +02:00
Nestor Soriano 8f596bd25a Update composer.lock 2020-07-24 09:41:45 +02:00
Nestor Soriano e55ef052a2 Add documentation about the container and good coding practices.
- src/README.md largely expanded
- tests/README.md expanded
- includes/README.md added
- src/Internal/REAMDE.md added

src/README.md and includes/README.md have TODO placeholders to add
guidelines regarding to actions and filters.
2020-07-24 09:25:12 +02:00
Christopher Allford 811ac747e6 Added callable reflection to `add_with_auto_arguments`
This allows the dependencies of the callable function to be automatically resolved out of the container too!
2020-07-24 09:25:12 +02:00
Nestor Soriano 408295720c Fix LegacyProxy::get_instance_of for classesd having an `instance` method.
Pass the arguments as `...$args` instead of `$args`.
Also fix related unit test, and remove unnecessary `is_function`.
2020-07-24 09:25:12 +02:00
Nestor Soriano bd1e6a5db0 Add the ContainerException class, use it for dependency injection related errors. 2020-07-24 09:25:12 +02:00
Nestor Soriano 71462e33f3 Improevements on ExtendedContainer and LegacyProxy documentation.
Also, remove a not really needed `try` block in LegacyProxy::get_instance_of.
2020-07-24 09:25:11 +02:00
Nestor Soriano 65b5cbe692 Improve AbstractServiceProvider::add_with_auto_arguments
If a class name is passed as a concrete, check that the class
constructor is public if it exists. If another type of concrete is
passed, check that it's valid (a callback or an object).

Also update the autoloader to check if the class file exists,
otherwise class_exists fails if a namespaced class doesn't exist.
2020-07-24 09:25:11 +02:00
Nestor Soriano 6fd84a0401 Add shortcut methods for the LegacyProxy in the WooCommerce class.
The following methods are added and can be invoked using `WC()`,
they just redirect to the same methods in LegacyProxy:

call_function
call_static
get_instance_of
2020-07-24 09:25:11 +02:00
Nestor Soriano abf53086ca Remove @throws annotation from some unit tests. 2020-07-24 09:25:11 +02:00
Christopher Allford 53e1f23af5 Moved autoloading of `Automattic\WooCommerce\Testing\Tools` namespace into test bootstrap
Since we need to load all of these files before WooCommerce has initialized we can't rely on Composer to handle the autoloading. We should take this namespace out of Composer altogether and just have our test autoloader take care of it.
2020-07-24 09:25:09 +02:00
Christopher Allford 24f9738913 Reworked the temporary PSR4 autoloader to better support more namespaces
Since we want to avoid all of the jetpack-autoloader errors I've reworked the temporary autoloader to support defining the namespace it should load from.
2020-07-24 09:24:06 +02:00
Nestor Soriano 29cf161415 Small changes after review feedback:
- Method and class renames.
- Removed unnecessary autoloader registration.
- Add a unit test for classes with non-object type hints
  in constructor arguments.
2020-07-24 09:24:06 +02:00
Nestor Soriano 1684ce08b3 Removed annotations as we're not going to use them. 2020-07-24 09:24:06 +02:00
Nestor Soriano 4ec143532c Adjustments in the LegacyProxy class.
- `get_instance_of` accepts now arguments to be passed to the
  class constructor if necessary.
- `get_special_instance_of` method removed, instead, now if a method
  named `get_instance_of_<lowercased class name>` exists in the class,
  it is used to get the instance of the class.
- A couple more unit tests added.
2020-07-24 09:24:06 +02:00
Nestor Soriano 282d1dab86 DependencyManagement code folder moved inside an Internal folder. 2020-07-24 09:24:06 +02:00
Nestor Soriano c1ed403dd0 Add `@public` to `FunctionsMockerHack`.
Also to a couple of methods related to the code hacker.
2020-07-24 09:24:06 +02:00
Nestor Soriano 044a79d941 Added unit tests for `LegacyProxy` and `MockableLegacyProxy`.
Also:

- Added sutoloader for the `tests/tools` directory.
2020-07-24 09:24:04 +02:00
Nestor Soriano c9154d071c Add tests for `AbstractServiceProvider` and `ExtendedContainer`.
Also:

- Make the methods in `AbstractServiceProvider` protected.
- Add an autoloader for files in the `tests/php/src` directory.
- Fix a bug in the provisional (?) autoloader.
2020-07-24 09:23:05 +02:00
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 d5d02a7175 Fix code sniffer errors in wc-core-functions.php 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 03bcf515c1 `DependencyManagement` and `Proxies` directories moved.
They have been moved out of the `Tools` directory to the root
`src` directory, `Tools` doesn't exist anymore.
2020-07-24 09:23:05 +02:00
Nestor Soriano b53fc512bf Some changes on the dependency injection engine based on feedback:
- `wc_get_container` declares a return type now.
- The autoloader now runs before `autoload_packages.php` is included.
- The autloader now excludes the namespaces for Admin, Blocs, Rest API.
- `private $container` in `Container` is now declared as being
  of type `\League\Container\Container`.
- The underlying container is registered with `share` instead of `add`.
- All methods in `AbstractServiceProvider` now allow passing a concrete.
- `AbstractServiceProvider::addWithAutoArguments` now checks if an
  argument has a default value, and registers the argument so that
  that value is supplied when constructing the object.
- `Proxies` class renamed to `ProxiesServiceProvider`.
- The methods to call code in `LegacyProxy` now use `call_user_func_array`.
2020-07-24 09:23:05 +02:00
Nestor Soriano 064ae558ab Changes in the overall organization of the dependency injection:
- The `Container` class now implements `Psr\Container\ContainerInterface`
  (and registers itself as such), holding a private instance of the
  real container. This way it's a read-only container from the point
  of view of plugins (which should use their own containers, but
  can still use this to get WooCommerce classes).
- All registrations are now done in the `Container` constructor via
  service providers.
- The container instance is now held in a global variable, set in
  `woocommerce.php`
- Added the `wc_get_container` function for old code.
- Added the `AbstractServiceProvider` class, which inherits with the
  corresponding League's class and adds some utility methods,
  most notably `add/shareWithAutoArguments`.
- Added the `ActionsProxy` and `LegacyProxy` classes, they are
  registered via a dedicated service provider.
- `WC_Queue_Interface` is no longer resolvable via the container
  (which is for classes inside `src` only).
- All the method names in the new classes have the format `fooBarFizz`
  to be PSR4 compliant, so the MethodNameInvalid error has been
  disabled in phpcs.xml for the `src` directory.
- Introduced the `@public` annotation for public API classes
  (classes that plugins can use and whose backwards compatibility
  we guarantee), applied to `ActionsProxy` and to `LegacyProxy` for now.
- Removed the hack for the autoloader as now it doesn't work anyway.
  For the changes in this branch to work, now WP_DEBUG must be false.
2020-07-24 09:23:01 +02:00
Nestor Soriano 23df8a4d17 Revert a couple of files to its original content after a messed rebase 2020-07-24 09:20:24 +02:00
Nestor Soriano a7829525eb Changes in the dependency injection container:
- Renamed from `ObjectContainer` to `Container`.
- It now inherits from PHP League's `Container`.
- It has now a `defineAsSharedAutowired` method.
- Initialization moved to the `WooCommerce::init_container` method.
- The static method for object resolution is now `WooCommerce::get_instance_of`.
2020-07-24 09:20:24 +02:00
Nestor Soriano 281ec18158 Undo the registration of old singletons in the DI container.
The container will in principle be used only to register classes
in the `src` directory.

Also, CustomerProvider class removed.
2020-07-24 09:20:24 +02:00
Nestor Soriano 518c52b829 Move PSR4 autoloading from woocommerce.php to the Autoloader class 2020-07-24 09:20:24 +02:00
Nestor Soriano 312383ae47 First steps towards introducing a dependency injection framework.
- Add PHP League's Container package via Composer.
- Add an ObjectContainer class that encapsulates all the configuration
  and insulates the codebase from the concrete DI engine used.
- Add an improved ReflectionContainer class that will allow to
  register individual classes as singletons while autowiring.
- Use ObjectContainer to resolve the WooCommerce class, everything
  instantiated with "new" inside it, and all singletons that are
  usually obtained via WC() function.
- Introduce the CustomerProvider class.
- Introduce a service provider to resolve WC_Queue_Interface,
  this replaces the WC_Queue class.
- Mark as obsolete all the replaced "instance()" methods,
  and the entire WC_Queue class.
2020-07-24 09:20:21 +02:00
budzanowski 7bc30512a4 Remove extra tab. 2020-07-23 23:22:16 +02:00
budzanowski 623363ba50 Check for if new locale has language pack installed. 2020-07-23 22:52:59 +02:00
Tam Mullen 86da271e6e
Merge pull request #27063 from woocommerce/fix/e2e-obw-4.3
Fix OBW e2e test in WC 4.3: Scroll Home Screen tasks list into view for the `Set up shipping` click to work
2020-07-23 15:48:18 +01:00
vedanshujain e1e5acfe1f Add 'includes/api/' from commit '880b82a0373b5b019294409b4d29f861b8964261'
git-subtree-dir: includes/api
git-subtree-mainline: 5a784bc1e4
git-subtree-split: 880b82a037
2020-07-23 00:34:46 +05:30
vedanshujain 880b82a037 Keep only source to merge with core. 2020-07-23 00:29:41 +05:30
vedanshujain 5a784bc1e4 Keep only unit test files for merge into core. 2020-07-23 00:07:29 +05:30
vedanshujain e42b97e7ea Add 'tests/legacy/unit-tests/api/' from commit '5335083922d350e73eaf0e8370d8335e1b076b55'
git-subtree-dir: tests/legacy/unit-tests/api
git-subtree-mainline: 2d39467fb4
git-subtree-split: 5335083922
2020-07-22 23:56:29 +05:30
vedanshujain 5335083922 Keep only unit test files for merge into core. 2020-07-22 23:55:16 +05:30
Dhruvin c843455b0b Clear Action Scheduler Transients
Currently when Clear Transients action is performed via System Tools, the Action Scheduler Transients are not cleared resulting in incorrect comment counts.
2020-07-22 17:41:34 +05:30
Peter Fabian 2f53fac78c Update jetpack autoloader to ^1.6 2020-07-22 17:40:59 +05:30
Andrew Minion 1387725088 add X-WP-Total headers to product attribute API calls 2020-07-22 17:39:58 +05:30
Csaba Maulis 0c366c5a75 Fix `date_expire` and `expiry_date` field schema definitions
Coupon controller for all API versions
2020-07-22 17:38:57 +05:30
Mindaugas Budreika 0aac20b404 forward query params (product variations) 2020-07-22 17:23:47 +05:30
vedanshujain bc52dbad89 Fix tests in response to Core 25630 2020-07-22 17:22:16 +05:30
vedanshujain 0576b142a4 Add unit tests 2020-07-22 17:20:46 +05:30
vedanshujain 7d335c5eab Remove `modified` enum from customer because its not supported.
Customers are based on `users` table which does not have a modified or similar column to track last modified timestamp, so this param cannot be supported at this time.
2020-07-22 17:20:35 +05:30
Stamoulis Zamanis a37628e94b Include modified in orderby filter 2020-07-22 17:20:23 +05:30
budzanowski 70c65f60f2 Don't process if there are no plugins to check. 2020-07-22 12:16:20 +02:00
budzanowski ed1a181eca Update api url. 2020-07-22 10:56:04 +02:00