Skip to content

Commit

Permalink
Update testing docs: Order Factory meta data and request methods (#2388)
Browse files Browse the repository at this point in the history
  • Loading branch information
mralaminahamed authored Oct 16, 2024
1 parent f4f60ea commit b4f7379
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
39 changes: 38 additions & 1 deletion docs/tdd/factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ $order_id = $this->factory()
->create([
'status' => 'pending',
'customer_id' => $this->factory()->customer->create([]),
'meta_data' => [
[
'key' => '_custom_meta_key',
'value' => 'custom_meta_value',
'unique' => true
],
[
'key' => '_another_meta_key',
'value' => 'another_meta_value'
]
],
'line_items' => array(
array(
'product_id' => $this->factory()->product
Expand Down Expand Up @@ -194,6 +205,32 @@ The assertion `$this->assertDatabaseCount('posts', 3, [ 'post_type' => 'shop_ord
You can pass [meta](./../../tests/php/src/Helpers/WC_Helper_Coupon.php#L33) as per your requirements.

### Adding Meta Data to Orders

To add meta data to an order, you can include a `meta_data` array in the order creation parameters. Each meta data item should be an array with `key` and `value` elements. Optionally, you can set `unique` to `true` if you want the meta to be unique.

Example:

```php
$order_id = $this->factory()->order->create([
// ... other order parameters ...
'meta_data' => [
[
'key' => '_custom_meta_key',
'value' => 'custom_meta_value',
'unique' => true
],
[
'key' => '_another_meta_key',
'value' => 'another_meta_value'
]
],
// ... more order parameters ...
]);
```

This will add two meta fields to the order: `_custom_meta_key` (set as unique) and `_another_meta_key`.

## Example of Factories

There is a test class named [DokanFactoryTest](./../../tests/php/src/DokanFactoryTest.php) which covers the test cases for most of the Dokan factories.
Expand All @@ -202,4 +239,4 @@ There is a test class named [DokanFactoryTest](./../../tests/php/src/DokanFactor

Dokan factory classes utilize the WC Factories so that we can adapt to future changes in WooCommerce. By wrapping WooCommerce helper methods, we ensure that our application test code remains consistent and compatible with any updates in WooCommerce. This approach allows us to modify the wrapper methods as needed to align with WooCommerce changes, maintaining the stability and reliability of our tests.

Dokan factory classes utilize the [WC Factories](https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce/tests/legacy/framework/helpers) so that we can adapt the future changes of Woocommerce. Our application test code will be consistent because well will be to change wrapper method to make compatible with WC changes.
Dokan factory classes utilize the [WC Factories](https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce/tests/legacy/framework/helpers) so that we can adapt the future changes of Woocommerce. Our application test code will be consistent because well will be to change wrapper method to make compatible with WC changes.
12 changes: 11 additions & 1 deletion docs/tdd/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ class SampleTest extends DokanTestCase {
$response = $this->post_request( 'route', $request_params );
$data = $response->get_data();
$status_code = $response->get_status();

// PUT request
$response = $this->put_request( 'route', $update_params );
$data = $response->get_data();
$status_code = $response->get_status();

// DELETE request
$response = $this->delete_request( 'route', $delete_params );
$data = $response->get_data();
$status_code = $response->get_status();
}
}
```
Expand Down Expand Up @@ -301,4 +311,4 @@ $array = [
// Use the custom assertion method
$this->assertNestedContains( [ 'subkey1' => 'value1' ], $array );
$this->assertNestedContains( [ 'key2' => 'value3' ], $array );
```
```

0 comments on commit b4f7379

Please sign in to comment.