2016-12-29 5 views
1

Ich arbeite mit phpunit und versuche, eine Wordpress-Seite zu verspotten. Ich habe wenige Schritte gemacht, aber ich zähle nicht, ein bestimmtes Szenario zu erreichen.phpunit Mock Funktion innerhalb Wert

Folgende ist meine Mutter Funktion:

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

Im Anschluss an dem PHPUnit-Test-Code für den obigen Block:

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

Mein Problem ist ich in der Lage könnte nicht Datum diesen Block zu bieten:

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

Irgendwelche Hilfe.! Vielen Dank!

Antwort

1

Das Problem ist, dass der Code auf die Datenbank zugreifen. Sie sollten entweder das Dataset bereitstellen, damit der Code die richtigen Posts aus der Datenbank lädt (entweder mit Fixtures oder Factories), oder Sie sollten die Funktion get_posts stubben (indem Sie den Test in einen Namespace einfügen und dort die Funktion get_posts definieren) Namensraum)

Verwandte Themen