MailerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php namespace GO\Job\Tests;
  2. use GO\Job;
  3. use PHPUnit\Framework\TestCase;
  4. class MailerTest extends TestCase
  5. {
  6. public function testShouldHaveDefaultConfigToSendAnEmail()
  7. {
  8. $job = new Job('ls');
  9. $config = $job->getEmailConfig();
  10. $this->assertTrue(isset($config['subject']));
  11. $this->assertTrue(isset($config['from']));
  12. $this->assertTrue(isset($config['body']));
  13. $this->assertTrue(isset($config['transport']));
  14. }
  15. public function testShouldAllowCustomTransportWhenSendingEmails()
  16. {
  17. $job = new Job(function () {
  18. return 'hi';
  19. });
  20. $job->configure([
  21. 'email' => [
  22. 'transport' => new \Swift_NullTransport(),
  23. ],
  24. ]);
  25. $this->assertInstanceOf(\Swift_NullTransport::class, $job->getEmailConfig()['transport']);
  26. }
  27. public function testEmailTransportShouldAlwaysBeInstanceOfSwift_Transport()
  28. {
  29. $job = new Job(function () {
  30. return 'hi';
  31. });
  32. $job->configure([
  33. 'email' => [
  34. 'transport' => 'Something not allowed',
  35. ],
  36. ]);
  37. $this->assertInstanceOf(\Swift_Transport::class, $job->getEmailConfig()['transport']);
  38. }
  39. public function testShouldSendJobOutputToEmail()
  40. {
  41. $emailAddress = 'local@localhost.com';
  42. $command = PHP_BINARY . ' ' . __DIR__ . '/../test_job.php';
  43. $job1 = new Job($command);
  44. $job2 = new Job(function () {
  45. return 'Hello World!';
  46. });
  47. $nullTransportConfig = [
  48. 'email' => [
  49. 'transport' => new \Swift_NullTransport(),
  50. ],
  51. ];
  52. $job1->configure($nullTransportConfig);
  53. $job2->configure($nullTransportConfig);
  54. $outputFile1 = __DIR__ . '/../tmp/output001.log';
  55. $this->assertTrue($job1->output($outputFile1)->email($emailAddress)->run());
  56. $outputFile2 = __DIR__ . '/../tmp/output002.log';
  57. $this->assertTrue($job2->output($outputFile2)->email($emailAddress)->run());
  58. unlink($outputFile1);
  59. unlink($outputFile2);
  60. }
  61. public function testShouldSendMultipleFilesToEmail()
  62. {
  63. $emailAddress = 'local@localhost.com';
  64. $command = PHP_BINARY . ' ' . __DIR__ . '/../async_job.php';
  65. $job = new Job($command);
  66. $outputFile1 = __DIR__ . '/../tmp/output003.log';
  67. $outputFile2 = __DIR__ . '/../tmp/output004.log';
  68. $nullTransportConfig = [
  69. 'email' => [
  70. 'transport' => new \Swift_NullTransport(),
  71. ],
  72. ];
  73. $job->configure($nullTransportConfig);
  74. $this->assertTrue($job->output([
  75. $outputFile1, $outputFile2,
  76. ])->email([$emailAddress])->run());
  77. unlink($outputFile1);
  78. unlink($outputFile2);
  79. }
  80. public function testShouldSendToMultipleEmails()
  81. {
  82. $emailAddress1 = 'local@localhost.com';
  83. $emailAddress2 = 'local1@localhost.com';
  84. $command = PHP_BINARY . ' ' . __DIR__ . '/../async_job.php';
  85. $job = new Job($command);
  86. $outputFile = __DIR__ . '/../tmp/output005.log';
  87. $nullTransportConfig = [
  88. 'email' => [
  89. 'transport' => new \Swift_NullTransport(),
  90. ],
  91. ];
  92. $job->configure($nullTransportConfig);
  93. $this->assertTrue($job->output($outputFile)->email([
  94. $emailAddress1, $emailAddress2,
  95. ])->run());
  96. unlink($outputFile);
  97. }
  98. public function testShouldAcceptCustomEmailConfig()
  99. {
  100. $emailAddress = 'local@localhost.com';
  101. $command = PHP_BINARY . ' ' . __DIR__ . '/../async_job.php';
  102. $job = new Job($command);
  103. $outputFile = __DIR__ . '/../tmp/output6.log';
  104. $this->assertTrue(
  105. $job->output($outputFile)->email($emailAddress)
  106. ->configure([
  107. 'email' => [
  108. 'subject' => 'My custom subject',
  109. 'from' => 'my@custom.from',
  110. 'body' => 'My custom body',
  111. 'transport' => new \Swift_NullTransport(),
  112. ],
  113. ])->run()
  114. );
  115. unlink($outputFile);
  116. }
  117. public function testShouldIgnoreEmailIfSpecifiedInConfig()
  118. {
  119. $job = new Job(function () {
  120. $tot = 1 + 2;
  121. // Return nothing....
  122. });
  123. $nullTransportConfig = [
  124. 'email' => [
  125. 'transport' => new \Swift_NullTransport(),
  126. 'ignore_empty_output' => true,
  127. ],
  128. ];
  129. $job->configure($nullTransportConfig);
  130. $outputFile = __DIR__ . '/../tmp/output.log';
  131. $this->assertTrue($job->output($outputFile)->email('local@localhost.com')->run());
  132. @unlink($outputFile);
  133. }
  134. }