From 41eda0c7c40d0f777389dac1f68843e9f6971417 Mon Sep 17 00:00:00 2001 From: Tim Bond Date: Fri, 12 Apr 2024 15:53:52 -0700 Subject: [PATCH] Resolve PHPUnit deprecations * Remove calls to getMockForAbstractClass(), see [1] * Remove calls to PHPUnit method returnValueMap, see [2] * Work around issue with deprecated test_suffix CSV, see [3] * Do not explicitly add `-colors=always` since it is already added [4] [1]: https://github.com/sebastianbergmann/phpunit/issues/5241 [2]: https://github.com/sebastianbergmann/phpunit/issues/5423 [3]: https://github.com/php-actions/phpunit/pull/64 [4]: https://github.com/php-actions/phpunit/blob/c27e49b5fd8cd59d032b7b4441d2e15f23cf6519/phpunit-action.bash#L154 --- .github/workflows/test.yml | 3 +- tests/AbstractMeetupTest.php | 36 +++++++----------- .../Processor/RssFeedProcessorTest.php | 36 ++++++++++++++---- .../Generator/MeetupGeneratorCommandTest.php | 14 +++---- tests/fixtures/TestMeetup.php | 38 +++++++++++++++++++ 5 files changed, 87 insertions(+), 40 deletions(-) create mode 100644 tests/fixtures/TestMeetup.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 22565b4..2ab1dcb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,6 +22,7 @@ jobs: - name: Run PHPUnit Tests uses: php-actions/phpunit@v3 with: - args: --colors=always --testdox tests + args: --testdox tests bootstrap: vendor/autoload.php + test_suffix: "Test.php" #workaround for https://github.com/php-actions/phpunit/pull/64 php_version: "8.3" diff --git a/tests/AbstractMeetupTest.php b/tests/AbstractMeetupTest.php index 224ee27..1a9db93 100644 --- a/tests/AbstractMeetupTest.php +++ b/tests/AbstractMeetupTest.php @@ -6,61 +6,51 @@ use DateTimeImmutable; use MergePHP\Website\AbstractMeetup; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Tests\fixtures\TestMeetup; class AbstractMeetupTest extends TestCase { - private function generateMock(string $title): AbstractMeetup|MockObject - { - $mock = $this->getMockForAbstractClass(AbstractMeetup::class); - $mock->method('getTitle')->willReturn($title); - return $mock; - } - public function testItGeneratesASlugIgnoringEmojis(): void { - $mock = $this->generateMock('50 Ways To Leave Your ♥er'); + $meetup = new TestMeetup('50 Ways To Leave Your ♥er'); - $this->assertEquals('50-ways-to-leave-your-er', $mock->getSlug()); + $this->assertEquals('50-ways-to-leave-your-er', $meetup->getSlug()); } public function testItGeneratesASlugWithoutConsecutiveDashes(): void { - $mock = $this->generateMock('First Part - Second Part'); + $meetup = new TestMeetup('First Part - Second Part'); - $this->assertEquals('first-part-second-part', $mock->getSlug()); + $this->assertEquals('first-part-second-part', $meetup->getSlug()); } public function testItGeneratesASlugThatDoesNotStartOrEndWithADash(): void { - $mock = $this->generateMock('@ slug !'); + $meetup = new TestMeetup('@ slug !'); - $this->assertEquals('slug', $mock->getSlug()); + $this->assertEquals('slug', $meetup->getSlug()); } public function testItGeneratesASlugThatDoesNotReallyDoWellWithAccentedCharacters(): void { - $mock = $this->generateMock('slúg'); + $meetup = new TestMeetup('slúg'); - $this->assertEquals('sl-g', $mock->getSlug()); + $this->assertEquals('sl-g', $meetup->getSlug()); } public function testItReturnsADefaultImageUrlOnlyWhenTheImageIsNotDefined(): void { - $mock = $this->getMockForAbstractClass(AbstractMeetup::class); + $meetup = new TestMeetup(''); - $this->assertEquals( - '/images/placeholder.webp', - $mock->getImage(), - ); + $this->assertEquals('/images/placeholder.webp', $meetup->getImage()); } public function testItAllowsANullYouTubeLink(): void { - $mock = $this->getMockForAbstractClass(AbstractMeetup::class); + $meetup = new TestMeetup(''); - $this->assertNull($mock->getYouTubeLink()); + $this->assertNull($meetup->getYouTubeLink()); } public function testItAllowsTheImageAndYouTubeLinkToBeOverridden(): void diff --git a/tests/Builder/Processor/RssFeedProcessorTest.php b/tests/Builder/Processor/RssFeedProcessorTest.php index 63fb8b9..eb7d68c 100644 --- a/tests/Builder/Processor/RssFeedProcessorTest.php +++ b/tests/Builder/Processor/RssFeedProcessorTest.php @@ -17,7 +17,7 @@ class RssFeedProcessorTest extends TestCase { - private const string MEETUP_DATE_STRING = '2000-01-01T00:00:00+00:00'; + public const string MEETUP_DATE_STRING = '2000-01-01T00:00:00+00:00'; private const string MODIFIED_DATE_STRING = '2000-01-02T00:00:00+00:00'; private const string EXPECTED_FILENAME = 'vfs://root/atom.xml'; private const string FIXTURES_DIR = __DIR__ . '/../../fixtures/'; @@ -76,12 +76,32 @@ public function testItIgnoresTheMeetupImageURL(): void private function generateMeetup(string $description = 'Example description'): AbstractMeetup { - $mock = $this->getMockForAbstractClass(AbstractMeetup::class); - $mock->method('getTitle')->willReturn('Example Meetup'); - $mock->method('getDescription')->willReturn($description); - $mock->method('getDateTime')->willReturn(new DateTimeImmutable(RssFeedProcessorTest::MEETUP_DATE_STRING)); - $mock->method('getSpeakerName')->willReturn('Speaker Name'); - $mock->method('getSpeakerBio')->willReturn('Speaker Bio'); - return $mock; + return new class ($description) extends AbstractMeetup + { + public function __construct(private readonly string $description) + { + } + + public function getTitle(): string + { + return 'Example Meetup'; + } + public function getDescription(): string + { + return $this->description; + } + public function getDateTime(): DateTimeImmutable + { + return new DateTimeImmutable(RssFeedProcessorTest::MEETUP_DATE_STRING); + } + public function getSpeakerName(): string + { + return 'Speaker Name'; + } + public function getSpeakerBio(): string + { + return 'Speaker Bio'; + } + }; } } diff --git a/tests/Generator/MeetupGeneratorCommandTest.php b/tests/Generator/MeetupGeneratorCommandTest.php index abf818a..eeededb 100644 --- a/tests/Generator/MeetupGeneratorCommandTest.php +++ b/tests/Generator/MeetupGeneratorCommandTest.php @@ -46,14 +46,12 @@ public function setUp(): void $response = new MeetupGeneratorResponse('foo', 123); $service = $this->createMock(MeetupGeneratorService::class); $service->method('getSuggestedDate')->willReturn('2023-01-01'); - $service->method('generate')->will( - $this->returnValueMap( - [ - // arg1, arg2, arg3, arg4, arg5, arg6, return - [...self::COMMAND_1_2_ARGS, $response], - [...self::COMMAND_3_ARGS, $response], - ] - ) + $service->method('generate')->willReturnMap( + [ + // arg1, arg2, arg3, arg4, arg5, arg6, return + [...self::COMMAND_1_2_ARGS, $response], + [...self::COMMAND_3_ARGS, $response], + ] ); $application = new Application(); diff --git a/tests/fixtures/TestMeetup.php b/tests/fixtures/TestMeetup.php new file mode 100644 index 0000000..5a81fd8 --- /dev/null +++ b/tests/fixtures/TestMeetup.php @@ -0,0 +1,38 @@ +title; + } + + public function getDescription(): string + { + return 'Description'; + } + + public function getDateTime(): DateTimeImmutable + { + return new DateTimeImmutable(); + } + + public function getSpeakerName(): string + { + return 'Speaker name'; + } + + public function getSpeakerBio(): string + { + return 'Speaker bio'; + } +}