Skip to content

Annotation like @accessPrivateMethod #409

@janbarasek

Description

@janbarasek

Very often I need to test private methods in packages, models, ...

... but I can not.

Imagine test:

class QueryNormalizerTest extends TestCase
{

	/**
	 * @var QueryNormalizer
	 */
	private $queryNormalizer;

	/**
	 * Expected syntax:
	 * @accessPrivateMethod $queryNormalizer::taskFixBrackets
	 */
	public function testFixBrackets(): void
	{
		Assert::same('1+2*(9-4)', $this->queryNormalizer->taskFixBrackets('1+2*(9-4'));
	}

}

QueryNormalizer class:

class QueryNormalizer
{

	public function normalize(string $query): string
	{
		return ...;
	}

	private function taskFixBrackets(string $query): string
	{
		return ...;
	}

}

Currently I can not test private methods. I know I can call public method in class what will call some private methods, but in real case it is so hard. I want split tests and make separately logic for independent atomic test for specific methods and "big" test for call all logic like user of class.

When I can not test methods separately it's extremely hard to debug.

Possible solution

Define new annotation @accessPrivateMethod to test where I can define name of property with class (service) instance and specific method name.

For example:

@accessPrivateMethod $queryNormalizer::taskFixBrackets

In TestCase will be inner logic for ReflectionMethod what change method to pubic by setAccessible or other magic.

If my suggestion make sense I will be very glad for implement it. :)

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions