Skip to content

Commit

Permalink
0.2.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrico committed Oct 9, 2017
1 parent 3ba4aa0 commit aa34e6f
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tmp/
composer.lock
vendor/
/phpunit.xml
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="coverage.xml" />
<log type="coverage-html" target="tmp" />
</logging>
</phpunit>
2 changes: 1 addition & 1 deletion src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function bootstrap() {
return FALSE;
}

self::$done = add_action( 'admin_menu', function () {
self::$done = (bool) add_action( 'admin_menu', function () {
Boxes::init();
AdminNotices::init();
}, 0 );
Expand Down
12 changes: 9 additions & 3 deletions src/BoxInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ final class BoxInfo implements \ArrayAccess {
*/
public function __construct( string $title, string $id = '', string $context = '', string $priority = '' ) {

( $priority && in_array( $priority, self::PRIORITIES, TRUE ) ) or $priority = self::PRIORITY_ADVANCED;
( $context && in_array( $context, self::CONTEXTS, TRUE ) ) or $context = self::CONTEXT_ADVANCED;
$priority = in_array( $priority, self::PRIORITIES, TRUE )
? $priority
: self::PRIORITY_ADVANCED;

$context = in_array( $context, self::CONTEXTS, TRUE )
? $context
: self::CONTEXT_ADVANCED;

$id or $id = sanitize_title_with_dashes( $title );

$this->storage = compact( 'title', 'id', 'context', 'priority' );
Expand Down Expand Up @@ -108,7 +114,7 @@ public function offsetExists( $offset ) {
*/
public function offsetGet( $offset ) {

return $this->offsetExists( $offset ) ? $this->meta[ $offset ] : null;
return $this->offsetExists( $offset ) ? $this->meta[ $offset ] : NULL;
}

/**
Expand Down
91 changes: 60 additions & 31 deletions tests/src/AdminNoticesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace MetaboxOrchestra\Tests;

use MetaboxOrchestra\AdminNotices;
use Brain\Monkey\Functions;
use MetaboxOrchestra\AdminNotices;

/**
* @author Giuseppe Mazzapica <[email protected]>
Expand All @@ -32,31 +32,35 @@ public function testAddedNoticesAreRecorded() {
$notices->add( 'This is an error', 'Error!', AdminNotices::ERROR );
$notices->add( 'This is a success', 'Success!', AdminNotices::SUCCESS );

Functions\expect( 'doing_action' )->with( 'shutdown' )->andReturn( TRUE );
Functions\expect( 'doing_action' )
->with( 'shutdown' )
->andReturn( TRUE );

Functions\expect( 'update_user_option' )
->once()
->with( 1, AdminNotices::OPTION_NAME, \Mockery::type( 'array' ) )
->andReturnUsing( function ( $id, $option, $messages ) {

static::assertArrayHasKey( 'screen_test', $messages );
static::assertArrayHasKey( AdminNotices::ERROR, $messages[ 'screen_test' ] );
static::assertArrayHasKey( AdminNotices::SUCCESS, $messages[ 'screen_test' ] );
static::assertInternalType( 'array', $messages[ 'screen_test' ][ AdminNotices::ERROR ] );
static::assertInternalType( 'array', $messages[ 'screen_test' ][ AdminNotices::SUCCESS ] );
static::assertCount( 1, $messages[ 'screen_test' ][ AdminNotices::ERROR ] );
static::assertCount( 1, $messages[ 'screen_test' ][ AdminNotices::SUCCESS ] );

$error = reset( $messages[ 'screen_test' ][ AdminNotices::ERROR ] );
$success = reset( $messages[ 'screen_test' ][ AdminNotices::SUCCESS ] );

static::assertInternalType( 'array', $error );
static::assertInternalType( 'array', $success );
static::assertContains( 'This is an error', $error );
static::assertContains( 'Error!', $error );
static::assertContains( 'This is a success', $success );
static::assertContains( 'Success!', $success );
} );
->andReturnUsing(
function ( $id, $option, $messages ) {

static::assertArrayHasKey( 'screen_test', $messages );
static::assertArrayHasKey( AdminNotices::ERROR, $messages[ 'screen_test' ] );
static::assertArrayHasKey( AdminNotices::SUCCESS, $messages[ 'screen_test' ] );
static::assertInternalType( 'array', $messages[ 'screen_test' ][ AdminNotices::ERROR ] );
static::assertInternalType( 'array', $messages[ 'screen_test' ][ AdminNotices::SUCCESS ] );
static::assertCount( 1, $messages[ 'screen_test' ][ AdminNotices::ERROR ] );
static::assertCount( 1, $messages[ 'screen_test' ][ AdminNotices::SUCCESS ] );

$error = reset( $messages[ 'screen_test' ][ AdminNotices::ERROR ] );
$success = reset( $messages[ 'screen_test' ][ AdminNotices::SUCCESS ] );

static::assertInternalType( 'array', $error );
static::assertInternalType( 'array', $success );
static::assertContains( 'This is an error', $error );
static::assertContains( 'Error!', $error );
static::assertContains( 'This is a success', $success );
static::assertContains( 'Success!', $success );
}
);

$notices->record();
}
Expand All @@ -71,20 +75,35 @@ public function testAddedNoticesArePrinted() {
$notices->add( 'This is an error', 'Error!', AdminNotices::ERROR );
$notices->add( 'This is a success', 'Success!', AdminNotices::SUCCESS );

$to_print = null;
$to_print = NULL;

Functions\expect( 'doing_action' )->once()->with( 'shutdown' )->andReturn( TRUE );
Functions\expect( 'doing_action' )
->once()
->with( 'shutdown' )
->andReturn( TRUE );
Functions\expect( 'update_user_option' )
->andReturnUsing( function ( $id, $option, $messages ) use ( &$to_print ) {
$to_print = $messages;
return TRUE;
} );
->andReturnUsing(
function ( $id, $option, $messages ) use ( &$to_print ) {

$to_print = $messages;

return TRUE;
}
);

$notices->record();

Functions\expect( 'doing_action' )->once()->with( 'admin_notices' )->andReturn( TRUE );
Functions\expect( 'get_user_option' )->once()->with( AdminNotices::OPTION_NAME, 123 )->andReturn( $to_print );
Functions\expect( 'delete_user_option' )->once()->with( 123, AdminNotices::OPTION_NAME );
Functions\expect( 'doing_action' )
->once()
->with( 'admin_notices' )
->andReturn( TRUE );
Functions\expect( 'get_user_option' )
->once()
->with( AdminNotices::OPTION_NAME, 123 )
->andReturn( $to_print );
Functions\expect( 'delete_user_option' )
->once()
->with( 123, AdminNotices::OPTION_NAME );
Functions\expect( 'esc_html' )->andReturnFirstArg();
Functions\expect( 'wp_kses_post' )->andReturnFirstArg();

Expand All @@ -99,4 +118,14 @@ public function testAddedNoticesArePrinted() {
static::assertContains( 'This is an error', $output );
static::assertContains( 'This is a success', $output );
}

public function testAddNoUserId() {

Functions\expect( 'get_current_user_id' )
->once()
->andReturn( FALSE );

$testee = new AdminNotices();
static::assertInstanceOf( AdminNotices::class, $testee->add( '' ) );
}
}
32 changes: 32 additions & 0 deletions tests/src/BootstrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php # -*- coding: utf-8 -*-

namespace MetaboxOrchestra\Tests;

use Brain\Monkey\Actions;
use Brain\Monkey\Functions;
use MetaboxOrchestra\Bootstrap;

class BootstrapTest extends TestCase {

public function testBasic() {

$testee = new Bootstrap();
static::assertInstanceOf( Bootstrap::class, $testee );
}

public function testBootstrap() {

Functions\when( 'is_admin' )->justReturn( TRUE );
Actions\expectAdded( 'admin_menu' );

static::assertTrue( Bootstrap::bootstrap() );
static::assertFalse( Bootstrap::bootstrap() );
}

public function testBootstrapInFrontend() {

Functions\when( 'is_admin' )->justReturn( FALSE );

static::assertFalse( Bootstrap::bootstrap() );
}
}
102 changes: 102 additions & 0 deletions tests/src/BoxInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php # -*- coding: utf-8 -*-

namespace MetaboxOrchestra\Tests;

use Brain\Monkey\Functions;
use MetaboxOrchestra\BoxInfo;

class BoxInfoTest extends TestCase {

public function testBasic() {

Functions\stubs( [ 'sanitize_title_with_dashes' ] );

$testee = new BoxInfo( 'foo' );
static::assertInstanceOf( \ArrayAccess::class, $testee );

static::assertSame( 'foo', $testee->title() );
static::assertSame( 'foo', $testee->id() );
static::assertSame( BoxInfo::PRIORITY_ADVANCED, $testee->priority() );
static::assertSame( BoxInfo::CONTEXT_ADVANCED, $testee->context() );
}

public function testConstructorId() {

Functions\stubs( [ 'sanitize_title_with_dashes' ] );
$expected = 'unique-id';
static::assertSame( $expected, ( new BoxInfo( 'foo', $expected ) )->id() );
}

/**
* @param string $input
* @param null|string $expected
*
* @dataProvider provideConstructorContext
*/
public function testConstructorContext( $input, $expected = NULL ) {

$expected = $expected ? : $input;
Functions\stubs( [ 'sanitize_title_with_dashes' ] );
static::assertSame(
$expected,
( new BoxInfo( 'foo', '', $input ) )->context()
);
}

public function provideConstructorContext() {

return [
'side' => [ BoxInfo::CONTEXT_SIDE ],
'normal' => [ BoxInfo::CONTEXT_NORMAL ],
'advanced' => [ BoxInfo::CONTEXT_ADVANCED ],
'invalid context' => [ 'foo', BoxInfo::CONTEXT_ADVANCED ]
];
}

/**
* @param string $input
* @param null|string $expected
*
* @dataProvider provideConstructorPriority
*/
public function testConstructorPriority( $input, $expected = NULL ) {

$expected = $expected ? : $input;
Functions\stubs( [ 'sanitize_title_with_dashes' ] );
static::assertSame(
$expected,
( new BoxInfo( 'foo', '', '', $input ) )->priority()
);
}

public function provideConstructorPriority() {

return [
'high' => [ BoxInfo::PRIORITY_HIGH ],
'sorted' => [ BoxInfo::PRIORITY_SORTED ],
'core' => [ BoxInfo::PRIORITY_CORE ],
'normal' => [ BoxInfo::PRIORITY_NORMAL ],
'advanced' => [ BoxInfo::PRIORITY_ADVANCED ],
'invalid priority' => [ 'foo', BoxInfo::PRIORITY_ADVANCED ]
];
}

public function testArrayAccess() {

Functions\stubs( [ 'sanitize_title_with_dashes' ] );
$testee = new BoxInfo( '' );

$key = 'foo';
$expected = 'bar';

$testee[ $key ] = $expected;

static::assertSame( $expected, $testee[ $key ] );
static::assertTrue( isset( $testee[ $key ] ) );

unset( $testee[ $key ] );

static::assertFalse( isset( $testee[ $key ] ) );
static::assertNull( $testee[ $key ] );
}
}
24 changes: 24 additions & 0 deletions tests/src/NoopBoxActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php # -*- coding: utf-8 -*-

namespace MetaboxOrchestra\Tests;

use MetaboxOrchestra\AdminNotices;
use MetaboxOrchestra\BoxAction;
use MetaboxOrchestra\NoopBoxAction;

class NoopBoxActionTest extends TestCase {

public function testBasic() {

$testee = new NoopBoxAction();
static::assertInstanceOf( BoxAction::class, $testee );

}

public function testSave() {

/** @var AdminNotices $stub */
$stub = \Mockery::mock( AdminNotices::class );
static::assertFalse( ( new NoopBoxAction() )->save( $stub ) );
}
}
28 changes: 28 additions & 0 deletions tests/src/NoopBoxViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php # -*- coding: utf-8 -*-

namespace MetaboxOrchestra\Tests;

use Brain\Monkey\Functions;
use MetaboxOrchestra\BoxInfo;
use MetaboxOrchestra\BoxView;
use MetaboxOrchestra\NoopBoxView;

class NoopBoxViewTest extends TestCase {

public function testBasic() {

$testee = new NoopBoxView();
static::assertInstanceOf( BoxView::class, $testee );

}

public function testRender() {

Functions\stubs( [ 'sanitize_title_with_dashes' ] );

static::assertSame(
'',
( new NoopBoxView() )->render( new BoxInfo( '', '', '' ) )
);
}
}

0 comments on commit aa34e6f

Please sign in to comment.