2

I’m working on an integration test that utilizes a custom model and am unable to load the object. Core objects are loading fine. I can create a quote using my own fixture without issue. As far as I can tell, the Account model and resource model are structured properly.

Consider the following (failing) test:

 /**
 * @magentoDbIsolation enabled
 * @magentoAppIsolation enabled
 * @magentoDataFixture loadFixture
 */
public function testOrderProvidesDiscountWithPromo(  ) {

    \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');

    /** @var  Mageplaza\Affiliate\Model\Account $affiliate */
    $affiliate = Bootstrap::getObjectManager()->create(Mageplaza\Affiliate\Model\Account::class);
    $affiliate->load(1, 'account_id');

    $quote = Bootstrap::getObjectManager()->create(\Magento\Quote\Model\Quote::class);
    $quote->load('test_promo', 'reserved_order_id');

    $this->assertEquals(1, $affiliate->getData('account_id'));
}

The affiliate account is created using the following fixture:

\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');

/** @var  Mageplaza\Affiliate\Model\Account $affiliate */
$affiliate = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Mageplaza\Affiliate\Model\Account::class);

$affiliate->setAccountId(1)
    ->setCustomerId(1)
    ->setCode('mycode')
    ->setGroupId(1)
    ->setBalance(0)
    ->setStatus(1)
    ->setTree(1);

$affiliate->save();

The $quote object is loading properly but the $affiliate one is not.

Can anyone see what I may be doing wrong or are there any extra steps that must be taken to ensure that the custom model loads properly as well?

(I'm using Magento 2.2 for what it’s worth)

1 Answer 1

2

The problem was that I was setting the account_id, which is idFieldName in the resource model.

As a result, when saving the object (ref. AbstractDb.php), $this->isObjectNotNew evaluates to TRUE because the idFieldName was set and the new object is not saved (Magento attempts to update the object instead):

public function save(\Magento\Framework\Model\AbstractModel $object)
{
    if ($object->isDeleted()) {
        return $this->delete($object);
    }

    $this->beginTransaction();

    try {
            ...

            if ($this->isObjectNotNew($object)) {
                $this->updateObject($object);
            } else {
                $this->saveNewObject($object);
            }

            ...

    return $this;
}

Solution

In the fixture above -- updated to:

$affiliate->setCustomerId(1)
    ->setCode('mycode')
    ->setGroupId(1)
    ->setBalance(0)
    ->setStatus(1)
    ->setTree(1);

$affiliate->save();

And Test (now passing) updated to:

/** @var  Mageplaza\Affiliate\Model\Account $affiliate */
$affiliate = Bootstrap::getObjectManager()->create(Mageplaza\Affiliate\Model\Account::class);
$affiliate->load('mycode', 'code');

$this->assertNotNull($affiliate->getData('account_id')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.