استخدام فصولي الخاصة في اختبارات وحدة Wp
-
09-12-2019 - |
سؤال
(أحاول حل مشكلات نمط نماذج التعليمات البرمجية)
أقوم بتطوير موضوع ولكن لدي بعض الفصول التي تم تطويرها لهذا الموضوع.أريد إجراء اختبارات الوحدة على موضوعي.
لقد اتبعت هذه الخطوات:
تم سحب اختبار الريبو إلى مجلد الويب الجذر الخاص بي:
svn co http://develop.svn.wordpress.org/trunk/ wordpress-develop
نسخ مجلد السمة الخاص بي إلى ووردبريس-تطوير/src/wp-content/themes
إنشاء قاعدة بيانات فارغة وإعدادها wp-اختبارات-config.php ملف.تمت إضافة هذا السطر أيضًا إلى wp-اختبارات-config.php
define( 'WP_DEFAULT_THEME', 'THEMENAME' );
مخلوق ووردبريس-develop/tests/phpunit/tests/konser.php ملف بهذا المحتوى:
<?php class Test_Konser extends WP_UnitTestCase { public function test_firstTest(){ // Arrange $id = $this->factory->post->create(array( 'post_type' => 'konser', )); add_post_meta($id, "konser_tarih", time()); $ids = get_posts(array( 'post_type' => 'konser', ) ); foreach ($ids as $id) { $this->assertInstanceOf('WP_Post',$id); $ss = get_post_meta($id->ID, "konser_tarih", true); $this->assertTrue($ss!=''); } } }
لكنني أريد استخدام بلدي
Konser
وPostType
الطبقات.function setUp() { $this->post_id = new Konser(410); parent::setUp(); } function test_ID() { $this->assertInternalType("int", $this->post_id); }
تمت إضافة هذا السطرين إلى /wordpress-develop/tests/phpunit/includes/functions.php
require_once('src/wp-content/themes/THEMENAME/include/PostType.php'); require_once('src/wp-content/themes/THEMENAME/include/Konser.php');
لكني أتلقى استدعاء دالة غير محددة get_post() في konsertv-develop/src/wp-content/themes/THEMENAME/include/PostType.php
هل يمكنك أن تخبرني ما الذي أفتقده؟
محتوى PostType.php
<?php
class PostType
{
public $post;
public $ID;
function PostType($ID) {
$this->post = get_post($ID);
$this->ID = $ID;
}
function getPost()
{
return $this->post;
}
function getTarih()
{
return get_post_meta($this->ID, "konser_tarih", true);
}
function getID()
{
return $this->ID;
}
}
المحلول
6 .تمت إضافة هذا السطرين إلى
/wordpress-develop/tests/phpunit/includes/functions.php
require_once('src/wp-content/themes/THEMENAME/include/PostType.php');
require_once('src/wp-content/themes/THEMENAME/include/Konser.php');
هذه هي المشكلة.ال get_post()
الدالة غير موجودة، لأن phpunit/includes/functions.php
يتم تضمين الملف قبل تحميل WordPress.وهذا في الواقع جزء من سبب وجوده كملف منفصل:لذلك يمكن تضمينه في phpunit bootstrap الخاص بك (إذا كنت بحاجة إلى واحد، وتحتاج إلى استخدام هذه الوظائف).للحصول على مثال لكيفية القيام بذلك للمكونات الإضافية، يمكنك أن ترى تعليمي لقد كتبت.
ومع ذلك، في حالتك، أنت تختبر قالبًا، ومجموعة الاختبار الخاصة بك ليست معقدة بدرجة كافية بحيث تضمن التمهيد الخاص بها.في الواقع، خطوتك الثالثة هي الإضافة define( 'WP_DEFAULT_THEME', 'THEMENAME' );
إلى wp-tests-config.php
يجب أن تكون كافية.الملفين أنت require
سيتم تحميل ing تلقائيًا كجزء من موضوعك.(نعم، يتم تحميل السمة أثناء اختبارات الوحدة.)
لذا كان خطأك هو الخطوة 6.ليس من الضروري.