From 04a95a41aeefe916aa2cae071f35097e3a0c70de Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Thu, 31 Oct 2024 15:41:20 +0100 Subject: [PATCH] fix: Ensure mocks in unit tests have unique classnames --- Tests/Unit/Scope/ScopeProviderTest.php | 34 +++++++++++++++----------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Tests/Unit/Scope/ScopeProviderTest.php b/Tests/Unit/Scope/ScopeProviderTest.php index 422f097..1c505e5 100644 --- a/Tests/Unit/Scope/ScopeProviderTest.php +++ b/Tests/Unit/Scope/ScopeProviderTest.php @@ -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') @@ -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') @@ -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([ @@ -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') @@ -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') @@ -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') @@ -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') @@ -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](); + } + }