|
2 | 2 |
|
3 | 3 | namespace Give\Tests\Unit\API\REST\V3\Routes\Campaigns; |
4 | 4 |
|
| 5 | +use DateTime; |
5 | 6 | use Exception; |
6 | 7 | use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute; |
7 | 8 | use Give\Campaigns\Models\Campaign; |
8 | 9 | use Give\Donations\Models\Donation; |
| 10 | +use Give\Donations\ValueObjects\DonationStatus; |
9 | 11 | use WP_REST_Server; |
10 | 12 | use Give\Tests\RestApiTestCase; |
11 | 13 | use Give\Tests\TestTraits\RefreshDatabase; |
12 | 14 | use Give\Tests\TestTraits\HasDefaultWordPressUsers; |
13 | 15 | use Give\Tests\Unit\API\REST\V3\SchemaValidationTrait; |
14 | 16 |
|
| 17 | +/** |
| 18 | + * @unreleased |
| 19 | + */ |
15 | 20 | class CampaignSchemaTest extends RestApiTestCase |
16 | 21 | { |
17 | 22 | use RefreshDatabase; |
@@ -52,4 +57,111 @@ public function testCampaignSchemaShouldMatchActualResponse() |
52 | 57 | // Validate enum values match schema |
53 | 58 | $this->validateEnumValues($schema, $actualData); |
54 | 59 | } |
| 60 | + |
| 61 | + /** |
| 62 | + * @unreleased |
| 63 | + */ |
| 64 | + public function testCampaignCollectionSchemaShouldMatchActualResponse() |
| 65 | + { |
| 66 | + /** @var Campaign[] $campaigns */ |
| 67 | + $campaign = Campaign::factory()->count(3)->create(); |
| 68 | + |
| 69 | + // Get the schema via OPTIONS request |
| 70 | + $schemaRoute = '/' . CampaignRoute::NAMESPACE . '/' . CampaignRoute::CAMPAIGNS; |
| 71 | + $schemaRequest = $this->createRequest('OPTIONS', $schemaRoute, [], 'administrator'); |
| 72 | + $schemaResponse = $this->dispatchRequest($schemaRequest); |
| 73 | + $schemaJson = json_encode($schemaResponse->get_data()); |
| 74 | + $schema = json_decode($schemaJson, true); |
| 75 | + |
| 76 | + // Get actual collection data |
| 77 | + $dataRoute = '/' . CampaignRoute::NAMESPACE . '/' . CampaignRoute::CAMPAIGNS; |
| 78 | + $dataRequest = $this->createRequest(WP_REST_Server::READABLE, $dataRoute, [], 'administrator'); |
| 79 | + $dataResponse = $this->dispatchRequest($dataRequest); |
| 80 | + $actualDataJson = json_encode($dataResponse->get_data()); |
| 81 | + $actualData = json_decode($actualDataJson, true); |
| 82 | + |
| 83 | + // Assert that we have data in the collection |
| 84 | + $this->assertNotEmpty($actualData, 'Collection should contain at least one campaign'); |
| 85 | + $this->assertIsArray($actualData, 'Collection should be an array'); |
| 86 | + |
| 87 | + // Validate first item in collection |
| 88 | + if (!empty($actualData)) { |
| 89 | + $firstItem = $actualData[0]; |
| 90 | + $this->validateSchemaProperties($schema, $firstItem); |
| 91 | + $this->validateDataTypes($schema, $firstItem); |
| 92 | + $this->validateEnumValues($schema, $firstItem); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @unreleased |
| 98 | + */ |
| 99 | + public function testCampaignStatisticsSchemaShouldMatchActualResponse() |
| 100 | + { |
| 101 | + $campaign = Campaign::factory()->create(); |
| 102 | + $donations = Donation::factory()->count(3)->create([ |
| 103 | + 'campaignId' => $campaign->id, |
| 104 | + 'formId' => $campaign->defaultFormId, |
| 105 | + 'status' => DonationStatus::COMPLETE(), |
| 106 | + ]); |
| 107 | + |
| 108 | + // Get the schema via OPTIONS request |
| 109 | + $schemaRoute = '/' . CampaignRoute::NAMESPACE . '/' . CampaignRoute::CAMPAIGNS . '/' . $campaign->id . '/statistics'; |
| 110 | + $schemaRequest = $this->createRequest('OPTIONS', $schemaRoute, [], 'administrator'); |
| 111 | + $schemaResponse = $this->dispatchRequest($schemaRequest); |
| 112 | + $schemaJson = json_encode($schemaResponse->get_data()); |
| 113 | + $schema = json_decode($schemaJson, true); |
| 114 | + |
| 115 | + // Get actual campaign statistics data |
| 116 | + $dataRoute = '/' . CampaignRoute::NAMESPACE . '/' . CampaignRoute::CAMPAIGNS . '/' . $campaign->id . '/statistics'; |
| 117 | + $dataRequest = $this->createRequest(WP_REST_Server::READABLE, $dataRoute, [], 'administrator'); |
| 118 | + $dataResponse = $this->dispatchRequest($dataRequest); |
| 119 | + $actualDataJson = json_encode($dataResponse->get_data()); |
| 120 | + $actualData = json_decode($actualDataJson, true); |
| 121 | + |
| 122 | + // Validate that all required schema properties exist in actual response |
| 123 | + $this->validateSchemaProperties($schema, $actualData); |
| 124 | + |
| 125 | + // Validate data types match schema |
| 126 | + $this->validateDataTypes($schema, $actualData); |
| 127 | + |
| 128 | + // Validate enum values match schema |
| 129 | + $this->validateEnumValues($schema, $actualData); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @unreleased |
| 134 | + */ |
| 135 | + public function testCampaignRevenueSchemaShouldMatchActualResponse() |
| 136 | + { |
| 137 | + $campaign = Campaign::factory()->create(); |
| 138 | + $donations = Donation::factory()->count(3)->create([ |
| 139 | + 'campaignId' => $campaign->id, |
| 140 | + 'formId' => $campaign->defaultFormId, |
| 141 | + 'status' => DonationStatus::COMPLETE(), |
| 142 | + ]); |
| 143 | + |
| 144 | + // Get the schema via OPTIONS request |
| 145 | + $schemaRoute = '/' . CampaignRoute::NAMESPACE . '/' . CampaignRoute::CAMPAIGNS . '/' . $campaign->id . '/revenue'; |
| 146 | + $schemaRequest = $this->createRequest('OPTIONS', $schemaRoute, [], 'administrator'); |
| 147 | + $schemaResponse = $this->dispatchRequest($schemaRequest); |
| 148 | + $schemaJson = json_encode($schemaResponse->get_data()); |
| 149 | + $schema = json_decode($schemaJson, true); |
| 150 | + |
| 151 | + // Get actual campaign revenue data |
| 152 | + $dataRoute = '/' . CampaignRoute::NAMESPACE . '/' . CampaignRoute::CAMPAIGNS . '/' . $campaign->id . '/revenue'; |
| 153 | + $dataRequest = $this->createRequest(WP_REST_Server::READABLE, $dataRoute, [], 'administrator'); |
| 154 | + $dataResponse = $this->dispatchRequest($dataRequest); |
| 155 | + $actualDataJson = json_encode($dataResponse->get_data()); |
| 156 | + $actualData = json_decode($actualDataJson, true); |
| 157 | + |
| 158 | + // Validate that all required schema properties exist in actual response |
| 159 | + $this->validateSchemaProperties($schema, $actualData); |
| 160 | + |
| 161 | + // Validate data types match schema |
| 162 | + $this->validateDataTypes($schema, $actualData); |
| 163 | + |
| 164 | + // Validate enum values match schema |
| 165 | + $this->validateEnumValues($schema, $actualData); |
| 166 | + } |
55 | 167 | } |
0 commit comments