2016-12-29 13 views
1

私はphpunitと一緒に作業していて、wordpressのページを模擬しようとしています。私はいくつかのステップを実行しましたが、私は特定のシナリオを達成できませんでした。続き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; 
        } 
       } 
      } 
     } 
} 

後は、上記ブロックの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, '')); 
} 

私の問題は、私は、このブロックに日付を提供することができcouldntのです。

$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関数を名前空間)