@@ -1237,6 +1237,179 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
12371237 $ subscriber = $ this ->api ->get_subscriber_tags (12345 );
12381238 }
12391239
1240+ /**
1241+ * Test that get_custom_fields() returns the expected data.
1242+ *
1243+ * @since 1.0.0
1244+ *
1245+ * @return void
1246+ */
1247+ public function testGetCustomFields ()
1248+ {
1249+ $ result = $ this ->api ->get_custom_fields ();
1250+ $ this ->assertInstanceOf ('stdClass ' , $ result );
1251+ $ this ->assertArrayHasKey ('custom_fields ' , get_object_vars ($ result ));
1252+
1253+ // Inspect first custom field.
1254+ $ customField = get_object_vars ($ result ->custom_fields [0 ]);
1255+ $ this ->assertArrayHasKey ('id ' , $ customField );
1256+ $ this ->assertArrayHasKey ('name ' , $ customField );
1257+ $ this ->assertArrayHasKey ('key ' , $ customField );
1258+ $ this ->assertArrayHasKey ('label ' , $ customField );
1259+ }
1260+
1261+ /**
1262+ * Test that create_custom_field() works.
1263+ *
1264+ * @since 1.0.0
1265+ *
1266+ * @return void
1267+ */
1268+ public function testCreateCustomField ()
1269+ {
1270+ $ label = 'Custom Field ' . mt_rand ();
1271+ $ result = $ this ->api ->create_custom_field ($ label );
1272+
1273+ $ result = get_object_vars ($ result );
1274+ $ this ->assertArrayHasKey ('id ' , $ result );
1275+ $ this ->assertArrayHasKey ('name ' , $ result );
1276+ $ this ->assertArrayHasKey ('key ' , $ result );
1277+ $ this ->assertArrayHasKey ('label ' , $ result );
1278+ $ this ->assertEquals ($ result ['label ' ], $ label );
1279+
1280+ // Delete custom field.
1281+ $ this ->api ->delete_custom_field ($ result ['id ' ]);
1282+ }
1283+
1284+ /**
1285+ * Test that create_custom_field() throws a ClientException when a blank
1286+ * label is specified.
1287+ *
1288+ * @since 1.0.0
1289+ *
1290+ * @return void
1291+ */
1292+ public function testCreateCustomFieldWithBlankLabel ()
1293+ {
1294+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
1295+ $ this ->api ->create_custom_field ('' );
1296+ }
1297+
1298+ /**
1299+ * Test that create_custom_fields() works.
1300+ *
1301+ * @since 1.0.0
1302+ *
1303+ * @return void
1304+ */
1305+ public function testCreateCustomFields ()
1306+ {
1307+ $ labels = [
1308+ 'Custom Field ' . mt_rand (),
1309+ 'Custom Field ' . mt_rand (),
1310+ ];
1311+ $ result = $ this ->api ->create_custom_fields ($ labels );
1312+
1313+ // Confirm result is an array comprising of each custom field that was created.
1314+ $ this ->assertIsArray ($ result );
1315+ foreach ($ result as $ index => $ customField ) {
1316+ // Confirm individual custom field.
1317+ $ customField = get_object_vars ($ customField );
1318+ $ this ->assertArrayHasKey ('id ' , $ customField );
1319+ $ this ->assertArrayHasKey ('name ' , $ customField );
1320+ $ this ->assertArrayHasKey ('key ' , $ customField );
1321+ $ this ->assertArrayHasKey ('label ' , $ customField );
1322+
1323+ // Confirm label is correct.
1324+ $ this ->assertEquals ($ labels [$ index ], $ customField ['label ' ]);
1325+
1326+ // Delete custom field as tests passed.
1327+ $ this ->api ->delete_custom_field ($ customField ['id ' ]);
1328+ }
1329+ }
1330+
1331+ /**
1332+ * Test that update_custom_field() works.
1333+ *
1334+ * @since 1.0.0
1335+ *
1336+ * @return void
1337+ */
1338+ public function testUpdateCustomField ()
1339+ {
1340+ // Create custom field.
1341+ $ label = 'Custom Field ' . mt_rand ();
1342+ $ result = $ this ->api ->create_custom_field ($ label );
1343+ $ id = $ result ->id ;
1344+
1345+ // Change label.
1346+ $ newLabel = 'Custom Field ' . mt_rand ();
1347+ $ this ->api ->update_custom_field ($ id , $ newLabel );
1348+
1349+ // Confirm label changed.
1350+ $ customFields = $ this ->api ->get_custom_fields ();
1351+ foreach ($ customFields ->custom_fields as $ customField ) {
1352+ if ($ customField ->id === $ id ) {
1353+ $ this ->assertEquals ($ customField ->label , $ newLabel );
1354+ }
1355+ }
1356+
1357+ // Delete custom field as tests passed.
1358+ $ this ->api ->delete_custom_field ($ id );
1359+ }
1360+
1361+ /**
1362+ * Test that update_custom_field() throws a ClientException when an
1363+ * invalid custom field ID is specified.
1364+ *
1365+ * @since 1.0.0
1366+ *
1367+ * @return void
1368+ */
1369+ public function testUpdateCustomFieldWithInvalidID ()
1370+ {
1371+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
1372+ $ this ->api ->update_custom_field (12345 , 'Something ' );
1373+ }
1374+
1375+ /**
1376+ * Test that delete_custom_field() works.
1377+ *
1378+ * @since 1.0.0
1379+ *
1380+ * @return void
1381+ */
1382+ public function testDeleteCustomField ()
1383+ {
1384+ // Create custom field.
1385+ $ label = 'Custom Field ' . mt_rand ();
1386+ $ result = $ this ->api ->create_custom_field ($ label );
1387+ $ id = $ result ->id ;
1388+
1389+ // Delete custom field as tests passed.
1390+ $ this ->api ->delete_custom_field ($ id );
1391+
1392+ // Confirm custom field no longer exists.
1393+ $ customFields = $ this ->api ->get_custom_fields ();
1394+ foreach ($ customFields ->custom_fields as $ customField ) {
1395+ $ this ->assertNotEquals ($ customField ->id , $ id );
1396+ }
1397+ }
1398+
1399+ /**
1400+ * Test that delete_custom_field() throws a ClientException when an
1401+ * invalid custom field ID is specified.
1402+ *
1403+ * @since 1.0.0
1404+ *
1405+ * @return void
1406+ */
1407+ public function testDeleteCustomFieldWithInvalidID ()
1408+ {
1409+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
1410+ $ this ->api ->delete_custom_field (12345 );
1411+ }
1412+
12401413 /**
12411414 * Test that list_purchases() returns the expected data.
12421415 *
0 commit comments