Basic release
This commit is contained in:
		@@ -0,0 +1,40 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Shy\WordPress\Tests;
 | 
			
		||||
 | 
			
		||||
use Shy\WordPress\HookableTrait;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Check that HookableTrait actually works.
 | 
			
		||||
 * 
 | 
			
		||||
 * @author Philipp Cordes <pc@irgendware.net>
 | 
			
		||||
 */
 | 
			
		||||
class HookableTraitTest extends \WP_UnitTestCase
 | 
			
		||||
{
 | 
			
		||||
	use HookableTrait;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	public function actionMethod()
 | 
			
		||||
	{
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function testWorksAsAction()
 | 
			
		||||
	{
 | 
			
		||||
		$this->addHookMethod( 'shywp_test_action', 'actionMethod' );
 | 
			
		||||
		$this->assertTrue( has_action( 'shywp_test_action' ), 'Registering an action via addHookMethod() worked.' );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	public function filterMethod( $value )
 | 
			
		||||
	{
 | 
			
		||||
		return $value;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function testWorksAsFilter()
 | 
			
		||||
	{
 | 
			
		||||
		$this->addHookMethod( 'shywp_test_filter', 'filterMethod' );
 | 
			
		||||
		$this->assertTrue( has_filter( 'shywp_test_filter' ), 'Registering a filter via addHookMethod() worked.' );
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,180 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Shy\WordPress\Tests;
 | 
			
		||||
 | 
			
		||||
use Shy\WordPress\SettingsPage;
 | 
			
		||||
use PHPUnit_Framework_MockObject_MockObject as MockObject;
 | 
			
		||||
use PHPUnit_Framework_MockObject_Builder_InvocationMocker as BuilderInvocationMocker;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SettingsPageTest extends \WP_UnitTestCase
 | 
			
		||||
{
 | 
			
		||||
	/**
 | 
			
		||||
	 * Mock a SettingsPage.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param string|null $slug
 | 
			
		||||
	 * @param string      $capability
 | 
			
		||||
	 * @return SettingsPage|MockObject {
 | 
			
		||||
	 *    @method BuilderInvocationMocker method(string)
 | 
			
		||||
	 * }
 | 
			
		||||
	 */
 | 
			
		||||
	protected function mockSettingsPage( $slug = null, $capability = 'manage_options' )
 | 
			
		||||
	{
 | 
			
		||||
		$builder = $this->getMockBuilder( 'Shy\WordPress\SettingsPage' )
 | 
			
		||||
			->enableProxyingToOriginalMethods();
 | 
			
		||||
 | 
			
		||||
		if ( null === $slug ) {
 | 
			
		||||
			$builder->disableOriginalConstructor();
 | 
			
		||||
		} else {
 | 
			
		||||
			$builder->setConstructorArgs( array( $slug, $capability ) );
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return $builder->getMock();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Test reading defaults from the settings page.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @covers SettingsPage::__construct()
 | 
			
		||||
	 * @covers SettingsPage::getDefaults()
 | 
			
		||||
	 * @covers SettingsPage::offsetExists()
 | 
			
		||||
	 * @covers SettingsPage::offsetGet()
 | 
			
		||||
	 * @expectedException OutOfBoundsException
 | 
			
		||||
	 */
 | 
			
		||||
	public function testReading()
 | 
			
		||||
	{
 | 
			
		||||
		$slug     = 'shywp_settingspage_test_slug_reading';
 | 
			
		||||
		$defaults = array( 'foo' => 'bar' );
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage( $slug );
 | 
			
		||||
		$page->method( 'getDefaults' )->willReturn( $defaults );
 | 
			
		||||
 | 
			
		||||
		$this->assertEquals( $defaults, get_option( $slug ) );
 | 
			
		||||
 | 
			
		||||
		$this->assertArrayHasKey( 'foo', $page );
 | 
			
		||||
		$this->assertEquals( $defaults['foo'], $page['foo'] );
 | 
			
		||||
 | 
			
		||||
		$this->assertArrayNotHasKey( 'baz', $page );
 | 
			
		||||
		$page['baz'];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Test writing to the settings page.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @covers SettingsPage::offsetSet()
 | 
			
		||||
	 * @expectedException OutOfBoundsException
 | 
			
		||||
	 */
 | 
			
		||||
	public function testWriting()
 | 
			
		||||
	{
 | 
			
		||||
		$slug     = 'shywp_settingspage_test_slug_writing';
 | 
			
		||||
		$defaults = array( 'foo' => 'bar' );
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage( $slug );
 | 
			
		||||
		$page->method( 'getDefaults' )->willReturn( $defaults );
 | 
			
		||||
 | 
			
		||||
		$page['foo'] = 'foo';
 | 
			
		||||
		$this->assertEquals( 'foo', $page['foo'] );
 | 
			
		||||
		$page['baz'] = '123';
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Fail to remove a setting.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @covers SettingPage::offsetUnset()
 | 
			
		||||
	 * @expectedException BadMethodCallException
 | 
			
		||||
	 */
 | 
			
		||||
	public function testRemoving()
 | 
			
		||||
	{
 | 
			
		||||
		$slug = 'shywp_settingspage_test_slug_removing';
 | 
			
		||||
		$defaults = array();
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage( $slug );
 | 
			
		||||
		$page->method( 'getDefaults' )->willReturn( $defaults );
 | 
			
		||||
 | 
			
		||||
		unset( $page['baz'] );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Test whether the settings page can be showed.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @covers SettingsPage::__construct()
 | 
			
		||||
	 * @covers SettingsPage::getParentSlug()
 | 
			
		||||
	 * @covers SettingsPage::getPageTitle()
 | 
			
		||||
	 * @covers SettingsPage::getMenuTitle()
 | 
			
		||||
	 */
 | 
			
		||||
	public function testRegisterPage()
 | 
			
		||||
	{
 | 
			
		||||
		$this->expectOutputRegex( '/<page&title>/' );
 | 
			
		||||
 | 
			
		||||
		$slug = 'shywp_settingspage_test_slug_registerpage';
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage( $slug );
 | 
			
		||||
		$page->method( 'getParentSlug' )->willReturn( 'index.php' );
 | 
			
		||||
		$page->method( 'getPageTitle' )->willReturn( '<page&title>' );
 | 
			
		||||
		$page->method( 'getMenuTitle' )->willReturn( '<menu&title>' );
 | 
			
		||||
 | 
			
		||||
		$page->expects( $this->once() )->method( 'registerPage' )->with();
 | 
			
		||||
		$page->expects( $this->once() )->method( 'registerSettings' )->with();
 | 
			
		||||
 | 
			
		||||
		// FIXME: Simulate display of backend.
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @covers SettingsPage::sanitizeOptions()
 | 
			
		||||
	 */
 | 
			
		||||
	public function testSanitize()
 | 
			
		||||
	{
 | 
			
		||||
		$slug = 'shywp_settingspage_test_slug_sanitize';
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage( $slug );
 | 
			
		||||
		$page->method( 'sanitizeOptions' )->will( $this->returnArgument( 0 ) );
 | 
			
		||||
		$page->expects( $this->atLeastOnce() )->method( 'sanitizeOptions' );
 | 
			
		||||
 | 
			
		||||
		$this->markTestIncomplete();
 | 
			
		||||
		// FIXME: Simulate form submission
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function testRenderTextField()
 | 
			
		||||
	{
 | 
			
		||||
		$this->expectOutputRegex( '/^<input type="text"/' );
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage();
 | 
			
		||||
		$page->renderTextField( array(
 | 
			
		||||
			'label_for' => 'foo',
 | 
			
		||||
			'name'      => 'bar',
 | 
			
		||||
		) );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function testRenderCheckboxField()
 | 
			
		||||
	{
 | 
			
		||||
		$this->expectOutputRegex( '/^<label><input type="checkbox"/' );
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage();
 | 
			
		||||
		$page->renderCheckboxField( array(
 | 
			
		||||
			'label_for' => 'foo',
 | 
			
		||||
			'name'      => 'bar',
 | 
			
		||||
			'caption'   => 'baz',
 | 
			
		||||
		) );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function testRenderPage()
 | 
			
		||||
	{
 | 
			
		||||
		$this->markTestIncomplete();
 | 
			
		||||
		$this->expectOutputRegex( '/<form action="options.php" method="post">.*<3&>.*cryptic_teaser.*</form>/' );
 | 
			
		||||
 | 
			
		||||
		$slug   = 'shywp_settingspage_test_slug_renderpage';
 | 
			
		||||
 | 
			
		||||
		$page = $this->mockSettingsPage( $slug, 'read' );
 | 
			
		||||
		$page->method( 'getPageTitle' )->willReturn( '<3&>' );
 | 
			
		||||
		$page->method( 'renderSectionTeaser' )->will( $this->returnCallback( function () use ( $teaser ) {
 | 
			
		||||
			echo 'cryptic_teaser';
 | 
			
		||||
		} ) );
 | 
			
		||||
 | 
			
		||||
		// FIXME: Simulate view of the settings page
 | 
			
		||||
		$page->renderPage();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Try to load a Shy WordPress test class.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param string $name
 | 
			
		||||
 * @return boolean
 | 
			
		||||
 */
 | 
			
		||||
function shy_wordpress_tests_autoloader( $name )
 | 
			
		||||
{
 | 
			
		||||
	if ( substr( $name, 0, 20 ) !== 'Shy\\WordPress\\Tests\\' ) {
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	$name = __DIR__ . '/' . str_replace( '\\', DIRECTORY_SEPARATOR, $name ) . '.php';
 | 
			
		||||
 | 
			
		||||
	return is_file( $name ) && include( $name );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
spl_autoload_register( 'shy_wordpress_tests_autoloader' );
 | 
			
		||||
@@ -0,0 +1,14 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * PHPUnit bootstrap file
 | 
			
		||||
 * 
 | 
			
		||||
 * Variant of the one from github.com/tierra/wordpress-plugins-tests
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
require_once '../src/autoloader.php';
 | 
			
		||||
require_once 'autoloader.php';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
require_once ( getenv( 'WP_DEVELOP_DIR' ) ?: '../../../..' )
 | 
			
		||||
	. '/tests/phpunit/includes/bootstrap.php';
 | 
			
		||||
		Reference in New Issue
	
	Block a user