سؤال

ما هو أبسط إعداد لإنشاء اختبارات تكامل وحدة التحكم لوحدة Magento 1 المخصصة بدون وحدة Ecomdev؟

هل كانت مفيدة؟

المحلول

بفضل مساعدة Vinai ، إليك تهيئة أساسية تتيح لك إنشاء اختبارات تكامل لوحدة Magento 1 الخاصة بك.

سيكون كل ملف تم إنشاؤه أدناه أقل من app/code/<codePool>/Vendor/Module/Test

المساعد والتكوين

تحتاج أولاً إلى phpunit.xml.dist:

<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         colors="true"
         bootstrap="bootstrap.php"
         backupGlobals="false"
         verbose="true"
>
    <testsuites>
        <testsuite name="Module Integration Tests">
            <directory suffix="Test.php">.</directory>
        </testsuite>
    </testsuites>

</phpunit>

ثم تحتاج إلى إنشاء ملف bootstrap.php بسيط لتتمكن من الوصول إلى Mage

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

require __DIR__ . '/../../../../../../app/Mage.php';

function fix_error_handler()
{
    $mageErrorHandler = set_error_handler(function () {
        return false;
    });
    set_error_handler(function ($errno, $errstr, $errfile) use ($mageErrorHandler) {
        if (substr($errfile, -19) === 'Varien/Autoload.php') {
            return null;
        }
        return is_callable($mageErrorHandler) ?
            call_user_func_array($mageErrorHandler, func_get_args()) :
            false;
    });
}
fix_error_handler();
Mage::app();
Mage::setIsDeveloperMode(true);
fix_error_handler();
$_SESSION = [];

أنت الآن بحاجة إلى مساعد وحدة تحكم صغير سيتم استخدامه لإرسال الطلبات بشكل صحيح ، وعادة ما أقوم بإنشائه تحت Test/Controller/ControllerTestHelper.php:

<?php

class Vendor_Module_Test_Controller_ControllerTestHelper {

    final protected function dispatchRequest($module, $controller, $action)
    {
        $request = Mage::app()->getRequest();
        $request->setModuleName($module);
        $request->setControllerName($controller);
        $request->setActionName($action);

        Mage::app()->getStore()->setConfig('web/url/redirect_to_base', false);
        Mage::app()->getFrontController()->dispatch();
    }

    final public function dispatchPostRequest($module, $controller, $action)
    {
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $this->dispatchRequest($module, $controller, $action);
    }

    final public function dispatchGetRequest($module, $controller, $action)
    {
        $_SERVER['REQUEST_METHOD'] = 'GET';
        $this->dispatchRequest($module, $controller, $action);
    }
}

أخيرًا ، آخر شيء تحتاجه هو استجابة HTTP مخصصة لتجنب مسح الإخراج في وحدة التحكم وتجنب أخطاء مثل الرؤوس المرسلة بالفعل.

Test/Controller/HttpResponse :

<?php

class Vendor_Module_Test_Controller_HttpResponse extends \Mage_Core_Controller_Response_Http {

    public function canSendHeaders($throw = false)
    {
        return true;
    }

    public function sendHeaders()
    {
        return $this;
    }

    public function sendResponse()
    {
        $this->sendHeaders();

        if ($this->isException() && $this->renderExceptions()) {
            $exceptions = '';
            foreach ($this->getException() as $e) {
                $exceptions .= $e->__toString() . "\n";
            }
            echo $exceptions;
            return;
        }

        // Don't flush the output
        //$this->outputBody();
        // Return it instead
        return $this->_body;
    }
}

الاختبار الأول

جاهز للاختبار الأول ، لا تنسَ اللاحقة المُعلنة عام phpunit.xml.dist

لذا يمكنك إنشاء Test/Controller/MyControllerTest.php:

<?php

class Vendor_Module_Test_Controller_MyControllerTest extends \PHPUnit_Framework_TestCase {

    public function setUp()
    {
        // Stub response to avoid headers already sent problems
        $stubResponse = new \Vendor_Module_Test_Controller_HttpResponse();
        Mage::app()->setResponse($stubResponse);

        // Use a controller helper
        $controllerTestHelper = new \Vendor_Module_Test_Controller_ControllerTestHelper($this);
        // Dispatch the request
        $controllerTestHelper->dispatchGetRequest('custom', 'module', 'route');
    }

    public function testSomething()
    {
        // Get the header
        $headers = Mage::app()->getResponse()->getHeaders();

        // Get the response
        $response = Mage::app()->getResponse()->getBody(true);

        // Test something
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top