Skip to content

Commit

Permalink
Widget tested
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Sep 14, 2024
1 parent 32dfd5f commit db60873
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="true" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/phpunit/bootstrap.php" backupGlobals="true" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache">
<php>
<const name="MSLS_PLUGIN_PATH" value="multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
<const name="MSLS_PLUGIN__FILE__" value="/var/www/html/wp-content/plugins/multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
Expand Down
23 changes: 21 additions & 2 deletions tests/phpunit/TestMslsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ class TestMslsWidget extends MslsUnitTestCase {
protected function setUp(): void {
parent::setUp();

\Mockery::mock( '\WP_Widget' );
$this->test = new MslsWidget();
}

public function test_init(): void {
$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'is_excluded' )->once()->andReturn( false );

Functions\expect( 'msls_options' )->once()->andReturn( $options );
Functions\expect( 'register_widget' )->once()->with( MslsWidget::class );

$this->test = \Mockery::mock( MslsWidget::class )->makePartial();
$this->expectNotToPerformAssertions();

$this->test->init();
}

public function test_widget(): void {
Expand Down Expand Up @@ -53,4 +63,13 @@ public function test_update(): void {
$result = $this->test->update( array( 'title' => 'xyz' ), array( 'title' => 'abc' ) );
$this->assertEquals( array( 'title' => 'xyz' ), $result );
}

public function test_form(): void {
$expected = '<p><label for="title">Title:</label> <input class="widefat" id="title" name="title" type="text" value="" /></p>';

$this->expectOutputString( $expected );

$result = $this->test->form( array() );
$this->assertEquals( $expected, $result );
}
}
18 changes: 18 additions & 0 deletions tests/phpunit/WP_Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace lloc\MslsTests;

class WP_Widget {


public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
}

public function get_field_id( $id ) {
return $id;
}

public function get_field_name( $name ) {
return $name;
}
}
3 changes: 3 additions & 0 deletions tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class_alias( \lloc\MslsTests\WP_Widget::class, '\WP_Widget' );

0 comments on commit db60873

Please sign in to comment.