tddwizard / magento2-fixtures

Fixture library for Magento 2 integration tests by @schmengler (@integer-net)

Home Page:http://tddwizard.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

regionId missing despite being explicitly set

dsmithhayes opened this issue · comments

Hello,

I am testing a custom CRON job for a custom module which requires a full order to be placed first. When attempting to use the placeOrder() method from the Checkout it throws an error for the shipping address claiming the regionId is not set. The following is my setUp() method, and the test method. Identifying and confidential information has been redacted.

    protected function setUp()
    {
        $address = AddressBuilder::anAddress()
            ->withTelephone('redacted')
            ->withStreet('redacted')
            ->withCity('Hamilton')
            ->withRegionId('ON')    // sets the regionId, I would assume
            ->withCountryId('CA')
            ->withPostcode('redacted');

        $this->customerFixture = new CustomerFixture(
            CustomerBuilder::aCustomer()
                ->withEmail('test@redacted.com')
                ->withFirstname('Test')
                ->withLastname('Test')
                ->withAddresses($address->asDefaultShipping()->asDefaultBilling())
                ->build()
        );
    }

As you can see I set the regionId to ON for Ontario. The following is the actual testing code.

    public function testCronDispatch()
    {
        $this->assertEquals('test@redacted.com', $this->customerFixture->getEmail());

        $this->customerFixture->login();
        $checkout = CustomerCheckout::fromCart(
            CartBuilder::forCurrentSession()
                ->withSimpleProduct(ProductBuilder::aSimpleProduct()
                    ->withName('Product Name')
                    ->withPrice(100.00)
                    ->withSku('pn-15')
                    ->withCustomAttributes([ 'asset_id' => 1 ])
                    ->build()
                    ->getSku())
                ->build()
        );

        $order = $checkout->placeOrder();    // fails here

        /**
         * @var Push
         */
        $pushCron = Bootstrap::getObjectManager()->create(Push::class);
        $order = $pushCron->preparePayload($order);
        $this->assertArrayHasKey('order', $order);
    }

The output from the test is as follows:

1) Org\Module\Test\Integration\Cron\CronTest::testCronDispatch
Magento\Framework\Exception\LocalizedException: Please check the shipping address information. regionId is a required field.

/home/dsmithhayes/src/redacted/vendor/magento/module-quote/Model/QuoteValidator.php:79
/home/dsmithhayes/src/redacted/vendor/magento/module-quote/Model/QuoteManagement.php:449
/home/dsmithhayes/src/redacted/vendor/magento/module-quote/Model/QuoteManagement.php:406
/home/dsmithhayes/src/redacted/vendor/tddwizard/magento2-fixtures/src/Checkout/CustomerCheckout.php:164
/home/dsmithhayes/src/redacted/app/code/Org/Module/Test/Integration/Cron/CronTest.php:80

I am using Magento 2.2.4, Enterprise Edition, with PHP 7.0 on Ubuntu 16.04.

Region ID is a numeric value.

mysql> select * from directory_country_region where code = 'ON';
+-----------+------------+------+--------------+
| region_id | country_id | code | default_name |
+-----------+------------+------+--------------+
|        74 | CA         | ON   | Ontario      |
+-----------+------------+------+--------------+
1 row in set (0.01 sec)

Changing the argument to withRegionId(74) should resolve the issue.

True, unfortunately. 'ON' will be coerced to 0. We should add int type for the $regionId parameter to prevent that mistake.