-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
258 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
tmp/ | ||
composer.lock | ||
vendor/ | ||
/phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,8 @@ | |
|
||
namespace MetaboxOrchestra\Tests; | ||
|
||
use MetaboxOrchestra\AdminNotices; | ||
use Brain\Monkey\Functions; | ||
use MetaboxOrchestra\AdminNotices; | ||
|
||
/** | ||
* @author Giuseppe Mazzapica <[email protected]> | ||
|
@@ -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(); | ||
} | ||
|
@@ -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(); | ||
|
||
|
@@ -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( '' ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( '', '', '' ) ) | ||
); | ||
} | ||
} |