Skip to content

Commit

Permalink
Merge branch 'master' into 2.x-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 8, 2024
2 parents 38e818b + faeaf9f commit 91e300f
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 12 deletions.
34 changes: 32 additions & 2 deletions monorepo/HydeStan/HydeStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ public function run(string $file, string $contents): void
{
$searches = [
"assertEquals('",
'assertEquals("',
'assertEquals(null,',
'assertSame(null,',
'assertEquals(true,',
'assertSame(true,',
'assertEquals(false,',
'assertSame(false,',
];

foreach ($searches as $search) {
Expand All @@ -307,8 +314,22 @@ public function run(string $file, string $contents): void
continue;
}

// Todo: Does not work when using objects to string cast, false positive, maybe use warning instead of fail
$this->fail(sprintf('Found %s instead assertSame for scalar type in %s on line %s', trim($search, "()'"), $file, $lineNumber));
if (str_contains($search, 'null')) {
$call = rtrim($search, ',').')';
$message = 'Found '.$call.' instead of assertNull in %s.';
$this->fail(sprintf($message, fileLink($file, $lineNumber)));
} elseif (str_contains($search, 'true')) {
$call = rtrim($search, ',').')';
$message = 'Found '.$call.' instead of assertTrue in %s.';
$this->fail(sprintf($message, fileLink($file, $lineNumber)));
} elseif (str_contains($search, 'false')) {
$call = rtrim($search, ',').')';
$message = 'Found '.$call.' instead of assertFalse in %s.';
$this->fail(sprintf($message, fileLink($file, $lineNumber)));
} else {
$message = 'Found %s instead assertSame for scalar type in %s';
$this->fail(sprintf($message, trim($search, "()'"), fileLink($file, $lineNumber)));
}
}
}
}
Expand Down Expand Up @@ -431,3 +452,12 @@ function check_str_contains_any(array $searches, string $line): bool

return $strContainsAny;
}

function fileLink(string $file, ?int $line = null): string
{
$path = (realpath(__DIR__.'/../../packages/framework/'.$file) ?: $file).($line ? ':'.$line : '');
$trim = strlen(getcwd()) + 2;
$path = substr($path, $trim);

return str_replace('\\', '/', $path);
}
8 changes: 5 additions & 3 deletions packages/framework/tests/Feature/BladeMatterParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ public function testGetValueWithType()
{
$this->assertSame('string', ParserTestClass::getValueWithType('string'));
$this->assertSame('string', ParserTestClass::getValueWithType('string'));
$this->assertSame(true, ParserTestClass::getValueWithType('true'));
$this->assertSame(false, ParserTestClass::getValueWithType('false'));

$this->assertSame(1, ParserTestClass::getValueWithType('1'));
$this->assertSame(0, ParserTestClass::getValueWithType('0'));
$this->assertSame(1.0, ParserTestClass::getValueWithType('1.0'));
$this->assertSame(0.0, ParserTestClass::getValueWithType('0.0'));
$this->assertSame(null, ParserTestClass::getValueWithType('null'));
$this->assertSame(['foo' => 'bar'], ParserTestClass::getValueWithType('["foo" => "bar"]'));
$this->assertSame(['foo' => 'bar'], ParserTestClass::getValueWithType("['foo' => 'bar']"));

$this->assertTrue(ParserTestClass::getValueWithType('true'));
$this->assertFalse(ParserTestClass::getValueWithType('false'));
$this->assertNull(ParserTestClass::getValueWithType('null'));
}

public function testParseArrayString()
Expand Down
12 changes: 11 additions & 1 deletion packages/framework/tests/Feature/NavigationDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ public function testConstruct()

$this->assertSame('label', $navigationData->label);
$this->assertSame('group', $navigationData->group);
$this->assertSame(true, $navigationData->hidden);
$this->assertSame(1, $navigationData->priority);
$this->assertTrue($navigationData->hidden);
}

public function testConstructWithDifferentData()
{
$navigationData = new NavigationData('label', 2, false);

$this->assertSame('label', $navigationData->label);
$this->assertSame(2, $navigationData->priority);
$this->assertFalse($navigationData->hidden);
$this->assertNull($navigationData->group);
}

public function testMake()
Expand Down
8 changes: 7 additions & 1 deletion packages/framework/tests/Feature/TypedConfigFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function testGetBool()
{
config(['foo' => true]);
$this->assertIsBool(Config::getBool('foo'));

config(['foo' => false]);
$this->assertIsBool(Config::getBool('foo'));
}

public function testGetInt()
Expand All @@ -56,7 +59,8 @@ public function testGetStringWithDefaultValue()

public function testGetBoolWithDefaultValue()
{
$this->assertSame(true, Config::getBool('foo', true));
$this->assertTrue(Config::getBool('foo', true));
$this->assertFalse(Config::getBool('foo', false));
}

public function testGetIntWithDefaultValue()
Expand All @@ -82,6 +86,7 @@ public function testGetStringWithStrictMode()
public function testGetBoolWithStrictMode()
{
$this->runUnitTest(true, true, Config::getBool(...));
$this->runUnitTest(false, false, Config::getBool(...));
}

public function testGetIntWithStrictMode()
Expand Down Expand Up @@ -137,6 +142,7 @@ public function testGetStringWithString()
public function testGetBoolWithBool()
{
$this->runUnitTest(true, true, Config::getBool(...));
$this->runUnitTest(false, false, Config::getBool(...));
}

public function testGetIntWithInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function testCanDefineHydeConfigSettingsInHydeYmlFile()

$this->assertSame('Test', config('hyde.name'));
$this->assertSame('http://localhost', config('hyde.url'));
$this->assertSame(false, config('hyde.pretty_urls'));
$this->assertSame(true, config('hyde.generate_sitemap'));
$this->assertSame(true, config('hyde.rss.enabled'));
$this->assertSame('feed.xml', config('hyde.rss.filename'));
$this->assertSame('Test RSS Feed', config('hyde.rss.description'));
$this->assertSame('en', config('hyde.language'));
$this->assertSame('_site', config('hyde.output_directory'));
$this->assertTrue(config('hyde.generate_sitemap'));
$this->assertTrue(config('hyde.rss.enabled'));
$this->assertFalse(config('hyde.pretty_urls'));
}

public function testCanDefineMultipleConfigSettingsInHydeYmlFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testNavigationMenuLabel()

public function testNavigationMenuGroup()
{
$this->assertSame(null, (new DocumentationPage('foo'))->navigationMenuGroup());
$this->assertNull((new DocumentationPage('foo'))->navigationMenuGroup());
}

public function testNavigationMenuGroupWithData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function testCheckArgvForOption()
$command = $this->getMock();

$this->assertSame('true', $command->checkArgvForOption('pretty-urls'));
$this->assertSame(null, $command->checkArgvForOption('dashboard'));
$this->assertNull($command->checkArgvForOption('dashboard'));

$_SERVER = $serverBackup;
}
Expand Down
40 changes: 40 additions & 0 deletions packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Pages\InMemoryPage;
use Hyde\Testing\UnitTestCase;
use Hyde\Testing\MocksKernelFeatures;
use Hyde\Testing\FluentTestingHelpers;

/**
* Meta test for internal testing helpers.
Expand All @@ -19,6 +20,7 @@
class TestingSupportHelpersMetaTest extends UnitTestCase
{
use MocksKernelFeatures;
use FluentTestingHelpers;

protected static bool $needsKernel = true;
protected static bool $needsConfig = true;
Expand Down Expand Up @@ -61,6 +63,44 @@ public function testWithPagesWhenSupplyingStrings()
$this->assertContainsOnlyInstancesOf(InMemoryPage::class, $this->kernel->pages());
}

public function testAssertAllSameAssertsAllValuesAreTheSame()
{
$string = 'foo';
$array = ['foo'];
$object = (object) ['foo' => 'bar'];

$this->assertAllSame($string, 'foo', 'foo');
$this->assertAllSame($array, $array, $array);
$this->assertAllSame($object, $object, $object);
}

public function testAssertAllSameFailsWhenValuesAreNotEqual()
{
$tests = [
['foo', 'bar'],
[['foo'], ['bar']],
[(object) ['foo' => 'bar'], (object) ['foo' => 'baz']],
];

foreach ($tests as $expected) {
try {
$this->assertAllSame(...$expected);
} catch (\PHPUnit\Framework\AssertionFailedError $exception) {
$this->assertStringContainsString('Failed asserting that two', $exception->getMessage());
$this->assertStringContainsString('are equal.', $exception->getMessage());
}
}
}

public function testAssertAllSameFailsWhenValuesAreNotIdentical()
{
try {
$this->assertAllSame((object) ['foo' => 'bar'], (object) ['foo' => 'bar']);
} catch (\PHPUnit\Framework\AssertionFailedError $exception) {
$this->assertSame('Failed asserting that two variables reference the same object.', $exception->getMessage());
}
}

protected function getPageIdentifiers()
{
return $this->kernel->pages()->keys()->all();
Expand Down
12 changes: 12 additions & 0 deletions packages/testing/src/FluentTestingHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Hyde;

use function config;
use function array_shift;
use function file_get_contents;
use function Hyde\normalize_newlines;

Expand Down Expand Up @@ -46,4 +47,15 @@ protected function withoutSiteUrl(): void
{
config(['hyde.url' => null]);
}

/** Assert that all the given variables are the same. */
protected function assertAllSame(...$vars): void
{
$first = array_shift($vars);

foreach ($vars as $var) {
$this->assertEquals($first, $var);
$this->assertSame($first, $var);
}
}
}

0 comments on commit 91e300f

Please sign in to comment.