Skip to content

Commit

Permalink
fix: Ensure mocks in unit tests have unique classnames
Browse files Browse the repository at this point in the history
  • Loading branch information
paxuclus committed Oct 31, 2024
1 parent 5bc037c commit 04a95a4
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions Tests/Unit/Scope/ScopeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ protected function setUp(): void
public function providers_are_sorted_according_to_settings(): void
{
$extra1 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock1')
->setMockClassName('ExtraMock1' . $this->classNameSuffixForMocks())
->getMock();
$extra1
->method('getExtra')
->willReturn(['foo' => 'bar']);
$extra2 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock2')
->setMockClassName('ExtraMock2' . $this->classNameSuffixForMocks())
->getMock();
$extra2
->method('getExtra')
Expand Down Expand Up @@ -80,19 +80,19 @@ public function providers_are_sorted_according_to_settings(): void
public function providers_with_falsy_configuration_are_not_used(): void
{
$extra1 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock1')
->setMockClassName('ExtraMock1' . $this->classNameSuffixForMocks())
->getMock();
$extra1
->method('getExtra')
->willReturn(['foo' => 'bar']);
$extra2 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock2')
->setMockClassName('ExtraMock2' . $this->classNameSuffixForMocks())
->getMock();
$extra2
->method('getExtra')
->willReturn(['foo' => 'baz']);
$extra3 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock3')
->setMockClassName('ExtraMock3' . $this->classNameSuffixForMocks())
->getMock();
$extra3
->method('getExtra')
Expand All @@ -101,7 +101,7 @@ public function providers_with_falsy_configuration_are_not_used(): void
$this->objectManagerMock
->expects(self::once())
->method('get')
->with('ExtraMock1')
->with('ExtraMock1' . $this->classNameSuffixForMocks())
->willReturn($extra1);

$this->provider->injectSettings([
Expand Down Expand Up @@ -290,13 +290,13 @@ public function only_last_environment_is_returned(): void
public function extra_is_merged_recursively(): void
{
$extra1 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock1')
->setMockClassName('ExtraMock1' . $this->classNameSuffixForMocks())
->getMock();
$extra1
->method('getExtra')
->willReturn(['foo' => 'bar']);
$extra2 = $this->getMockBuilder(ExtraProvider::class)
->setMockClassName('ExtraMock2')
->setMockClassName('ExtraMock2' . $this->classNameSuffixForMocks())
->getMock();
$extra2
->method('getExtra')
Expand Down Expand Up @@ -330,13 +330,13 @@ public function extra_is_merged_recursively(): void
public function only_last_release_is_returned(): void
{
$release1 = $this->getMockBuilder(ReleaseProvider::class)
->setMockClassName('ReleaseMock1')
->setMockClassName('ReleaseMock1' . $this->classNameSuffixForMocks())
->getMock();
$release1
->method('getRelease')
->willReturn('release-1');
$release2 = $this->getMockBuilder(ReleaseProvider::class)
->setMockClassName('ReleaseMock2')
->setMockClassName('ReleaseMock2' . $this->classNameSuffixForMocks())
->getMock();
$release2
->method('getRelease')
Expand Down Expand Up @@ -370,13 +370,13 @@ public function only_last_release_is_returned(): void
public function tags_are_merged_and_override_each_other(): void
{
$tag1 = $this->getMockBuilder(TagProvider::class)
->setMockClassName('TagMock1')
->setMockClassName('TagMock1' . $this->classNameSuffixForMocks())
->getMock();
$tag1
->method('getTags')
->willReturn(['foo' => 'bar']);
$tag2 = $this->getMockBuilder(TagProvider::class)
->setMockClassName('TagMock2')
->setMockClassName('TagMock2' . $this->classNameSuffixForMocks())
->getMock();
$tag2
->method('getTags')
Expand Down Expand Up @@ -410,13 +410,13 @@ public function tags_are_merged_and_override_each_other(): void
public function user_is_merged_recursively(): void
{
$user1 = $this->getMockBuilder(UserProvider::class)
->setMockClassName('UserMock1')
->setMockClassName('UserMock1' . $this->classNameSuffixForMocks())
->getMock();
$user1
->method('getUser')
->willReturn(['foo' => 'bar']);
$user2 = $this->getMockBuilder(UserProvider::class)
->setMockClassName('UserMock2')
->setMockClassName('UserMock2' . $this->classNameSuffixForMocks())
->getMock();
$user2
->method('getUser')
Expand Down Expand Up @@ -489,4 +489,10 @@ public function While_the_callable_runs_the_current_throwable_is_set(): void
self::assertNull($this->provider->getCurrentThrowable());
}

private function classNameSuffixForMocks(): string
{
$method = method_exists($this, 'nameWithDataSet') ? 'nameWithDataSet' : 'getName';
return '_' . [$this, $method]();
}

}

0 comments on commit 04a95a4

Please sign in to comment.