...
 
Commits (2)
<?php
namespace MonApp;
/**
* Plugin Name: CustPluginTestPhpUnit
* Plugin URI:
* Description: test
* Version: 0.0.1
* Author: jp
* Author URI:
* License: GPL3
* License URI:
*/
class CustPluginTestPhpUnit{
private $resultPosts;
public function __construct() {
//add_action('admin_menu', array($this,'custpluginTestPhpUnit'));
//$this->recupPosts();
//print_r($this->getPostsIds());
//print_r($this->getPost(40)->post_title);
//print_r($this->getPost(40));
}
public function custPluginTestPhpUnit(){
add_menu_page(
'TestPhpUnitPage',
'Test Php Unit',
'manage_options',
'TestPhpUnitMenu',
array($this,'recupPosts'));
add_pages_page( 'titrePage', '','manage_options', 'TestPhpUnitMenu', array($this,'affichage'));
}
public function getPerma($id)
{
return get_permalink($id);
}
public function getPost($id)
{
return get_post($id);
}
public function getPostsIds() {
global $wpdb;
return $wpdb->get_col( "select post_title from {$wpdb->posts} WHERE post_type='post' AND post_title like 'POST_%' ORDER BY post_title LIMIT 1");
}
public function recupPosts()
{
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_title',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$this->resultPosts = get_posts($args);
}
public function affichePosts($resultPosts)
{
echo '<ul>';
foreach($resultPosts as $resultPost){
echo '<li>' . $resultPost->post_title . ' ID=' . $resultPost->ID . '</li>';
echo '<a href="' . $this->getPerma($resultPost->ID) . '">' .
$this->getPerma($resultPost->ID) . '</a>';
}
echo '</ul>';
}
public function affichage()
{
$this->affichePosts($this->resultPosts);
}
}
$myplugin = new CustPluginTestPhpUnit();
?>
\ No newline at end of file
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use MonApp\CustPluginTestPhpUnit;
use WP_Mock;
use Mockery;
class CustPluginTestPhpUnitTest extends TestCase
{
public function setUp() {
parent::setUp();
WP_Mock::setUp();
}
public function tearDown() {
parent::tearDown();
WP_Mock::tearDown();
}
function testgetPerma()
{
//wpFunction & wpPassthruFunction deprecated
WP_Mock::userFunction('get_permalink', array(
'args' => 40,
'times' => 1,
'return' => 'http://localhost:8888/wordpress/2018/01/09/post_date_event/'
) );
$p = new CustPluginTestPhpUnit();
$this->assertEquals('http://localhost:8888/wordpress/2018/01/09/post_date_event/',$p->getPerma(40));
}
public function testgetPost() {
global $post;
$post = new \stdClass;
$post->ID = 40;
$post->title = 'POST_1';
\WP_Mock::userFunction( 'get_post', array(
'times' => 1,
'args' => array( $post->ID ),
'return' => $post,
) );
$p = new CustPluginTestPhpUnit();
$this->assertEquals('POST_1',$p->getPost(40)->title);
}
/*function testgetPostsIds() {
global $wpdb;
$wpdb = Mockery::mock('\WPDB');
$wpdb->shouldReceive('get_col')
->once()
->with("select post_title from {$wpdb->posts} WHERE post_type='post' AND post_title like 'POST_%' ORDER BY post_title LIMIT 1")
->andReturn(array('POST_1'));
$wpdb->posts = 'wp_posts';
$post_ids = getPostIds();
$this->assertEquals( array('POST_1'), $post_ids );
}*/
}
?>
\ No newline at end of file