TransportFactoryTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Mailer;
  17. use BadMethodCallException;
  18. use Cake\Mailer\Transport\DebugTransport;
  19. use Cake\Mailer\TransportFactory;
  20. use Cake\TestSuite\TestCase;
  21. use InvalidArgumentException;
  22. /**
  23. * TransportFactory Test class
  24. */
  25. class TransportFactoryTest extends TestCase
  26. {
  27. /**
  28. * @var array
  29. */
  30. protected $transports;
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. $this->transports = [
  35. 'debug' => [
  36. 'className' => 'Debug',
  37. ],
  38. 'badClassName' => [
  39. 'className' => 'TestFalse',
  40. ],
  41. ];
  42. TransportFactory::setConfig($this->transports);
  43. }
  44. /**
  45. * tearDown method
  46. */
  47. public function tearDown(): void
  48. {
  49. parent::tearDown();
  50. TransportFactory::drop('debug');
  51. TransportFactory::drop('badClassName');
  52. TransportFactory::drop('test_smtp');
  53. }
  54. /**
  55. * Test that using misconfigured transports fails.
  56. */
  57. public function testGetMissingClassName(): void
  58. {
  59. $this->expectException(InvalidArgumentException::class);
  60. $this->expectExceptionMessage('Transport config `debug` is invalid, the required `className` option is missing');
  61. TransportFactory::drop('debug');
  62. TransportFactory::setConfig('debug', []);
  63. TransportFactory::get('debug');
  64. }
  65. /**
  66. * Test configuring a transport.
  67. */
  68. public function testSetConfig(): void
  69. {
  70. $settings = [
  71. 'className' => 'Debug',
  72. 'log' => true,
  73. ];
  74. TransportFactory::drop('debug');
  75. TransportFactory::setConfig('debug', $settings);
  76. $result = TransportFactory::getConfig('debug');
  77. $this->assertEquals($settings, $result);
  78. }
  79. /**
  80. * Test configuring multiple transports.
  81. */
  82. public function testSetConfigMultiple(): void
  83. {
  84. $settings = [
  85. 'debug' => [
  86. 'className' => 'Debug',
  87. 'log' => true,
  88. ],
  89. 'test_smtp' => [
  90. 'className' => 'Smtp',
  91. 'username' => 'mark',
  92. 'password' => 'password',
  93. 'host' => 'example.com',
  94. ],
  95. ];
  96. TransportFactory::drop('debug');
  97. TransportFactory::setConfig($settings);
  98. $this->assertEquals($settings['debug'], TransportFactory::getConfig('debug'));
  99. $this->assertEquals($settings['test_smtp'], TransportFactory::getConfig('test_smtp'));
  100. }
  101. /**
  102. * Test that exceptions are raised when duplicate transports are configured.
  103. */
  104. public function testSetConfigErrorOnDuplicate(): void
  105. {
  106. $this->expectException(BadMethodCallException::class);
  107. $settings = [
  108. 'className' => 'Debug',
  109. 'log' => true,
  110. ];
  111. TransportFactory::setConfig('debug', $settings);
  112. TransportFactory::setConfig('debug', $settings);
  113. TransportFactory::drop('debug');
  114. }
  115. /**
  116. * Test configTransport with an instance.
  117. */
  118. public function testSetConfigInstance(): void
  119. {
  120. TransportFactory::drop('debug');
  121. $instance = new DebugTransport();
  122. TransportFactory::setConfig('debug', $instance);
  123. $this->assertEquals(['className' => $instance], TransportFactory::getConfig('debug'));
  124. }
  125. /**
  126. * Test enumerating all transport configurations
  127. */
  128. public function testConfigured(): void
  129. {
  130. $result = TransportFactory::configured();
  131. $this->assertIsArray($result, 'Should have config keys');
  132. foreach (array_keys($this->transports) as $key) {
  133. $this->assertContains($key, $result, 'Loaded transports should be present.');
  134. }
  135. }
  136. /**
  137. * Test dropping a transport configuration
  138. */
  139. public function testDrop(): void
  140. {
  141. $result = TransportFactory::getConfig('debug');
  142. $this->assertIsArray($result, 'Should have config data');
  143. TransportFactory::drop('debug');
  144. $this->assertNull(TransportFactory::getConfig('debug'), 'Should not exist.');
  145. }
  146. }