0

I've created an API extension that manages gallery easily for our demands and now I wanted to create a unit testing.

For example, one functionality is: give an 'sku' and 'entry' object (with base64 img) and API adds this image on that product.

This is the class I want to test:

<?php

namespace Gsp\ApiExtension\Model;

use Gsp\ApiExtension\Api\SGIMediaInterface;
use Magento\Framework\Api\ImageContentValidatorInterface;
use Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface;
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Framework\App\ObjectManager;

class SGIMedia implements SGIMediaInterface {

    /**
     * @var StoreRepositoryInterface $storeRepository
     */
    protected $storeRepository;

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

    /**
     * @var ImageContentValidatorInterface
     */
    protected $contentValidator;

    /**
     * @var ProductAttributeMediaGalleryManagementInterface
     */
    protected $galleryManagement;

    /**
     * @param StoreRepositoryInterface $storeRepository
     * @param ProductAttributeMediaGalleryManagementInterface $galleryManagement
     * @param ProductRepositoryInterface $productRepository
     * @param ImageContentValidatorInterface $contentValidator
     */
    public function __construct(StoreRepositoryInterface $storeRepository, ProductAttributeMediaGalleryManagementInterface $galleryManagement, ProductRepositoryInterface $productRepository, ImageContentValidatorInterface $contentValidator) {
        $this->productRepository = $productRepository;
        $this->contentValidator = $contentValidator;
        $this->galleryManagement = $galleryManagement;
        $this->storeRepository = $storeRepository;
    }

    /**
     * @inheritdoc
     */
    public function update($skus, ProductAttributeMediaGalleryEntryInterface $entry) {
        $sku_array = explode('|', $skus);

        foreach ($sku_array as $sku) {
            $this->galleryManagement->create($sku, $entry);
        }
    }

(...)

}

1) I need to mock a product, I've already did that, but then all attributes are NULL, so I would need to set an SKU at least. setData is not working. I'm stuck here.

I did this:

/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;

protected function setUp() {
    $this->product = $this->createMock(\Magento\Catalog\Model\Product::class, [], [], '', false);

    $this->product->setSku('test');
    var_dump($this->product->getSku()); //returns NULL

    $this->product->setData('sku', 'test');
    var_dump($this->product->getData('sku')); //returns NULL

    $this->product->save();
    var_dump($this->product->getData('sku')); //returns NULL
 }

2) Then I get my model and call my method passing this SKU and a fixed entry as a parameters.

3) Verify the product has this entry.

By the way, I'm very new at unit testing and on Magento too.

  • can you also post the method you are trying to test? maybe the full class you are testing? – Marius Dec 22 '16 at 16:43
  • I added my class – slayerbleast Dec 22 '16 at 17:04
3

Use $product->method('getSku')->willReturn('test'); Mocks can be configured in many ways! It is worth reading the documentation here: https://phpunit.de/manual/current/en/test-doubles.html

  • But doing this will not help at the next step, I think. I want to call the function update passing an sku as parameter. I need that sku is associated at this product – slayerbleast Dec 22 '16 at 17:03
  • I think you really want to write an integration test using real objects instead of a unit test. – Vinai Dec 22 '16 at 17:05
  • Well I don't know, all I need is a product that could do this functionality about adding an entry object inside it and verify that entry is correct. Can't do that a mock object? – slayerbleast Dec 22 '16 at 17:31
  • 3
    if you need "real" product functionality, you really might need an integration test. You might want to have a look at how to create fixtures in m2. devdocs.magento.com/guides/v2.0/mtf/mtf_entities/… It allows you to create a product object just for your test and then test against it's functionalities – David Verholen Dec 22 '16 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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