EcomDev PHPUnit Tip #11

For years, the test framework EcomDev_PHPUnit is quasi-standard for Magento unit tests. The current version is 0.3.7 and last state of official documentation is version 0.2.0 – since then, much has changed which you have to search yourself in code and GitHub issues. This series shall collect practical usage tips.

Tip #11: Minimal Fixtures For Product Types

Below are some example fixtures for different product types. They are as minimal as possible to create products and their relationships. In most cases you will want to add more attributes like name, visibility, status, price and stock status.

Configurable Product

eav:
  catalog_product:
    - entity_id: 1
      sku: simple-1
      type_id: simple
      attribute_set_id: 4
      color: green
    - entity_id: 2
      sku: simple-2
      type_id: simple
      attribute_set_id: 4
      color: red
    - entity_id: 3
      sku: conf-1
      type_id: configurable
      attribute_set_id: 4
      super_attributes:
        - color
      configurable_children:
        - 1
        - 2

This fixture creates a configurable product with two associated simple products (color: green and color:red), given that the color attribute has these values. For extension tests on a virgin Magento instance you could create these attribute values as follows:

tables:
  eav_attribute_option:
    - option_id: 3
      attribute_id: 92
      sort_order: 0
    - option_id: 4
      attribute_id: 92
      sort_order: 0
  eav_attribute_option_value:
    - value_id: 3
      option_id: 3
      store_id: 0
      value: green
    - value_id: 4
      option_id: 4
      store_id: 0
      value: red

The “color” attribute already exists with ID 92 and the eav_attribute_option table contains only the options with ID 1 and 2 by default (“Male” and “Female” for “gender”). We begin our fixture definition with ID 3, so that these will not be deleted.

Bundle Product (Variant 1)

eav:
  catalog_product:
    - entity_id: 1
      sku: simple-1
      type_id: simple
      attribute_set_id: 4
    - entity_id: 2
      sku: simple-2
      type_id: simple
      attribute_set_id: 4
    - entity_id: 3
      sku: simple-3
      type_id: simple
      attribute_set_id: 4
    - entity_id: 4
      sku: simple-4
      type_id: simple
      attribute_set_id: 4
    - entity_id: 5
      sku: bundle-1
      type_id: bundle
      attribute_set_id: 4
      bundle_options:
        1: [1, 2]
        2: [3, 4]

This fixture creates a bundle product with two selections (selection_id 1 and 2), with the simple products 1 and 2 respectively 3 and 4 as options. The selection type always is “radio” and it is not possible to define additional settings like “required” or “qty”. If you need these, you have to fill the bundle tables directly, see variant 2:

Bundle Products (Variant 2)

If we fill the bundle tables directly we can configure bundle products freely:

eav:
  catalog_product:
    -
      entity_id: 1
      type_id: bundle
      has_options: 1
      required_options: 1
      sku: bundle
    -
      entity_id: 2
      type_id: simple
      sku: fix
    -
      entity_id: 3
      type_id: simple
      sku: select1
    -
      entity_id: 4
      type_id: simple
      sku: select2
tables:
  bundle/option:
    -
      option_id: 1
      parent_id: 1
      required: 1
      type: checkbox
      position: 0
    -
      option_id: 2
      parent_id: 1
      required: 1
      type: select
      position: 1
    -
      option_id: 3
      parent_id: 1
      required: 0
      type: select
      position: 1
  bundle/option_value:
    -
      value_id: 1
      option_id: 1
      store_id: 1
      title: Fix
    -
      value_id: 2
      option_id: 2
      store_id: 1
      title: Select
  bundle/selection:
    -
      selection_id: 1
      option_id: 1
      parent_product_id: 1
      product_id: 2
      is_default: 1
      selection_qty: 1
    -
      selection_id: 2
      option_id: 2
      parent_product_id: 1
      product_id: 3
      is_default: 0
      selection_qty: 1
    -
      selection_id: 3
      option_id: 2
      parent_product_id: 1
      product_id: 4
      is_default: 0
      selection_qty: 1
    -
      selection_id: 4
      option_id: 3
      parent_product_id: 1
      product_id: 2
      is_default: 1
      selection_qty: 1

Grouped Product

eav:
  catalog_product:
    - entity_id: 1
      type_id: simple
      sku: simple-1
      attribute_set_id: 4
    - entity_id: 2
      type_id: simple
      sku: simple-2
      attribute_set_id: 4
    - entity_id: 3
      type_id: grouped
      sku: set
      attribute_set_id: 4
tables:
  catalog/product_link:
    - link_id: 1
      product_id: 3
      linked_product_id: 1
      link_type_id: 3
    - link_id: 2
      product_id: 3
      linked_product_id: 2
      link_type_id: 3

As of today (EcomDev_PHPUnit 0.3.7) there is no special syntax as for the configurable products, so that we need to create the relationship directly in the catalog_product_link table.