@@ -693,6 +693,179 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
693693 $ subscriber = $ this ->api ->get_subscriber_tags (12345 );
694694 }
695695
696+ /**
697+ * Test that get_custom_fields() returns the expected data.
698+ *
699+ * @since 1.0.0
700+ *
701+ * @return void
702+ */
703+ public function testGetCustomFields ()
704+ {
705+ $ result = $ this ->api ->get_custom_fields ();
706+ $ this ->assertInstanceOf ('stdClass ' , $ result );
707+ $ this ->assertArrayHasKey ('custom_fields ' , get_object_vars ($ result ));
708+
709+ // Inspect first custom field.
710+ $ customField = get_object_vars ($ result ->custom_fields [0 ]);
711+ $ this ->assertArrayHasKey ('id ' , $ customField );
712+ $ this ->assertArrayHasKey ('name ' , $ customField );
713+ $ this ->assertArrayHasKey ('key ' , $ customField );
714+ $ this ->assertArrayHasKey ('label ' , $ customField );
715+ }
716+
717+ /**
718+ * Test that create_custom_field() works.
719+ *
720+ * @since 1.0.0
721+ *
722+ * @return void
723+ */
724+ public function testCreateCustomField ()
725+ {
726+ $ label = 'Custom Field ' . mt_rand ();
727+ $ result = $ this ->api ->create_custom_field ($ label );
728+
729+ $ result = get_object_vars ($ result );
730+ $ this ->assertArrayHasKey ('id ' , $ result );
731+ $ this ->assertArrayHasKey ('name ' , $ result );
732+ $ this ->assertArrayHasKey ('key ' , $ result );
733+ $ this ->assertArrayHasKey ('label ' , $ result );
734+ $ this ->assertEquals ($ result ['label ' ], $ label );
735+
736+ // Delete custom field.
737+ $ this ->api ->delete_custom_field ($ result ['id ' ]);
738+ }
739+
740+ /**
741+ * Test that create_custom_field() throws a ClientException when a blank
742+ * label is specified.
743+ *
744+ * @since 1.0.0
745+ *
746+ * @return void
747+ */
748+ public function testCreateCustomFieldWithBlankLabel ()
749+ {
750+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
751+ $ this ->api ->create_custom_field ('' );
752+ }
753+
754+ /**
755+ * Test that create_custom_fields() works.
756+ *
757+ * @since 1.0.0
758+ *
759+ * @return void
760+ */
761+ public function testCreateCustomFields ()
762+ {
763+ $ labels = [
764+ 'Custom Field ' . mt_rand (),
765+ 'Custom Field ' . mt_rand (),
766+ ];
767+ $ result = $ this ->api ->create_custom_fields ($ labels );
768+
769+ // Confirm result is an array comprising of each custom field that was created.
770+ $ this ->assertIsArray ($ result );
771+ foreach ($ result as $ index => $ customField ) {
772+ // Confirm individual custom field.
773+ $ customField = get_object_vars ($ customField );
774+ $ this ->assertArrayHasKey ('id ' , $ customField );
775+ $ this ->assertArrayHasKey ('name ' , $ customField );
776+ $ this ->assertArrayHasKey ('key ' , $ customField );
777+ $ this ->assertArrayHasKey ('label ' , $ customField );
778+
779+ // Confirm label is correct.
780+ $ this ->assertEquals ($ labels [$ index ], $ customField ['label ' ]);
781+
782+ // Delete custom field as tests passed.
783+ $ this ->api ->delete_custom_field ($ customField ['id ' ]);
784+ }
785+ }
786+
787+ /**
788+ * Test that update_custom_field() works.
789+ *
790+ * @since 1.0.0
791+ *
792+ * @return void
793+ */
794+ public function testUpdateCustomField ()
795+ {
796+ // Create custom field.
797+ $ label = 'Custom Field ' . mt_rand ();
798+ $ result = $ this ->api ->create_custom_field ($ label );
799+ $ id = $ result ->id ;
800+
801+ // Change label.
802+ $ newLabel = 'Custom Field ' . mt_rand ();
803+ $ this ->api ->update_custom_field ($ id , $ newLabel );
804+
805+ // Confirm label changed.
806+ $ customFields = $ this ->api ->get_custom_fields ();
807+ foreach ($ customFields ->custom_fields as $ customField ) {
808+ if ($ customField ->id === $ id ) {
809+ $ this ->assertEquals ($ customField ->label , $ newLabel );
810+ }
811+ }
812+
813+ // Delete custom field as tests passed.
814+ $ this ->api ->delete_custom_field ($ id );
815+ }
816+
817+ /**
818+ * Test that update_custom_field() throws a ClientException when an
819+ * invalid custom field ID is specified.
820+ *
821+ * @since 1.0.0
822+ *
823+ * @return void
824+ */
825+ public function testUpdateCustomFieldWithInvalidID ()
826+ {
827+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
828+ $ this ->api ->update_custom_field (12345 , 'Something ' );
829+ }
830+
831+ /**
832+ * Test that delete_custom_field() works.
833+ *
834+ * @since 1.0.0
835+ *
836+ * @return void
837+ */
838+ public function testDeleteCustomField ()
839+ {
840+ // Create custom field.
841+ $ label = 'Custom Field ' . mt_rand ();
842+ $ result = $ this ->api ->create_custom_field ($ label );
843+ $ id = $ result ->id ;
844+
845+ // Delete custom field as tests passed.
846+ $ this ->api ->delete_custom_field ($ id );
847+
848+ // Confirm custom field no longer exists.
849+ $ customFields = $ this ->api ->get_custom_fields ();
850+ foreach ($ customFields ->custom_fields as $ customField ) {
851+ $ this ->assertNotEquals ($ customField ->id , $ id );
852+ }
853+ }
854+
855+ /**
856+ * Test that delete_custom_field() throws a ClientException when an
857+ * invalid custom field ID is specified.
858+ *
859+ * @since 1.0.0
860+ *
861+ * @return void
862+ */
863+ public function testDeleteCustomFieldWithInvalidID ()
864+ {
865+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
866+ $ this ->api ->delete_custom_field (12345 );
867+ }
868+
696869 /**
697870 * Test that list_purchases() returns the expected data.
698871 *
0 commit comments