Skip to content

Commit

Permalink
Resolve PHPUnit deprecations
Browse files Browse the repository at this point in the history
* 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]: sebastianbergmann/phpunit#5241
[2]: sebastianbergmann/phpunit#5423
[3]: php-actions/phpunit#64
[4]: https://github.com/php-actions/phpunit/blob/c27e49b5fd8cd59d032b7b4441d2e15f23cf6519/phpunit-action.bash#L154
  • Loading branch information
cookieguru committed Apr 13, 2024
1 parent d24a3d4 commit 41eda0c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
36 changes: 13 additions & 23 deletions tests/AbstractMeetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions tests/Builder/Processor/RssFeedProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -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';
}
};
}
}
14 changes: 6 additions & 8 deletions tests/Generator/MeetupGeneratorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions tests/fixtures/TestMeetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tests\fixtures;

use DateTimeImmutable;
use MergePHP\Website\AbstractMeetup;

class TestMeetup extends AbstractMeetup
{
public function __construct(private readonly string $title)
{
}

public function getTitle(): string
{
return $this->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';
}
}

0 comments on commit 41eda0c

Please sign in to comment.