magento2 – Unit test ‘non-existent’ factory methods


Potentially same question has been asked but differently.

You are testing something in isolation and assume no access to generated folder.

Helper method

 
class HelperClass extends AbstractHelper

    public function __construct(
        Context $context,
        CurrencyFactory $currencyFactory
    ) {
        $this->currencyFactory = $currencyFactory;

    (...)

    /**
     * Format price with symbol
     * @param float $price
     * @param string $symbol
     * @return string
     */
    public function formatPrice(float $price, string $symbol): string
    {
        return $this->currencyFactory
            ->create()
            ->format(
                $price,
                ('symbol' => $symbol),
                false
            );
    }

Unit test

    public function setUp(): void
    {
        $this->currencyMock = $this->createMock(Currency::class);
        $this->currencyMock->expects($this->any())
            ->method('load')
            ->willReturnSelf();

        $this->currencyFactoryMock = $this->getMockBuilder(CurrencyFactory::class)
            ->disableOriginalConstructor()
            ->setMethods(('create'))
            ->getMock();

        $this->currencyFactoryMock->expects($this->any())
            ->method('create')
            ->willReturn($this->currencyMock);

        $this->helper = new HelperClass(
            $this->contextMock,
            $this->currencyFactoryMock
        );
    }

    public function testFormatPrice()
    {
        $this->assertEquals('£2.23', $this->helper->formatPrice((float) 2.2294, '£'));
    }

Error: HelperClass::formatPrice() must be of the type string, null returned

Is there just no way of testing generated classes without them existing?