Skip to content

Commit

Permalink
Add tests to make sure private provider methods are ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
max-m committed Feb 28, 2025
1 parent 1355632 commit 165fb12
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/Fixture/Provider/BarProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ public function fooFormatter()
{
return 'barfoo';
}

private function maybeShadowedFormatter()
{
return 'shadowed';
}

private function maybeShadowedFormatter2()
{
return 'shadowed';
}
}
23 changes: 23 additions & 0 deletions test/Fixture/Provider/FooProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,27 @@ public function fooFormatterWithArguments($value = '')
{
return 'baz' . $value;
}

// The PHP CS Fixer `protected_to_private` fixer rule rewrites this to `private function`
/*
protected function protectedFormatter()
{
return 'protected';
}
*/

private function privateFormatter()
{
return 'private';
}

public function maybeShadowedFormatter()
{
return 'not shadowed';
}

public function maybeShadowedFormatter2()
{
return 'also not shadowed';
}
}
41 changes: 41 additions & 0 deletions test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,45 @@ public function word(): string

$uniqueGenerator->word();
}

// Disabled because the PHP CS Fixer’s `protected_to_private` rule rewrites
// `protected function protectedFormatter()` to `private function protectedFormatter()`,
// and currently there’s no way to disable CS Fixer rules for a single line
/*
public function testProtectedProviderMethodsAreNotCalled(): void
{
$this->faker->addProvider(new Fixture\Provider\FooProvider());
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Unknown format "protectedFormatter"'));
$this->faker->protectedFormatter();
}
public function testProtectedProviderMethodsDoNotShadowPublicMethods(): void
{
$this->faker->addProvider(new Fixture\Provider\FooProvider());
$this->faker->addProvider(new Fixture\Provider\BarProvider());
self::assertSame('not shadowed', $this->faker->maybeShadowedFormatter());
}
*/

public function testPrivateProviderMethodsAreNotCalled(): void
{
$this->faker->addProvider(new Fixture\Provider\FooProvider());

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Unknown format "privateFormatter"'));

$this->faker->privateFormatter();
}

public function testPrivateProviderMethodsDoNotShadowPublicMethods(): void
{
$this->faker->addProvider(new Fixture\Provider\FooProvider());
$this->faker->addProvider(new Fixture\Provider\BarProvider());

self::assertSame('also not shadowed', $this->faker->maybeShadowedFormatter2());
}
}

0 comments on commit 165fb12

Please sign in to comment.