Skip to content

Commit 5bcc53f

Browse files
Copilotlcharette
andcommitted
Refactor unit test to reduce code duplication with helper method
Co-authored-by: lcharette <2566513+lcharette@users.noreply.github.com>
1 parent f4de038 commit 5bcc53f

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

packages/sprinkle-core/app/tests/Unit/ServicesProvider/MarkdownServiceTest.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public function setUp(): void
4242
$this->ci = ContainerStub::create($provider->register());
4343
}
4444

45+
/**
46+
* Helper method to mock MarkdownRepositoryInterface with empty extensions.
47+
*/
48+
protected function mockEmptyMarkdownRepository(): void
49+
{
50+
$repository = Mockery::mock(MarkdownRepositoryInterface::class)
51+
->shouldReceive('getIterator')->once()->andReturn(new ArrayIterator([]))
52+
->getMock();
53+
$this->ci->set(MarkdownRepositoryInterface::class, $repository);
54+
}
55+
4556
public function testConverterWithDefaultConfig(): void
4657
{
4758
// Set mock Config with default markdown config
@@ -58,11 +69,7 @@ public function testConverterWithDefaultConfig(): void
5869

5970
$this->ci->set(Config::class, $config);
6071

61-
// Mock empty MarkdownRepositoryInterface
62-
$repository = Mockery::mock(MarkdownRepositoryInterface::class)
63-
->shouldReceive('getIterator')->once()->andReturn(new ArrayIterator([]))
64-
->getMock();
65-
$this->ci->set(MarkdownRepositoryInterface::class, $repository);
72+
$this->mockEmptyMarkdownRepository();
6673

6774
// Get the converter
6875
$converter = $this->ci->get(ConverterInterface::class);
@@ -83,11 +90,7 @@ public function testConverterWithEmptyConfig(): void
8390

8491
$this->ci->set(Config::class, $config);
8592

86-
// Mock empty MarkdownRepositoryInterface
87-
$repository = Mockery::mock(MarkdownRepositoryInterface::class)
88-
->shouldReceive('getIterator')->once()->andReturn(new ArrayIterator([]))
89-
->getMock();
90-
$this->ci->set(MarkdownRepositoryInterface::class, $repository);
93+
$this->mockEmptyMarkdownRepository();
9194

9295
// Get the converter - should still work with empty config
9396
$converter = $this->ci->get(ConverterInterface::class);
@@ -111,11 +114,7 @@ public function testConverterWithCustomConfig(): void
111114

112115
$this->ci->set(Config::class, $config);
113116

114-
// Mock empty MarkdownRepositoryInterface
115-
$repository = Mockery::mock(MarkdownRepositoryInterface::class)
116-
->shouldReceive('getIterator')->once()->andReturn(new ArrayIterator([]))
117-
->getMock();
118-
$this->ci->set(MarkdownRepositoryInterface::class, $repository);
117+
$this->mockEmptyMarkdownRepository();
119118

120119
// Get the converter
121120
$converter = $this->ci->get(ConverterInterface::class);
@@ -135,11 +134,7 @@ public function testConverterCanConvertBasicMarkdown(): void
135134

136135
$this->ci->set(Config::class, $config);
137136

138-
// Mock empty MarkdownRepositoryInterface
139-
$repository = Mockery::mock(MarkdownRepositoryInterface::class)
140-
->shouldReceive('getIterator')->once()->andReturn(new ArrayIterator([]))
141-
->getMock();
142-
$this->ci->set(MarkdownRepositoryInterface::class, $repository);
137+
$this->mockEmptyMarkdownRepository();
143138

144139
// Get the converter
145140
$converter = $this->ci->get(ConverterInterface::class);
@@ -163,11 +158,7 @@ public function testConverterHandlesHtmlAccordingToConfig(): void
163158

164159
$this->ci->set(Config::class, $config);
165160

166-
// Mock empty MarkdownRepositoryInterface
167-
$repository = Mockery::mock(MarkdownRepositoryInterface::class)
168-
->shouldReceive('getIterator')->once()->andReturn(new ArrayIterator([]))
169-
->getMock();
170-
$this->ci->set(MarkdownRepositoryInterface::class, $repository);
161+
$this->mockEmptyMarkdownRepository();
171162

172163
$converter = $this->ci->get(ConverterInterface::class);
173164

0 commit comments

Comments
 (0)