Skip to content

Commit

Permalink
test: fixes failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonTheAdams committed Nov 15, 2024
1 parent e49e265 commit b76fde4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/Actions/RenderAdminNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function getStandardWrapperClasses(AdminNotice $notice): string
$classes[] = "is-dismissible";
}

if ($notice->getLocation()->isInline()) {
if ($notice->getLocation() && $notice->getLocation()->isInline()) {
$classes[] = 'inline';
}

Expand Down
33 changes: 24 additions & 9 deletions tests/unit/Actions/DisplayNoticesInAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testShouldAcceptMultipleNotices(): void
$notice1 = $this->getSimpleMockNotice('foo');
$notice2 = $this->getSimpleMockNotice('bar');

$this->expectOutputString('foobar');
$this->expectOutputString($this->getSimpleNoticeOutput('foo') . $this->getSimpleNoticeOutput('bar'));
$displayNoticesInAdmin($notice1, $notice2);
}

Expand All @@ -73,7 +73,7 @@ public function testPassesDateLimits(AdminNotice $notice, bool $shouldPass): voi
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');

if ($shouldPass) {
$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
} else {
$this->expectOutputString('');
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testPassesWhenCallback(AdminNotice $notice, bool $shouldPass): v
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');

if ($shouldPass) {
$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
} else {
$this->expectOutputString('');
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function testPassesUserCapabilities(AdminNotice $notice, bool $shouldPass
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');

if ($shouldPass) {
$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
} else {
$this->expectOutputString('');
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testShouldPassScreenConditionsWhenThereAreNoConditions(): void
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');
$notice = $this->getSimpleMockNotice('foo');

$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
$displayNoticesInAdmin($notice);
}

Expand All @@ -209,7 +209,7 @@ public function testShouldPassScreenConditionsWhenConditionIsValidRegex(): void
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');
$notice = $this->getSimpleMockNotice('foo')->on('~Dashboard~i'); // check regex flags, too

$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
$displayNoticesInAdmin($notice);
}

Expand All @@ -220,7 +220,7 @@ public function testShouldPassScreenConditionsWhenConditionIsValidString(): void
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');
$notice = $this->getSimpleMockNotice('foo')->on('dashboard');

$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
$displayNoticesInAdmin($notice);
}

Expand All @@ -234,10 +234,26 @@ public function testShouldPassScreenConditionsWhenConditionMatchesWPScreen(): vo
$displayNoticesInAdmin = new DisplayNoticesInAdmin('namespace');
$notice = $this->getSimpleMockNotice('foo')->on(['base' => 'dashboard']);

$this->expectOutputString('foo');
$this->expectNoticeInOutput('foo');
$displayNoticesInAdmin($notice);
}

/**
* @unreleased
*/
public function expectNoticeInOutput(string $expected): void
{
$this->expectOutputString($this->getSimpleNoticeOutput($expected));
}

/**
* @unreleased
*/
private function getSimpleNoticeOutput(string $content): string
{
return "<div class='notice notice-info' data-stellarwp-namespace-notice-id='test_id'>$content</div>";
}

/**
* Produces a simple mock with predictable output.
*
Expand All @@ -246,7 +262,6 @@ public function testShouldPassScreenConditionsWhenConditionMatchesWPScreen(): vo
private function getSimpleMockNotice($output): AdminNotice
{
return (new AdminNotice('test_id', $output))
->withoutWrapper()
->withoutAutoParagraph();
}
}
8 changes: 5 additions & 3 deletions tests/unit/Actions/RenderAdminNoticeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class RenderAdminNoticeTest extends TestCase
public function testShouldRenderNoticeWithoutWrapper(): void
{
$notice = (new AdminNotice('test_id', 'Hello world!'))
->withoutAutoParagraph()
->withoutWrapper();
->withoutAutoParagraph();

$renderAdminNotice = new RenderAdminNotice('namespace');

$this->assertEquals('Hello world!', $renderAdminNotice($notice));
$this->assertEquals(
'<div class=\'notice notice-info\' data-stellarwp-namespace-notice-id=\'test_id\'>Hello world!</div>',
$renderAdminNotice($notice)
);
}

/**
Expand Down
58 changes: 0 additions & 58 deletions tests/unit/AdminNoticeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,64 +218,6 @@ public function testUrgency(): void
$this->assertEquals('warning', $notice->getUrgency());
}

/**
* @covers ::withWrapper
* @covers ::withoutWrapper
* @covers ::usesWrapper
*/
public function testWithWrapper(): void
{
// Defaults to true
$notice = new AdminNotice('test_id', 'test');
$this->assertTrue($notice->usesWrapper());

// Method can be set to false
$self = $notice->withWrapper(false);
$this->assertFalse($notice->usesWrapper());
$this->assertSame($notice, $self);

// Method can be explicitly set to true
$notice->withWrapper(true);
$this->assertTrue($notice->usesWrapper());

// withoutWrapper is an alias for withWrapper(false)
$self = $notice->withoutWrapper();
$this->assertFalse($notice->usesWrapper());
$this->assertSame($notice, $self);
}

/**
* @covers ::inline
* @covers ::notInline
* @covers ::isInline
*
* @since 1.2.0
*/
public function testInline(): void
{
// Defaults to false
$notice = new AdminNotice('test_id', 'test');
$this->assertFalse($notice->isInline());

// Method defaults to true
$self = $notice->inline();
$this->assertTrue($notice->isInline());
$this->assertSame($notice, $self);

// Method can be explicitly set to false
$notice->inline(false);
$this->assertFalse($notice->isInline());

// Method can be set to true
$notice->inline(true);
$this->assertTrue($notice->isInline());

// notInline is an alias for inline(false)
$self = $notice->notInline();
$this->assertFalse($notice->isInline());
$this->assertSame($notice, $self);
}

/**
* @covers ::dismissible
* @covers ::notDismissible
Expand Down

0 comments on commit b76fde4

Please sign in to comment.