magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.39k stars 9.29k forks source link

Integration Test DataFixture Creating Simple Product with Options not saved correctly into test database #35261

Closed GamerHegi64 closed 1 year ago

GamerHegi64 commented 2 years ago

Preconditions (*)

  1. 2.4.3-p1

Steps to reproduce (*)

  1. Create Integration Test in Extension

Test Function

/**
   * @magentoDataFixture Custom_PriceRequest::Test/Integration/_files/simpleProdcutOptions.php
   * @group simpleOptionsProduct
   * @dataProvider dataPostSimpleOptionsProduct
   */
  public function testExecuteSimpleOptionsProduct($post, $second_post) {
    $objectManager = Bootstrap::getObjectManager();
    $repository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);

    /** @var Product $product */
    $product = $repository->getById(32);

    var_dump('TEST VAR DUMPS');
    var_dump($product->getName());
    var_dump($product->getOptionById(1)->toJson());

    $this->getRequest()->setMethod(Http::METHOD_POST);
    $this->getRequest()->setPostValue($post);
    $this->dispatch('pricerequest/request/newrequest');

    $body = $this->getResponse()->getBody();
    $this->assertStringContainsString('Simple Product 2', $body);
    $this->assertStringContainsString('<strong>Options:</strong>', $body);

    $this->getRequest()->setPostValue($second_post);
    $this->dispatch('pricerequest/request/newrequest');

    $this->assertRedirect($this->stringEndsWith('/price-request-received/'));

  }

public function dataPostSimpleOptionsProduct() {
    return array(
      [
        [
          'product' => '32',
          'options' => [
            '1' => ['1']
          ]
        ],
        [
          'product' => '32',
          'options' => [
            '1' => ['1']
          ]
          'email' => 'max.mustermann@gmail.com',
          'firstname' => 'Max',
          'lastname' => 'Mustermann',
          'company' => 'acompany',
          'country' => 'DE',
          'address' => 'Musterstraße 64',
          'city' => 'Musterstadt',
          'zip' => '646464',
          'telephone' => '01000000',
          'privacy_policy' => 'on',
          'comment' => '64',
          'vatnumber' => 'DE01000000'
        ]
      ]
    );
  }

The Testfunction call a Controller. In the Controller this lines are executed

var_dump($this->dataHelper->getProductById(32)->getName());
var_dump($this->dataHelper->getProductById(32)->getOptionById(1)->toJson());

The Helper class Function.

public function getProductById($id) {
    return $this->productRepository->getById($id);
  }

DataFixture File

use \Magento\Catalog\Model\Product;
use \Magento\Catalog\Model\Product\Option;
use \Magento\Catalog\Api\ProductRepositoryInterface;
use \Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);

/** @var ProductCustomOptionRepositoryInterface $productOptionRepository */
$productOptionRepository = $objectManager->create(ProductCustomOptionRepositoryInterface::class);
/** @var Product $product */
$product = $objectManager->create(Product::class);
$product->setId(32)
  ->setTypeId(Product\Type::TYPE_SIMPLE)
  ->setAttributeSetId(4)
  ->setName('Simple Product 2')
  ->setSku('simple2')
  ->setPrice(64)
  ->setWeight(10)
  ->setVisibility(Product\Visibility::VISIBILITY_BOTH)
  ->setStatus(Product\Attribute\Source\Status::STATUS_ENABLED)
  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);

$productRepository->save($product);

$options = array(
  array(
    "sort_order" => 1,
    "title" => "Custom Option 1",
    "price_type" => "fixed",
    "price" => "10",
    "type" => "field",
    "is_require" => 0
  )
);

/** @var Option $option */
$option = $objectManager->create(Option::class);
$option->setProductSku($product->getSku())
  ->setTitle('Custom Option 1')
  ->setStoreId($product->getStoreId())
  ->addData($options[0]);

$productOptionRepository->save($option);

$product->setCustomOptions([$option]);

$p = $productRepository->getById(32);
var_dump('DATA FIXTURE FILE VAR DUMPS');
var_dump($p->getName());
var_dump($p->toJson());
var_dump($p->getOptions()[0]->toJson());

Expected result (*)

  1. The var dumps in Controller should show the same result as the var dumps in the test function and in the data fixture file.

Actual result (*)

  1. only get options of the product in the test function and in the data fixture file, not in the controller.

The output of the terminal.

PHPUnit 9.2.6 by Sebastian Bergmann and contributors.

Warning:       Using a custom test suite loader is deprecated

string(27) "DATA FIXTURE FILE VAR DUMPS"
string(16) "Simple Product 2"
string(412) "{"option_id":"1","product_id":"32","type":"field","is_require":"0","sku":null,"max_characters":null,"file_extension":null,"image_size_x":null,"image_size_y":null,"sort_order":"1","default_title":"Custom Option 1","store_title":"Custom Option 1","title":"Custom Option 1","default_price":"10.000000","default_price_type":"fixed","store_price":null,"store_price_type":null,"price":"10.000000","price_type":"fixed"}"
E                                                                                                                                                                     1 / 1 (100%)string(14) 
"TEST VAR DUMPS"
string(16) "Simple Product 2"
string(412) "{"option_id":"1","product_id":"32","type":"field","is_require":"0","sku":null,"max_characters":null,"file_extension":null,"image_size_x":null,"image_size_y":null,"sort_order":"1","default_title":"Custom Option 1","store_title":"Custom Option 1","title":"Custom Option 1","default_price":"10.000000","default_price_type":"fixed","store_price":null,"store_price_type":null,"price":"10.000000","price_type":"fixed"}"
string(16) "Simple Product 2"

Time: 00:03.240, Memory: 107.00 MB

There was 1 error:

1) Custom\PriceRequest\Test\Integration\Controller\Request\NewRequestTest::testExecuteSimpleOptionsProduct with data set #0 (array('32', array(array('1'))), array('32', '', 'max.mustermann@gmail.com', 'Max', 'Mustermann', 'acompany', 'DE', 'Musterstraße 64', 'Musterstadt', '646464', '01000000', 'on', '64', 'DE01000000'))
Error: Call to a member function toJson() on null

When I call the Controller over the Browser it work perfectly. Only in the Integtration test i have the problem.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 2 years ago

Hi @GamerHegi64. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 1 year ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-November commented 1 year ago

Hi @GamerHegi64 , Thank you for reporting and collaboration. Verified the issue on Magento 2.4-develop branch but couldn't reproduce the issue. Please check the below code and you will see the correct data:

$product = $productRepository->getById(32);
var_dump('TEST CTRL DUMPS');
var_dump($product->getName());
var_dump($product->getOptions()[0]->toJson());
image

Kindly recheck the issue on Magento 2.4-develop branch and let us know if you are still facing any issues. Thank you.

engcom-November commented 1 year ago

Hi @GamerHegi64 , We are closing this issue as there has been no latest update on the same. Kindly reopen / create new issue if you are still facing any issues. Thank you.