Source of file SparkPostTest.php
Size: 2,945 Bytes - Last Modified: 2021-12-23T10:01:31+00:00
/var/www/docs.ssmods.com/process/src/tests/SparkPostTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 | <?php namespace LeKoala\SparkPost\Test; use SilverStripe\Dev\SapphireTest; use SilverStripe\Core\Environment; use SilverStripe\Control\Email\Email; use LeKoala\SparkPost\SparkPostHelper; use SilverStripe\Control\Email\Mailer; use SilverStripe\Core\Injector\Injector; use SilverStripe\Control\Email\SwiftMailer; /** * Test for SparkPost * * @group SparkPost */ class SparkPostTest extends SapphireTest { protected $testMailer; protected function setUp() { parent::setUp(); $this->testMailer = Injector::inst()->get(Mailer::class); // Ensure we have the right mailer $mailer = new SwiftMailer(); $swiftMailer = new \Swift_Mailer(new \Swift_MailTransport()); $mailer->setSwiftMailer($swiftMailer); Injector::inst()->registerService($mailer, Mailer::class); } protected function tearDown() { parent::tearDown(); Injector::inst()->registerService($this->testMailer, Mailer::class); } public function testSetup() { if (!SparkPostHelper::getApiKey()) { return $this->markTestIncomplete("No api key set for test"); } $inst = SparkPostHelper::registerTransport(); $mailer = SparkPostHelper::getMailer(); $this->assertTrue($inst === $mailer); } public function testClient() { if (!SparkPostHelper::getApiKey()) { return $this->markTestIncomplete("No api key set for test"); } $client = SparkPostHelper::getClient(); $result = $client->listAllSendingDomains(); $this->assertTrue(is_array($result)); } public function testTLSVersion() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.howsmyssl.com/a/check'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); if (!$data) { $this->markTestIncomplete('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch)); return; } curl_close($ch); $json = json_decode($data); $this->assertNotEquals("TLS 1.0", $json->tls_version); } public function testSending() { if (!SparkPostHelper::getApiKey()) { return $this->markTestIncomplete("No api key set for test"); } $test_to = Environment::getEnv('SPARKPOST_TEST_TO'); $test_from = Environment::getEnv('SPARKPOST_TEST_FROM'); if (!$test_from || !$test_to) { $this->markTestIncomplete("You must define tests environement variable: SPARKPOST_TEST_TO, SPARKPOST_TEST_FROM"); } $inst = SparkPostHelper::registerTransport(); $email = new Email(); $email->setTo($test_to); $email->setSubject('Test email'); $email->setBody("Body of my email"); $email->setFrom($test_from); $sent = $email->send(); $this->assertTrue(!!$sent); } } |