Skip to content

Facing issue to write test case for the product custom attribute for graphQL API response

I build a functionality where in I have custom attribute in product, I am using graphQL query to fetch the products list. To test this functionality I am writing a test case which will check if query return the correct data or not.

Here is my code –

    <?php
declare(strict_types=1);
namespace VendorModuleTestIntegration;

use VendorModulePluginAddLanguageToLinksPlugin;
use MagentoCatalogApiDataProductInterface;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoTestFrameworkHelperBootstrap;
use MagentoGraphQlControllerGraphQl;
use MagentoFrameworkWebapiRestRequest;
use PHPUnitFrameworkTestCase;
use MagentoFrameworkAppRequestHttp;

class CorrectLanguageResponseTest extends TestCase
{
    /** @var MagentoFrameworkAppObjectManager */
    private $objectManager;

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

    /** @var ProductInterface|Product */
    private $product;

    /** @var AddLanguageToLinksPlugin */
    private $languageOptions;

    protected function setUp(): void
    {
        $this->objectManager = Bootstrap::getObjectManager();
        $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
        $this->product = $this->objectManager->create(ProductInterface::class);

        $this->languageOptions = $this->objectManager->create(AddLanguageToLinksPlugin::class)->getOptions();

        $productData = [
            'sku' => 'downloadable_product',
            'name' => 'Downloadable Product',
            'price' => 0,
            'type_id' => 'simple',
            'attribute_set_id' => 4,
            'status' => MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED,
            'visibility' => MagentoCatalogModelProductVisibility::VISIBILITY_BOTH,
        ];

        $this->product->setData($productData);
        $this->product->setCategoryIds([2]);
        $this->addLinkToProduct();
        $this->productRepository->save($this->product);
    }

    public function addLinkToProduct()
    {
        $newDownloadableLink = [
            [
                'link_id' => 1,
                'title' => 'English',
                'sort_order' => 0,
                'is_shareable' => 0,
                'number_of_downloads' => 0,
                'is_unlimited' => 1,
                'sample' => [
                    'type' => 'url',
                    'file' => 'http://example.com/sample-file.pdf',
                    'url' => 'http://example.com/sample-file.pdf'
                ],
                'file' => [
                    'type' => 'url',
                    'file' => 'http://example.com/downloadable-file.zip',
                    'url' => 'http://example.com/downloadable-file.zip'
                ],
                'language' => $this->languageOptions[3]['value']
            ],
            [
                'link_id' => 2,
                'title' => 'Norwegian',
                'sort_order' => 0,
                'is_shareable' => 0,
                'number_of_downloads' => 0,
                'is_unlimited' => 1,
                'sample' => [
                    'type' => 'url',
                    'file' => 'http://example.com/sample-file.pdf',
                    'url' => 'http://example.com/sample-file.pdf'
                ],
                'file' => [
                    'type' => 'url',
                    'file' => 'http://example.com/downloadable-file.zip',
                    'url' => 'http://example.com/downloadable-file.zip'
                ],
                'language' => $this->languageOptions[2]['value']
            ],
        ];

        $this->product->setData('downloadable_links_data', $newDownloadableLink);
    }

    public function testCheckCorrectGraphQlApiResponse()
    {        
        $query = <<<QUERY
            {
                products(
                    search: "",
                    pageSize: 10,
                    currentPage: 1
                ) {
                    total_count
                    items {
                        id
                        type_id
                        name
                        sku
                        language {
                            id
                            label
                        }
                    }
                }
            }
            QUERY;
            
        /** @var HTTP $request */
        $request = $this->objectManager->create(HTTP::class);
        $request->setMethod(Request::HTTP_METHOD_POST);
        $request->setContent(json_encode(['query' => $query]));
        
        /** @var GraphQl $graphQl */
        $graphQl = $this->objectManager->create(GraphQl::class);
        $response = $graphQl->dispatch($request);


        $this->assertEquals(200, $response->getStatusCode());
        $responseData = json_decode($response->getBody(), true);

        $this->assertArrayHasKey('data', $responseData);
        $this->assertArrayHasKey('products', $responseData['data']);
    }
}

Response –

$response = $graphQl->dispatch($request);

"{"data":{"products":{"total_count":0,"items":[]}}}"

Always getting 0 count, I am not what I am doing wrong here, any help is much appreciated