Fixture.php 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * PHP Unit test suite for Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @category   EcomDev
 * @package    EcomDev_PHPUnit
14
 * @copyright  Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
 */

/**
 * Fixture resource model.
 *
 * Created for direct operations with DB.
 *
 */
class EcomDev_PHPUnit_Model_Mysql4_Fixture extends Mage_Core_Model_Mysql4_Abstract
{
    protected function _construct()
    {
        $this->_setResource('ecomdev_phpunit');
    }

    /**
     * Cleans table in test database
     *
     * @param string $tableEntity
     * @return EcomDev_PHPUnit_Model_Mysql4_Fixture
     */
    public function cleanTable($tableEntity)
    {
40 41 42 43 44 45 46 47 48
    	try {
	        $this->_getWriteAdapter()
	            ->delete($this->getTable($tableEntity));
    	} catch (Exception $e) {
    		throw new EcomDev_PHPUnit_Model_Mysql4_Fixture_Exception(
    			sprintf('Unable to clear records for a table "%s"', $tableEntity),
    			$e
    		);
    	}
49 50 51 52 53 54 55 56 57 58 59
        return $this;
    }

    /**
     * Loads multiple data rows into table
     *
     * @param string $tableEntity
     * @param array $tableData
     */
    public function loadTableData($tableEntity, $tableData)
    {
Ivan Chepurnyi's avatar
Ivan Chepurnyi committed
60 61 62 63 64 65 66 67
        $tableColumns = $this->_getWriteAdapter()
            ->describeTable($this->getTable($tableEntity));

        $records = array();
        foreach ($tableData as $row) {
            $records[] = $this->_getTableRecord($row, $tableColumns);
        }

68 69 70 71 72 73 74 75 76 77 78 79
        try {
	        $this->_getWriteAdapter()->insertOnDuplicate(
	            $this->getTable($tableEntity),
	            $records
	        );
        } catch (Exception $e) {
        	throw new EcomDev_PHPUnit_Model_Mysql4_Fixture_Exception(
    			sprintf('Unable to insert/update records for a table "%s"', $tableEntity), 
    			$e
    		);
        }
        
80 81
        return $this;
    }
Ivan Chepurnyi's avatar
Ivan Chepurnyi committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	/**
     * Prepares entity table record from array
     *
     * @param array $row
     * @param array $tableColumns list of entity_table columns
     * @return array
     */
    protected function _getTableRecord($row, $tableColumns)
    {
        $record = array();

        // Fullfil table records with data
        foreach ($tableColumns as $columnName => $definition) {
            if (isset($row[$columnName])) {
                $record[$columnName] = $this->_getTableRecordValue($row[$columnName]);
            } elseif ($definition['DEFAULT'] !== null) {
                $record[$columnName] = $definition['DEFAULT'];
            } else {
                $record[$columnName] = (($definition['NULLABLE']) ? null : '');
            }
        }

        return $record;
    }

    /**
     * Processes table record values,
     * used for transforming custom values like serialized
     * or JSON data
     *
     *
     * @param mixed $value
     * @return string
     */
    protected function _getTableRecordValue($value)
    {
        // If it is scalar php type, then just return itself
        if (!is_array($value)) {
            return $value;
        }

        if (isset($value['json'])) {
            return Mage::helper('core')->jsonEncode($value['json']);
        }

        if (isset($value['serialized'])) {
            return serialize($value['serialized']);
        }

        throw new InvalidArgumentException('Unrecognized type for DB column');
    }
134
}