1414use CodeIgniter \Events \Events ;
1515use CodeIgniter \Test \CIUnitTestCase ;
1616use CodeIgniter \Test \Mock \MockEmail ;
17+ use ErrorException ;
1718
1819/**
1920 * @internal
@@ -44,9 +45,8 @@ public function autoClearProvider()
4445 */
4546 public function testEmailSendWithClearance ($ autoClear )
4647 {
47- $ config = config ('Email ' );
48- $ config ->validate = true ;
49- $ email = new MockEmail ($ config );
48+ $ email = $ this ->createMockEmail ();
49+
5050 $ email->
setTo (
'[email protected] ' );
5151
5252 $ this ->assertTrue ($ email ->send ($ autoClear ));
@@ -58,9 +58,8 @@ public function testEmailSendWithClearance($autoClear)
5858
5959 public function testEmailSendStoresArchive ()
6060 {
61- $ config = config ('Email ' );
62- $ config ->validate = true ;
63- $ email = new MockEmail ($ config );
61+ $ email = $ this ->createMockEmail ();
62+
6463 $ email->
setTo (
'[email protected] ' );
6564 $ email->
setFrom (
'[email protected] ' );
6665 $ email ->setSubject ('Archive Test ' );
@@ -75,9 +74,8 @@ public function testEmailSendStoresArchive()
7574
7675 public function testAutoClearLeavesArchive ()
7776 {
78- $ config = config ('Email ' );
79- $ config ->validate = true ;
80- $ email = new MockEmail ($ config );
77+ $ email = $ this ->createMockEmail ();
78+
8179 $ email->
setTo (
'[email protected] ' );
8280
8381 $ this ->assertTrue ($ email ->send (true ));
@@ -89,6 +87,7 @@ public function testEmailSendRepeatUpdatesArchive()
8987 {
9088 $ config = config ('Email ' );
9189 $ email = new MockEmail ($ config );
90+
9291 $ email->
setTo (
'[email protected] ' );
9392 $ email->
setFrom (
'[email protected] ' );
9493
@@ -104,9 +103,8 @@ public function testEmailSendRepeatUpdatesArchive()
104103
105104 public function testSuccessDoesTriggerEvent ()
106105 {
107- $ config = config ('Email ' );
108- $ config ->validate = true ;
109- $ email = new MockEmail ($ config );
106+ $ email = $ this ->createMockEmail ();
107+
110108 $ email->
setTo (
'[email protected] ' );
111109
112110 $ result = null ;
@@ -123,9 +121,8 @@ public function testSuccessDoesTriggerEvent()
123121
124122 public function testFailureDoesNotTriggerEvent ()
125123 {
126- $ config = config ('Email ' );
127- $ config ->validate = true ;
128- $ email = new MockEmail ($ config );
124+ $ email = $ this ->createMockEmail ();
125+
129126 $ email->
setTo (
'[email protected] ' );
130127 $ email ->returnValue = false ;
131128
@@ -139,4 +136,28 @@ public function testFailureDoesNotTriggerEvent()
139136
140137 $ this ->assertNull ($ result );
141138 }
139+
140+ public function testDestructDoesNotThrowException ()
141+ {
142+ $ email = $ this ->getMockBuilder (Email::class)
143+ ->disableOriginalConstructor ()
144+ ->onlyMethods (['sendCommand ' ])
145+ ->getMock ();
146+ $ email ->expects ($ this ->once ())->method ('sendCommand ' )
147+ ->willThrowException (new ErrorException ('SMTP Error. ' ));
148+
149+ // Force resource to be injected into the property
150+ $ SMTPConnect = fopen (__FILE__ , 'rb ' );
151+ $ this ->setPrivateProperty ($ email , 'SMTPConnect ' , $ SMTPConnect );
152+
153+ $ email ->__destruct ();
154+ }
155+
156+ private function createMockEmail (): MockEmail
157+ {
158+ $ config = config ('Email ' );
159+ $ config ->validate = true ;
160+
161+ return new MockEmail ($ config );
162+ }
142163}
0 commit comments