无法使用Codeception在多站点上使用wp_query

Sta*_*lga 5 php wordpress codeception

我试图设置一个测试来计算数据库中自定义帖子的数量,作为更强大的测试的第一步,但它不能正常工作.这是测试的副本:

<?php
// codecept run wpunit RWA:CourseModelsTest

namespace RWA;

require_once get_template_directory() . '/models/course.php';

class CourseModelsTest extends \Codeception\TestCase\WPTestCase {
    /**
     * @var \WpunitTester
     */
    protected $tester;

    protected function _before() {

    }

    protected function _after() {
    }

    // tests
    public function testSomeFeature() {

        $query = \Course::count();
        $this->assertEquals( 53, $query );
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的模型中,这是函数的样子

public static function count() {
    $args = array(
        'posts_per_page' => -1,
        'offset' => 0,
        'post_type' => 'courses',
        'suppress_filters' => true,
    );
    $query = new \WP_Query( $args );
    return count( $query->posts );
}
Run Code Online (Sandbox Code Playgroud)

但当它得到断言时,它表示53不等于0.

本地我正在运行多站点wordpress实例,但我不确定这是否会影响设置,或者这是否需要在代码中配置.有人有什么想法吗?

更新:这是我有一些当前配置文件的副本:

paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
modules:
config:
    WPDb:
        dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%'
        user: '%DB_USER%'
        password: '%DB_PASSWORD%'
        dump: 'tests/_data/dump.sql'
        populate: true #import the dump before the tests
        cleanup: false #import the dump between tests
        url: '%WP_URL%'
        urlReplacement: true #replace the hardcoded dump URL with the one above
        tablePrefix: '%TABLE_PREFIX%'
        waitlock: 0
    WPBrowser:
        url: '%WP_URL%'
        adminUsername: '%ADMIN_USERNAME%'
        adminPassword: '%ADMIN_PASSWORD%'
        adminPath: '%WP_ADMIN_PATH%'
    WPFilesystem:
        wpRootFolder: '%WP_ROOT_FOLDER%'
        plugins: '/wp-content/plugins'
        mu-plugins: '/wp-content/mu-plugins'
        themes: '/wp-content/themes'
        uploads: '/wp-content/uploads'
    WPLoader:
        wpRootFolder: "%WP_ROOT_FOLDER%"
        dbName: "%TEST_DB_NAME%"
        dbHost: "%TEST_DB_HOST%"
        dbUser: "%TEST_DB_USER%"
        dbPassword: "%TEST_DB_PASSWORD%"
        tablePrefix: "%TEST_TABLE_PREFIX%"
        domain: "%WP_DOMAIN%"
        adminEmail: "%ADMIN_EMAIL%"
        title: "Test"
        theme: retirement-wealth-academy
        plugins: ['']
        activatePlugins: ['']
    WPWebDriver:
        url: '%WP_URL%'
        adminUsername: '%ADMIN_USERNAME%'
        adminPassword: '%ADMIN_PASSWORD%'
        adminPath: '%WP_ADMIN_PATH%'
        capabilities:
            browserName: "chrome"
            javascriptEnabled: true
            webStorageEnabled: true
            nativeEvents: true
extensions:
enabled:
    - Codeception\Extension\RunFailed
commands:
    - Codeception\Command\GenerateWPUnit
    - Codeception\Command\GenerateWPRestApi
    - Codeception\Command\GenerateWPRestController
    - Codeception\Command\GenerateWPRestPostTypeController
    - Codeception\Command\GenerateWPAjax
    - Codeception\Command\GenerateWPCanonical
    - Codeception\Command\GenerateWPXMLRPC
    - Codeception\Command\DbSnapshot
    - tad\Codeception\Command\SearchReplace
params:
- .env
Run Code Online (Sandbox Code Playgroud)

而对于我的特定套房

actor: WpunitTester
modules:
    enabled:
        - WPLoader
        - \Helper\Wpunit
        - WPDb
Run Code Online (Sandbox Code Playgroud)

我没有针对正确的数据库,我不知道我做错了什么.我在wploader的模块文件中找到了一个useBlog方法,并且是一个多站点选项,但是无法让它做我想做的事 - 在博客2上通过类(类im测试)使用wp_query.

Sta*_*lga 2

经过查阅大量文档和github问题,我已经解决了这个问题!我使用的数据库转储非常大,而且我在运行测试后注意到,我的帖子和 postmeta 表丢失了所有数据(这解释了查询返回 0)。使用 populator 来处理 WPDb 模块中的 sql 加载可以解决很多问题。如果你的 sql 转储很大(我的大约 26 兆),你应该使用 populator。

  WPDb:
    dsn: 'mysql:host=127.0.0.1;dbname=ffwpcore_test'
    user: '%DB_USER%'
    password: '%DB_PASSWORD%'
    dump: 'tests/_data/dump.sql'
    populate: true #import the dump before the tests
    cleanup: false #import the dump between tests
    url: '%WP_URL%'
    urlReplacement: true #replace the hardcoded dump URL with the one above
    tablePrefix: '%TABLE_PREFIX%'
    waitlock: 0
    populator: '/Applications/MAMP/Library/bin/mysql  --host=localhost -uroot -proot $dbname < $dump'
Run Code Online (Sandbox Code Playgroud)

我现在从 wp_query 获得了返回,现在可以进一步开发我的测试。