Widgets: Create WP_Widget_Mock as a mock of WP_Widget which can be used for widget tests.

You cannot instantiate an abstract class. Not even in WordPress world.

See #35981.

git-svn-id: https://develop.svn.wordpress.org/trunk@37427 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2016-05-12 21:24:18 +00:00
parent c1ec341ddb
commit e2329076f3

View File

@ -336,7 +336,7 @@ class Tests_Widgets extends WP_UnitTestCase {
* @see WP_Widget::form() * @see WP_Widget::form()
*/ */
function test_wp_widget_form() { function test_wp_widget_form() {
$widget = new WP_Widget( 'foo', 'Foo' ); $widget = new WP_Widget_Mock( 'foo', 'Foo' );
ob_start(); ob_start();
$retval = $widget->form( array() ); $retval = $widget->form( array() );
$output = ob_get_clean(); $output = ob_get_clean();
@ -350,7 +350,7 @@ class Tests_Widgets extends WP_UnitTestCase {
function test_wp_widget_constructor() { function test_wp_widget_constructor() {
$id_base = 'foo'; $id_base = 'foo';
$name = 'Foo'; $name = 'Foo';
$foo_widget = new WP_Widget( $id_base, $name ); $foo_widget = new WP_Widget_Mock( $id_base, $name );
$this->assertEquals( $id_base, $foo_widget->id_base ); $this->assertEquals( $id_base, $foo_widget->id_base );
$this->assertEquals( $name, $foo_widget->name ); $this->assertEquals( $name, $foo_widget->name );
@ -368,7 +368,7 @@ class Tests_Widgets extends WP_UnitTestCase {
$control_options = array( $control_options = array(
'id_base' => 'bar_id_base', 'id_base' => 'bar_id_base',
); );
$bar_widget = new WP_Widget( $id_base, $name, $widget_options, $control_options ); $bar_widget = new WP_Widget_Mock( $id_base, $name, $widget_options, $control_options );
$this->assertEquals( $widget_options['classname'], $bar_widget->widget_options['classname'] ); $this->assertEquals( $widget_options['classname'], $bar_widget->widget_options['classname'] );
$this->assertEquals( $control_options['id_base'], $bar_widget->control_options['id_base'] ); $this->assertEquals( $control_options['id_base'], $bar_widget->control_options['id_base'] );
} }
@ -379,7 +379,7 @@ class Tests_Widgets extends WP_UnitTestCase {
* *
*/ */
function test_wp_widget_get_field_name( $expected, $value_to_test ) { function test_wp_widget_get_field_name( $expected, $value_to_test ) {
$widget = new WP_Widget( 'foo', 'Foo' ); $widget = new WP_Widget_Mock( 'foo', 'Foo' );
$widget->_set( 2 ); $widget->_set( 2 );
$this->assertEquals( $expected, $widget->get_field_name( $value_to_test ) ); $this->assertEquals( $expected, $widget->get_field_name( $value_to_test ) );
} }
@ -430,7 +430,7 @@ class Tests_Widgets extends WP_UnitTestCase {
* *
*/ */
function test_wp_widget_get_field_id( $expected, $value_to_test ) { function test_wp_widget_get_field_id( $expected, $value_to_test ) {
$widget = new WP_Widget( 'foo', 'Foo' ); $widget = new WP_Widget_Mock( 'foo', 'Foo' );
$widget->_set( 2 ); $widget->_set( 2 );
$this->assertEquals( $expected, $widget->get_field_id( $value_to_test ) ); $this->assertEquals( $expected, $widget->get_field_id( $value_to_test ) );
} }
@ -503,7 +503,7 @@ class Tests_Widgets extends WP_UnitTestCase {
function test_wp_widget_is_preview() { function test_wp_widget_is_preview() {
global $wp_customize; global $wp_customize;
$widget = new WP_Widget( 'foo', 'Foo' ); $widget = new WP_Widget_Mock( 'foo', 'Foo' );
$this->assertEmpty( $wp_customize ); $this->assertEmpty( $wp_customize );
$this->assertFalse( $widget->is_preview() ); $this->assertFalse( $widget->is_preview() );
@ -679,3 +679,14 @@ class Tests_Widgets extends WP_UnitTestCase {
} }
} }
/**
* Mock of WP_Widget.
*
* @since 4.6.0
*/
class WP_Widget_Mock extends WP_Widget {
public function widget( $args, $instance ) {
return;
}
};