cancel
Showing results for 
Search instead for 
Did you mean: 

Integration Test not working in Custom Module Magento 2

Integration Test not working in Custom Module Magento 2

I am creating the Integration test in my custom module. I have installed the module in vendor folder structure is like as vendor/[ModuleVendor]/[ModuleName]/Src

I am using below command to run the integration test for custom module only

 

php vendor/phpunit/phpunit/phpunit -c dev/tests/integration/phpunit.xml vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration/

 

I have changed the phpunit.xml file like as

 

<testsuites>
        <!--<testsuite name="Magento Integration Tests">
            <file>testsuite/Magento/IntegrationTest.php</file>
        </testsuite>-->
        <!-- Memory tests run first to prevent influence of other tests on accuracy of memory measurements -->
        <!--<testsuite name="Memory Usage Tests">
            <file>testsuite/Magento/MemoryUsageTest.php</file>
        </testsuite>
        <testsuite name="Magento Integration Tests Real Suite">
            <directory>testsuite</directory>
            <directory>../../../app/code/*/*/Test/Integration</directory>
            <exclude>testsuite/Magento/MemoryUsageTest.php</exclude>
            <exclude>testsuite/Magento/IntegrationTest.php</exclude>
        </testsuite>-->
        <testsuite name="my integration">
            <directory suffix="Test.php">../../../vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration</directory>
            <exclude>../../../app/code/Magento</exclude>
            <exclude>../../../vendor/magento</exclude>
        </testsuite>
    </testsuites>

Added the product fixture in file vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration/_files/product_simple.php

 

 

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\TestFramework\Helper\Bootstrap;

/** @var $product \Magento\Catalog\Model\Product */
$product = Bootstrap::getObjectManager()
    ->create(\Magento\Catalog\Model\Product::class);

/*
 * Creation of Quote on Test Websites
 */
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);

/** @var ProductInterface $product */
$product = $objectManager->create(ProductInterface::class);
$product->setTypeId('simple')
    ->setName('Simple Product Integration test')
    ->setSku('simpleintegration')
    ->setWebsiteIds([0,1,2,3])
    ->setPrice(60)
    ->setMetaTitle('meta title')
    ->setMetaKeyword('meta keyword')
    ->setMetaDescription('meta description')
    ->setVisibility(Visibility::VISIBILITY_BOTH)
    ->setStatus(Status::STATUS_ENABLED)
    ->setStockData(['use_config_manage_stock' => 0])
    ->setAttributeSetId(4)
    ->setIsSalable(true)
    ->setSalable(true);
$product = $productRepository->save($product);

controller to check the integration test. vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration/Controller/CustomTest.php

 

 

<?php
namespace [ModuleVendor]\[ModuleName]\Test\Integration\Controller;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Session;
use Magento\Framework\Registry;
use Magento\TestFramework\Catalog\Model\ProductLayoutUpdateManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use Magento\TestFramework\TestCase\AbstractController;
/**
 * Checks product view on storefront
 *
 * @see \Magento\Catalog\Controller\Product
 *
 * @magentoDbIsolation enabled
 */
class CustomTest extends \Magento\TestFramework\TestCase\AbstractController
{
    /** @var Registry */
    private $registry;

    /** @var ProductRepositoryInterface */
    private $productRepository;

    /** @var Session */
    private $session;

    /**
     * @magentoDataFixture [ModuleVendor]/ModuleName]/controllers/_files/product_simple.php
     * @magentoAppArea frontend
     * @return void
     */
    public function testViewAction()
    {
        $product = $this->productRepository->get('simpleintegration');
        $this->dispatch(sprintf('catalog/product/view/id/%s', $product->getEntityId()));
        $currentProduct = $this->registry->registry('current_product');
        $this->assertInstanceOf(ProductInterface::class, $currentProduct);
        $this->assertEquals($product->getEntityId(), $currentProduct->getEntityId());
        $this->assertEquals($product->getEntityId(), $this->session->getLastViewedProductId());

        $responseBody = $this->getResponse()->getBody();
        /* Product info */
        $this->assertStringContainsString($product->getName(), $responseBody);
        $this->assertStringContainsString($product->getDescription(), $responseBody);
        $this->assertStringContainsString($product->getShortDescription(), $responseBody);
        $this->assertStringContainsString($product->getSku(), $responseBody);

        /* Stock info */
        $this->assertStringContainsString('$1,234.56', $responseBody);
        $this->assertStringContainsString('In stock', $responseBody);
        $this->assertStringContainsString((string)__('Add to Cart'), $responseBody);
        /* Meta info */
        $this->assertStringContainsString('<title>Simple Product 1 Meta Title</title>', $responseBody);
        $this->assertEquals(
            1,
            Xpath::getElementsCountForXpath(
                '//meta[@name="keywords" and @content="Simple Product 1 Meta Keyword"]',
                $responseBody
            )
        );
        $this->assertEquals(
            1,
            Xpath::getElementsCountForXpath(
                '//meta[@name="description" and @content="Simple Product 1 Meta Description"]',
                $responseBody
            )
        );
    }
}

 

Spoiler
Error I am getting on Console Screen

 

PHPUnit 9.5.28 by Sebastian Bergmann and contributors.

Could not use "Magento\TestFramework\SuiteLoader" as loader.

=== Memory Usage System Stats ===
Memory usage (OS):	111.29M (172.54% of 64.50M reported by PHP)
Estimated memory leak:	46.79M (42.04% of used memory)