2016-12-29 2 views
1

Я работаю с phpunit и пытаюсь высмеять страницу wordpress. Я сделал несколько шагов, но я считаю, что не способен выполнить конкретный сценарий.phpunit mock function in value

Ниже Мой Родитель функция:

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; 
        } 
       } 
      } 
     } 
} 

Ниже приводится тест PHPUnit код выше блока:

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, '')); 
} 

Моя проблема, я не мог в состоянии предоставить дату в этом блоке:

$lastModified = get_posts($args); 
        if (isset($lastModified{0}->post_modified)) { 
         $postModified = $lastModified{0}->post_modified; 
        } else { 
         $postModified = $favouritePost->post_modified; 
        } 

Любая помощь.! Благодаря!

ответ

1

Проблема в том, что код доступа к базе данных. Вы должны либо предоставить набор данных, чтобы код загружал правильные сообщения из базы данных (используя либо светильники, либо фабрики), либо вы должны заглушить функцию get_posts (вы можете сделать это, поставив тест в пространстве имен и определяя функцию get_posts в этом пространство имен)