سؤال

هل يستخدم أي شخص هنا Zend Framework ، Zfdoctrine و phpunit معا؟

كيفية إعادة بناء قاعدة البيانات في كل اختبار؟ كيفية فصل بيئات الإنتاج/الإنتاج/الاختبار؟

هل يمكنك مشاركة إعداد اختبار الوحدة الخاص بك؟

لقد كنت أحاول شيئًا كهذا:

// /tests/bootstrap.php
// ... setup paths and constants here
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap('doctrine');
$provider = new ZFDoctrine_Tool_DoctrineProvider;
$provider->generateModelsFromYaml();
//$provider->buildProject(true);

لكن هذا ينتهي في:

Notice: Constant APPLICATION_PATH already defined in /home/user/www/library/ZendFramework/1.10.7/library/Zend/Tool/Project/Context/Zf/BootstrapFile.php on line 106

Fatal error: Call to a member function getResponse() on a non-object in /home/user/www/library/zf-doctrine/library/ZFDoctrine/Tool/DoctrineProvider.php on line 271

لا يتم إنشاء النماذج.

أحصل على أخطاء مماثلة تعمل:

$provider->createDatabase();

ولكن في هذه الحالة يتم إنشاء قاعدة بيانات.
أوامر المزود الأخرى لا تعمل.


الحل:

$provider = new ZFDoctrine_Tool_DoctrineProvider;
$registry = new Zend_Tool_Framework_Registry;
$provider->setRegistry($registry);
@$provider->buildProject(true);

إذا كان أي شخص يعرف نهجًا أفضل ، فالرجاء تصحيحني.

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

المحلول

لم أستخدم Zfdoctrine ، لكن مجرد عقيدة 1.2. لا أعرف ما إذا كان حلي أفضل ولكني كنت أحسب أن نشره إذا كان any1 مهتمًا ، فإليك bootstrap.php في مجلد الاختبارات الخاص بي:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));

// Define application environment
/**
 * In the application.ini:
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
doctrine.dsn = "mysql://my_user:passwd@localhost/my_phpunit_test_db"
 */
define('APPLICATION_ENV', 'testing');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path()
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/../configs/application.ini'
);

$application->getBootstrap()->bootstrap();

// Can run out if too small
ini_set('memory_limit', '512M');

// Get the doctrine settings
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run(array("doctrine", "build-all-reload","force"));

المفتاح هنا هو في الواقع السطر الأخير الذي يعيد بناء جميع قواعد البيانات التي تخلق بيئة نظيفة لكل اختبار.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top