phpunit mock函数里面的值

时间:2016-12-29 06:50:49

标签: php unit-testing mocking phpunit

我正在使用phpunit并尝试模拟wordpress页面。我已经完成了一些步骤,但我认为无法实现特定情况。

以下是My Parent功能:

CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                      NAMES
b984b6659d20        mysql:5.7                "docker-entrypoint.sh"   8 minutes ago       Up 7 minutes        3306/tcp                                   myre_mysql_run_1
d34d8974912c        myre_php-apache-engine   "/usr/local/bin/entry"   20 hours ago        Up 7 minutes        80/tcp                                     myre_php_apache_engine_dev
981c2a7fa83b        mysql:5.7                "docker-entrypoint.sh"   20 hours ago        Up 47 seconds       3306/tcp                                   myre_mysql_dev
472531e09d08        jwilder/nginx-proxy      "/app/docker-entrypoi"   20 hours ago        Up 7 minutes        0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   nginx_proxy

以下是上述块的PHPunit测试代码:

public static function getMyFavouritesList()
    {
        $userId = get_current_user_id();
        $myFavouites = get_user_meta($userId, 'wpfp_favorites', true);
        $myIncidents = get_user_meta($userId, 'favourite_incidents', true);

        $pages = array();
        $forumPosts = array();
        $userPosts = array();
        $incidents = array();

        $pageId = get_the_ID();

        if (is_array($myFavouites)) {
            foreach ($myFavouites as $favourite) {

                if ($pageId == $favourite) {
                    continue;
                }

                $favouritePost = get_post($favourite);
                if (!empty($favouritePost) && is_object($favouritePost)) {

                    // Get post last updated date from post reply
                    $args = array(
                        'orderby' => 'date',
                        'order' => 'DESC',
                        'posts_per_page' => '1',
                        'post_type' => 'reply',
                        'post_parent' => $favouritePost->ID
                    );//Facing issue while providing data to this block.
                    $lastModified = get_posts($args);
                    if (isset($lastModified{0}->post_modified)) { 
                        $postModified = $lastModified{0}->post_modified;
                    } else {
                        $postModified = $favouritePost->post_modified;
                    }

                    $favouriteDetail = array(
                        'id' => $favouritePost->ID,
                        'title' => $favouritePost->post_title,
                        'url' => get_permalink($favouritePost->ID),
                        'date' => date("d F Y", strtotime($postModified)),
                    );
                    if (in_array($favouritePost->post_type, array('page'))) {

                        $pages[] = $favouriteDetail;
                    } elseif (in_array($favouritePost->post_type, array('forum', 'topic'))) {

                        $forumPosts[] = $favouriteDetail;
                    }
                }
            }
        }
}

我的问题是我无法为此块提供日期:

public function testGetMyFavouritesListForPage($post, $currentPostId, $userMeta, $expected, $userPosts)
    {
        $GLOBALS['post'] = $post;
        $GLOBALS['get_ID'] = $currentPostId;
        $GLOBALS['user_meta'] = $userMeta;
        $GLOBALS['posts'] = $userPosts;

        $result = MyFavourites::getMyFavouritesList();
        $this->assertEquals($expected, $result);
    }


public function dataProviderForMyFavourites()
    {
        $page = new \stdClass();
        $page->ID = 1;
        $page->post_title = 'page title';
        $page->post_modified = '2014-06-30 14:50:04';
        $page->post_type = 'page';
$favouriteIncidents = array( 0 => array('id' => '15', 'name' => 'Incident name'));

$incidentsExpected = '<div class="row-fluid favourites-pages">';
        $incidentsExpected .='<h3 class="underlined">Pages</h3>';
        $incidentsExpected .='<table class="phpnew-favourites-list table table-striped table-hover span12">';
        $incidentsExpected .='<tbody><tr><td class="span6"><strong>Page</strong></td>';
        $incidentsExpected .='<td class="span2"><strong>Last updated</strong></td>';
        $incidentsExpected .='<td class="span2"><strong>Remove</strong></td>';
        $incidentsExpected .='<td class="span2"><strong>Print</strong></td></tr>';
        $incidentsExpected .='<tr><td><a href="http://support-centre.com/?p=1">page title</a></td>';
        $incidentsExpected .='<td>26 August 2014</td>';
        $incidentsExpected .='<td><a href="?wpfpaction=remove&postid=1" class="unfavourite"><img src="/wp-content/themes/phpnew/images/grey-star.png" title="Unfavourite" /></a></td>';
        $incidentsExpected .='<td><a href="http://php-centre.com/?p=1?print=pdf" ';
        $incidentsExpected .='target="_blank"><img src="/wp-content/themes/phpnew/images/phpnew-health-print.png" title="Download PDF" /></a></td></tr></tbody></table>';
        $incidentsExpected .='</div><div class="row-fluid favourites-fourms">';
        $incidentsExpected .='<h3 class="underlined">Forum posts</h3>';
        $incidentsExpected .='<p>You have no favourite forum posts</p></div>';
        $incidentsExpected .='<div class="row-fluid favourites-fourms">';
        $incidentsExpected .='<h3 class="underlined">Your posts</h3>';
        $incidentsExpected .="<p>You haven't created any forum posts yet</p></div>";
        $incidentsExpected .='<div class="row-fluid favourites-incidents"><h3 class="underlined">Incidents</h3>';
        $incidentsExpected .='<table class="phpnew-favourites-list table table-striped table-hover span12"><tbody><tr>';
        $incidentsExpected .='<td class="span6"><strong>Incidents</strong></td><td class="span2">&nbsp;</td>';
        $incidentsExpected .='<td class="span2"><strong>Remove</strong></td><td class="span2">&nbsp;</td></tr>';
        $incidentsExpected .='<tr><td><a href="/incidents/?incidentid=15&incidentname=Incident%20name">Incident name</a></td><td>&nbsp;</td>';
        $incidentsExpected .='<td><a href="?incidentid=15&wpfpactionIncident=remove" class="unfavourite">';
        $incidentsExpected .='<img src="/wp-content/themes/phpnew/images/grey-star.png" title="Unfavourite" /></a></td><td>&nbsp;</td></tr>';
        $incidentsExpected .='</tbody></table></div>';
return array(array($page, 1, $favouriteIncidents,  $incidentsExpected, ''));
}

任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

问题是代码访问数据库。您应该提供数据集,因此代码从数据库加载正确的帖子(使用fixture或工厂),或者您应该存根get_posts函数(您可以通过将测试放在命名空间中并在其中定义get_posts函数来完成此操作)命名空间)