2727
2828class MessageBuilderTest extends TestCase
2929{
30+ private TemplateRepository &MockObject $ templateRepository ;
3031 private MessageFormatBuilder &MockObject $ formatBuilder ;
3132 private MessageScheduleBuilder &MockObject $ scheduleBuilder ;
3233 private MessageContentBuilder &MockObject $ contentBuilder ;
@@ -35,22 +36,22 @@ class MessageBuilderTest extends TestCase
3536
3637 protected function setUp (): void
3738 {
38- $ templateRepository = $ this ->createMock (TemplateRepository::class);
39+ $ this -> templateRepository = $ this ->createMock (TemplateRepository::class);
3940 $ this ->formatBuilder = $ this ->createMock (MessageFormatBuilder::class);
4041 $ this ->scheduleBuilder = $ this ->createMock (MessageScheduleBuilder::class);
4142 $ this ->contentBuilder = $ this ->createMock (MessageContentBuilder::class);
4243 $ this ->optionsBuilder = $ this ->createMock (MessageOptionsBuilder::class);
4344
4445 $ this ->builder = new MessageBuilder (
45- templateRepository: $ templateRepository ,
46+ templateRepository: $ this -> templateRepository ,
4647 messageFormatBuilder: $ this ->formatBuilder ,
4748 messageScheduleBuilder: $ this ->scheduleBuilder ,
4849 messageContentBuilder: $ this ->contentBuilder ,
4950 messageOptionsBuilder: $ this ->optionsBuilder
5051 );
5152 }
5253
53- private function createRequest (): CreateMessageDto
54+ private function createRequest (? int $ templateId = 0 ): CreateMessageDto
5455 {
5556 return new CreateMessageDto (
5657 content: new MessageContentDto (
@@ -59,7 +60,6 @@ private function createRequest(): CreateMessageDto
5960 footer: ''
6061 ),
6162 format: new MessageFormatDto (
62- htmlFormated: false ,
6363 sendFormat: 'text ' ,
6464 ),
6565 metadata: new MessageMetadataDto (
@@ -78,15 +78,15 @@ private function createRequest(): CreateMessageDto
7878 requeueInterval: null ,
7979 requeueUntil: null
8080 ),
81- templateId: 0
81+ templateId: $ templateId
8282 );
8383 }
8484
8585 private function mockBuildCalls (CreateMessageDto $ createMessageDto ): void
8686 {
8787 $ this ->formatBuilder ->expects ($ this ->once ())
8888 ->method ('build ' )
89- ->with ($ createMessageDto-> format )
89+ ->with ($ createMessageDto )
9090 ->willReturn ($ this ->createMock (Message \MessageFormat::class));
9191
9292 $ this ->scheduleBuilder ->expects ($ this ->once ())
@@ -113,7 +113,14 @@ public function testBuildsNewMessage(): void
113113
114114 $ this ->mockBuildCalls ($ request );
115115
116- $ this ->builder ->build (createMessageDto: $ request , context: $ context );
116+ $ this ->templateRepository ->expects ($ this ->once ())
117+ ->method ('find ' )
118+ ->with (0 )
119+ ->willReturn (null );
120+
121+ $ result = $ this ->builder ->build (createMessageDto: $ request , context: $ context );
122+
123+ $ this ->assertInstanceOf (Message::class, $ result );
117124 }
118125
119126 public function testThrowsExceptionOnInvalidContext (): void
@@ -132,25 +139,37 @@ public function testUpdatesExistingMessage(): void
132139
133140 $ this ->mockBuildCalls ($ request );
134141
142+ $ this ->templateRepository ->expects ($ this ->once ())
143+ ->method ('find ' )
144+ ->with (0 )
145+ ->willReturn (null );
146+
135147 $ existingMessage
136148 ->expects ($ this ->once ())
137149 ->method ('setFormat ' )
138150 ->with ($ this ->isInstanceOf (Message \MessageFormat::class));
151+
139152 $ existingMessage
140153 ->expects ($ this ->once ())
141154 ->method ('setSchedule ' )
142155 ->with ($ this ->isInstanceOf (MessageSchedule::class));
156+
143157 $ existingMessage
144158 ->expects ($ this ->once ())
145159 ->method ('setContent ' )
146160 ->with ($ this ->isInstanceOf (MessageContent::class));
161+
147162 $ existingMessage
148163 ->expects ($ this ->once ())
149164 ->method ('setOptions ' )
150165 ->with ($ this ->isInstanceOf (Message \MessageOptions::class));
151- $ existingMessage ->expects ($ this ->once ())->method ('setTemplate ' )->with (null );
152166
153- $ result = $ this ->builder ->build ($ request , $ context );
167+ $ existingMessage
168+ ->expects ($ this ->once ())
169+ ->method ('setTemplate ' )
170+ ->with (null );
171+
172+ $ result = $ this ->builder ->build (createMessageDto: $ request , context: $ context );
154173
155174 $ this ->assertSame ($ existingMessage , $ result );
156175 }
0 commit comments