1

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?

Update

I've been experimenting on this repo

https://coveralls.io/github/DominicWatts/ReviewReminder

https://github.com/DominicWatts/ReviewReminder

https://github.com/DominicWatts/ReviewReminder/blob/master/Test/Unit/OrderTest.php

You should be able to run my docker unit test over the code with minimal effort

[clone github then in repo root]

docker run --rm -v $(pwd):/code domw/phpunit:7.2 phpunit Test

4
  • Uh, why aren't you using the test object manager? Magento\Framework\TestFramework\Unit\Helper\ObjectManager
    – David Tay
    May 28 at 20:34
  • @DavidTay can you demonstrate? Jun 1 at 12:56
  • $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager(); Then instantiate your helper with $this->helper = $objectManager->getObject(HelperClass::class, [ 'currencyFactory' => $currencyFactory ]);
    – David Tay
    Jun 2 at 12:33
  • @DavidTay Logical but I'm still not sure I've got the right syntax. I've updated the answer with the repo I've experimented on Jun 2 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.