@@ -898,19 +898,19 @@ public function testGetResourcesInvalidResourceType()
898
898
}
899
899
900
900
/**
901
- * Test that form_subscribe () returns the expected data.
901
+ * Test that add_subscriber_to_form () returns the expected data.
902
902
*
903
903
* @since 1.0.0
904
904
*
905
905
* @return void
906
906
*/
907
- public function testFormSubscribe ()
907
+ public function testAddSubscriberToForm ()
908
908
{
909
- // Subscribe.
910
909
$ 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
+ );
914
914
$ this ->assertInstanceOf ('stdClass ' , $ result );
915
915
$ this ->assertArrayHasKey ('subscription ' , get_object_vars ($ result ));
916
916
$ this ->assertArrayHasKey ('id ' , get_object_vars ($ result ->subscription ));
@@ -921,19 +921,115 @@ public function testFormSubscribe()
921
921
}
922
922
923
923
/**
924
- * Test that form_subscribe () throws a ClientException when an invalid
924
+ * Test that add_subscriber_to_form () throws a ClientException when an invalid
925
925
* form ID is specified.
926
926
*
927
927
* @since 1.0.0
928
928
*
929
929
* @return void
930
930
*/
931
- public function testFormSubscribeWithInvalidFormID ()
931
+ public function testAddSubscriberToFormWithInvalidFormID ()
932
932
{
933
933
$ 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 ' ]);
937
1033
}
938
1034
939
1035
/**
0 commit comments