@@ -898,19 +898,19 @@ public function testGetResourcesInvalidResourceType()
898898 }
899899
900900 /**
901- * Test that form_subscribe () returns the expected data.
901+ * Test that add_subscriber_to_form () returns the expected data.
902902 *
903903 * @since 1.0.0
904904 *
905905 * @return void
906906 */
907- public function testFormSubscribe ()
907+ public function testAddSubscriberToForm ()
908908 {
909- // Subscribe.
910909 $ email = $ this ->generateEmailAddress ();
911- $ result = $ this ->api ->form_subscribe ((int ) $ _ENV ['CONVERTKIT_API_FORM_ID ' ], [
912- 'email ' => $ email ,
913- ]);
910+ $ result = $ this ->api ->add_subscriber_to_form (
911+ (int ) $ _ENV ['CONVERTKIT_API_FORM_ID ' ],
912+ $ email
913+ );
914914 $ this ->assertInstanceOf ('stdClass ' , $ result );
915915 $ this ->assertArrayHasKey ('subscription ' , get_object_vars ($ result ));
916916 $ this ->assertArrayHasKey ('id ' , get_object_vars ($ result ->subscription ));
@@ -921,19 +921,115 @@ public function testFormSubscribe()
921921 }
922922
923923 /**
924- * Test that form_subscribe () throws a ClientException when an invalid
924+ * Test that add_subscriber_to_form () throws a ClientException when an invalid
925925 * form ID is specified.
926926 *
927927 * @since 1.0.0
928928 *
929929 * @return void
930930 */
931- public function testFormSubscribeWithInvalidFormID ()
931+ public function testAddSubscriberToFormWithInvalidFormID ()
932932 {
933933 $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
934- $ result = $ this ->api ->form_subscribe (12345 , [
935- 'email ' => $ this ->generateEmailAddress (),
936- ]);
934+ $ result = $ this ->api ->add_subscriber_to_form (12345 , $ this ->generateEmailAddress ());
935+ }
936+
937+ /**
938+ * Test that add_subscriber_to_form() throws a ClientException when an invalid
939+ * email address is specified.
940+ *
941+ * @since 1.0.0
942+ *
943+ * @return void
944+ */
945+ public function testAddSubscriberToFormWithInvalidEmailAddress ()
946+ {
947+ $ this ->expectException (GuzzleHttp \Exception \ClientException::class);
948+ $ result = $ this ->api ->add_subscriber_to_form ($ _ENV ['CONVERTKIT_API_FORM_ID ' ], 'not-an-email-address ' );
949+ }
950+
951+ /**
952+ * Test that add_subscriber_to_form() returns the expected data
953+ * when a first_name parameter is included.
954+ *
955+ * @since 1.0.0
956+ *
957+ * @return void
958+ */
959+ public function testAddSubscriberToFormWithFirstName ()
960+ {
961+ $ emailAddress = $ this ->generateEmailAddress ();
962+ $ firstName = 'First Name ' ;
963+ $ result = $ this ->api ->add_subscriber_to_form (
964+ $ _ENV ['CONVERTKIT_API_FORM_ID ' ],
965+ $ emailAddress ,
966+ $ firstName
967+ );
968+
969+ $ this ->assertInstanceOf ('stdClass ' , $ result );
970+ $ this ->assertArrayHasKey ('subscription ' , get_object_vars ($ result ));
971+
972+ // Fetch subscriber from API to confirm the first name was saved.
973+ $ subscriber = $ this ->api ->get_subscriber ($ result ->subscription ->subscriber ->id );
974+ $ this ->assertEquals ($ subscriber ->subscriber ->email_address , $ emailAddress );
975+ $ this ->assertEquals ($ subscriber ->subscriber ->first_name , $ firstName );
976+ }
977+
978+ /**
979+ * Test that add_subscriber_to_form() returns the expected data
980+ * when custom field data is included.
981+ *
982+ * @since 1.0.0
983+ *
984+ * @return void
985+ */
986+ public function testAddSubscriberToFormWithCustomFields ()
987+ {
988+ $ result = $ this ->api ->add_subscriber_to_form (
989+ $ _ENV ['CONVERTKIT_API_FORM_ID ' ],
990+ $ this ->generateEmailAddress (),
991+ 'First Name ' ,
992+ [
993+ 'last_name ' => 'Last Name ' ,
994+ ]
995+ );
996+
997+ // Check subscription object returned.
998+ $ this ->assertInstanceOf ('stdClass ' , $ result );
999+ $ this ->assertArrayHasKey ('subscription ' , get_object_vars ($ result ));
1000+
1001+ // Fetch subscriber from API to confirm the custom fields were saved.
1002+ $ subscriber = $ this ->api ->get_subscriber ($ result ->subscription ->subscriber ->id );
1003+ $ this ->assertEquals ($ subscriber ->subscriber ->fields ->last_name , 'Last Name ' );
1004+ }
1005+
1006+ /**
1007+ * Test that add_subscriber_to_form() returns the expected data
1008+ * when custom field data is included.
1009+ *
1010+ * @since 1.0.0
1011+ *
1012+ * @return void
1013+ */
1014+ public function testAddSubscriberToFormWithTagID ()
1015+ {
1016+ $ result = $ this ->api ->add_subscriber_to_form (
1017+ $ _ENV ['CONVERTKIT_API_FORM_ID ' ],
1018+ $ this ->generateEmailAddress (),
1019+ 'First Name ' ,
1020+ [],
1021+ [
1022+ (int ) $ _ENV ['CONVERTKIT_API_TAG_ID ' ]
1023+ ]
1024+ );
1025+
1026+ // Check subscription object returned.
1027+ $ this ->assertInstanceOf ('stdClass ' , $ result );
1028+ $ this ->assertArrayHasKey ('subscription ' , get_object_vars ($ result ));
1029+
1030+ // Fetch subscriber tags from API to confirm the tag saved.
1031+ $ subscriberTags = $ this ->api ->get_subscriber_tags ($ result ->subscription ->subscriber ->id );
1032+ $ this ->assertEquals ($ subscriberTags ->tags [0 ]->id , $ _ENV ['CONVERTKIT_API_TAG_ID ' ]);
9371033 }
9381034
9391035 /**
0 commit comments