Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Bounce_Handler_Test | |
0.00% |
0 / 41 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
| get_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_email | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| run_test | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| verify_test | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| delete_test_data | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * An object for orchestrating tests, holding test data and verifing tests. |
| 4 | * |
| 5 | * @link https://BrianHenry.ie |
| 6 | * @since 1.0.0 |
| 7 | * |
| 8 | * @package brianhenryie/bh-wp-aws-ses-bounce-handler |
| 9 | */ |
| 10 | |
| 11 | namespace BrianHenryIE\AWS_SES_Bounce_Handler\Admin; |
| 12 | |
| 13 | use BrianHenryIE\AWS_SES_Bounce_Handler\API_Interface; |
| 14 | use BrianHenryIE\AWS_SES_Bounce_Handler\API\SES_Bounce_Handler_Integration_Interface; |
| 15 | use Psr\Log\LoggerAwareTrait; |
| 16 | use Psr\Log\LoggerInterface; |
| 17 | |
| 18 | /** |
| 19 | * Create a uid, bounce simulator email address, setup integrations, save the data, verify tests, delete data. |
| 20 | */ |
| 21 | class Bounce_Handler_Test { |
| 22 | |
| 23 | use LoggerAwareTrait; |
| 24 | |
| 25 | protected API_Interface $api; |
| 26 | |
| 27 | /** |
| 28 | * Uid for referencing the test. Created from time(). |
| 29 | * Public for saving. |
| 30 | * |
| 31 | * @var int |
| 32 | */ |
| 33 | public $id; |
| 34 | |
| 35 | /** |
| 36 | * The bounce simulator email address being used. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $email; |
| 41 | |
| 42 | /** |
| 43 | * Array of arrays of test data from the integrations, for saving. |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | public array $test_data = array(); |
| 48 | |
| 49 | /** |
| 50 | * An id for the test. Made from the timestamp. |
| 51 | * |
| 52 | * @return int |
| 53 | */ |
| 54 | public function get_id() { |
| 55 | return $this->id; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * AWS SES bounce simulator email address. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function get_email() { |
| 64 | return $this->email; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Use time() to create a uid for creating a bounce simulator email address. |
| 69 | * |
| 70 | * Bounce_Handler_Test constructor. |
| 71 | */ |
| 72 | public function __construct( API_Interface $api, LoggerInterface $logger ) { |
| 73 | |
| 74 | $this->logger = $logger; |
| 75 | $this->api = $api; |
| 76 | |
| 77 | $this->id = time(); |
| 78 | $this->email = "bounce+{$this->id}@simulator.amazonses.com"; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Starts each integration's test, returns html to be output to the user. |
| 84 | * |
| 85 | * @return array {string: html, string: message} |
| 86 | */ |
| 87 | public function run_test() { |
| 88 | |
| 89 | $data = array(); |
| 90 | $data['html'] = ''; |
| 91 | |
| 92 | $data['html'] .= '<p>Test started at time: <em>' . $this->get_id() . '</em></p>'; |
| 93 | $data['html'] .= '<p>Using email address: <em>' . $this->get_email() . '</em></p>'; |
| 94 | |
| 95 | foreach ( $this->api->get_integrations() as $name => $integration ) { |
| 96 | |
| 97 | if ( ! $integration->is_enabled() ) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | $test_setup = $integration->setup_test( $this ); |
| 102 | |
| 103 | $this->test_data[ $name ] = $test_setup['data']; |
| 104 | |
| 105 | $data['html'] .= $test_setup['html']; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | $to = $this->get_email(); |
| 110 | $subject = 'BH WP AWS SES Bounce Handler Test Email'; |
| 111 | $message = 'BH WP AWS SES Bounce Handler Test Email'; |
| 112 | |
| 113 | $mail_send = wp_mail( $to, $subject, $message ); |
| 114 | |
| 115 | if ( ! $mail_send ) { |
| 116 | |
| 117 | $data['message'] = 'wp_mail() failed'; |
| 118 | wp_send_json_error( $data, 500 ); |
| 119 | } |
| 120 | |
| 121 | $data['html'] .= '<p>Test email sent to: <em>' . $this->get_email() . '</em></p>'; |
| 122 | |
| 123 | return $data; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Checks with each integration if the expected changes have occurred. |
| 129 | * |
| 130 | * @return array {bool: testSuccess, string: html} |
| 131 | */ |
| 132 | public function verify_test() { |
| 133 | |
| 134 | $integrations = $this->api->get_integrations(); |
| 135 | |
| 136 | $results_data = array(); |
| 137 | $results_data['html'] = ''; |
| 138 | $results_data['testSuccess'] = true; |
| 139 | |
| 140 | $this->logger->debug( json_encode( $this->test_data ) ); |
| 141 | $this->logger->debug( ' test data is an array ' . ( is_array( $this->test_data ) ? 'yes' : 'no' ) ); |
| 142 | |
| 143 | foreach ( $this->test_data as $name => $test_data ) { |
| 144 | |
| 145 | $this->logger->debug( $name . ' ' . json_encode( $test_data ) ); |
| 146 | |
| 147 | $test_verify = $integrations[ $name ]->verify_test( $test_data ); |
| 148 | $results_data['html'] .= $test_verify['html']; |
| 149 | if ( false === $test_verify['success'] ) { |
| 150 | $results_data['testSuccess'] = false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return $results_data; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Passes test data to integrations to delete. |
| 160 | */ |
| 161 | public function delete_test_data() { |
| 162 | |
| 163 | $integrations = $this->api->get_integrations(); |
| 164 | |
| 165 | foreach ( $this->test_data as $name => $data ) { |
| 166 | |
| 167 | $integrations[ $name ]->delete_test_data( $data ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | } |