سؤال

(أحاول حل مشكلات نمط نماذج التعليمات البرمجية)

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

لقد اتبعت هذه الخطوات:

  1. تم سحب اختبار الريبو إلى مجلد الويب الجذر الخاص بي:

    svn co http://develop.svn.wordpress.org/trunk/ wordpress-develop
    
  2. نسخ مجلد السمة الخاص بي إلى ووردبريس-تطوير/src/wp-content/themes

  3. إنشاء قاعدة بيانات فارغة وإعدادها wp-اختبارات-config.php ملف.تمت إضافة هذا السطر أيضًا إلى wp-اختبارات-config.php

    define( 'WP_DEFAULT_THEME', 'THEMENAME' );
    
  4. مخلوق ووردبريس-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!='');
        }
    }
    }
    

    enter image description here

  5. لكنني أريد استخدام بلدي Konser و PostType الطبقات.

    function setUp() {
    
       $this->post_id = new Konser(410);
       parent::setUp();
    }
    function test_ID() {
        $this->assertInternalType("int", $this->post_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');
    
  7. لكني أتلقى استدعاء دالة غير محددة 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.ليس من الضروري.

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