8000 Make processor class non-final so it can easily be mocked in unit tests · Issue #67 · nette/schema · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Make processor class non-final so it can easily be mocked in unit tests #67
Open
@gauthierm

Description

@gauthierm

The Processor class is marked as final and this makes it difficult to mock in unit tests. In a unit test, I want to hard-code the result of the processing with fixture data as I don't want to test nette/schema, I want to test my code. This is typically done with a mocking library like Mockery, but because the class is final this is not easy to do cleanly.

<?php
class ContentParserTest extends TestCase
{
    #[Test]
    public function parsesNestedContentFields(): void
    {
        $factory = Mockery::mock(Factory::class);
        $processor = Mockery::mock(Processor::class);

        $parser = new ContentParser($processor, $factory);
        
        ... other test stuff
    }
}

This results in:

Mockery\Ex 4E75 ception: The class \Nette\Schema\Processor is marked final and its methods cannot be replaced. Classes marked final can be passed in to \Mockery::mock() as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.

Alternatively, consider making an interface for the Processor, leave the Processor class as final and implement the interface. That way unit tests and methods can use the interface and you can still prevent subclassing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0