Skip to content

Commit 4c83c41

Browse files
authored
tests: improve test cleanup (#484)
1 parent b17f342 commit 4c83c41

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
## [Unreleased]
44

55
- feat: Deprecate `PhoneField.phoneFormat` in favor of `PhoneField.phoneFormatType`, and add new `GfPhoneFormat` type. Props @chetanupare, @justlevine.
6-
76
- fix: Correctly resolve `AddressField.defaultState`, `AddressField.defaultProvince` based on the `addressType`. H/t @byanko-bot
87
- fix: Set the default `AddressField.inputs.label` for the State/Province and Zip/Postal inputs based on the `addressType`. H/t @byanko-bot
98
- chore: Update Composer and NPM deps.
9+
- tests: Improve cleanup and factory instantiation for better local isolation.
1010

1111
## [v0.13.3]
1212

tests/_support/TestCase/GFGraphQLTestCase.php

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,25 @@
1515

1616
/**
1717
* Class - GraphQLTestCase
18+
*
19+
* @property \stdClass{
20+
* draft_entry: \Tests\WPGraphQL\GF\Factory\DraftEntry,
21+
* entry: \Tests\WPGraphQL\GF\Factory\Entry,
22+
* field: \Tests\WPGraphQL\GF\Factory\Field,
23+
* form: \Tests\WPGraphQL\GF\Factory\Form,
24+
* } factory
1825
*/
1926
class GFGraphQLTestCase extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
27+
/**
28+
* The available factories
29+
*/
30+
private const FACTORIES = [
31+
'draft_entry' => \Tests\WPGraphQL\GF\Factory\DraftEntry::class,
32+
'entry' => \Tests\WPGraphQL\GF\Factory\Entry::class,
33+
'field' => \Tests\WPGraphQL\GF\Factory\Field::class,
34+
'form' => \Tests\WPGraphQL\GF\Factory\Form::class,
35+
];
36+
2037
/**
2138
* @var \WpunitTester
2239
*/
@@ -42,17 +59,11 @@ class GFGraphQLTestCase extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
4259
public function setUp(): void {
4360
parent::setUp();
4461

62+
// Clean up any leftover GF data from previous test runs.
63+
$this->cleanup_gf_data();
64+
4565
// Load factories.
46-
$factories = [
47-
'DraftEntry',
48-
'Entry',
49-
'Field',
50-
'Form',
51-
];
52-
53-
foreach ( $factories as $factory ) {
54-
$factory_name = strtolower( preg_replace( '/\B([A-Z])/', '_$1', $factory ) );
55-
$factory_class = '\\Tests\\WPGraphQL\\GF\\Factory\\' . $factory;
66+
foreach ( self::FACTORIES as $factory_name => $factory_class ) {
5667
$this->factory->{$factory_name} = new $factory_class( $this->factory );
5768
}
5869

@@ -81,6 +92,30 @@ public function tearDown(): void {
8192
parent::tearDown();
8293
}
8394

95+
/**
96+
* Clean up all Gravity Forms data from previous test runs.
97+
*
98+
* This ensures test isolation by removing any leftover entries and forms
99+
* that may have persisted from interrupted or failed tests.
100+
*/
101+
protected function cleanup_gf_data(): void {
102+
// Delete all entries.
103+
$entries = \GFAPI::get_entries( 0 );
104+
if ( ! empty( $entries ) && ! is_wp_error( $entries ) ) {
105+
$entry_ids = wp_list_pluck( $entries, 'id' );
106+
foreach ( $entry_ids as $entry_id ) {
107+
\GFAPI::delete_entry( $entry_id );
108+
}
109+
}
110+
111+
// Delete all forms.
112+
$forms = \GFAPI::get_forms();
113+
if ( ! empty( $forms ) && ! is_wp_error( $forms ) ) {
114+
$form_ids = wp_list_pluck( $forms, 'id' );
115+
\GFAPI::delete_forms( $form_ids );
116+
}
117+
}
118+
84119
/**
85120
* Programmatically generate an expectedField array for assertions.
86121
*

0 commit comments

Comments
 (0)