2024-08-13

This commit is contained in:
gcch 2024-08-13 15:28:49 +02:00
commit 6b27b6d5e6
30 changed files with 6781 additions and 2 deletions

View file

@ -0,0 +1,278 @@
<?php
use Mockery as M;
use Carbon_Fields\Pimple\Container as PimpleContainer;
use Carbon_Fields\Container\Container;
use Carbon_Fields\Container\Repository as ContainerRepository;
use Carbon_Fields\Toolset\Key_Toolset;
use Carbon_Fields\Container\Condition\Factory as ConditionFactory;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* @group container
* @coversDefaultClass Carbon_Fields\Container\Container
*/
class ContainerTest extends WP_UnitTestCase {
public $containerId;
public $containerTitle;
public $containerType;
public $containerTypeUpperCase;
public $containerTypeSpaced;
public $containerTypeInvalid;
public $containerTypeClass;
public $containerTypeDatastoreClass;
public $containerTypeBrokenClass;
public $containerConditionFulfillableCollection;
public $containerConditionTranslatorJson;
public function setUp(): void {
$ioc = new PimpleContainer();
$ioc['container_repository'] = function( $ioc ) {
return new ContainerRepository();
};
$ioc['key_toolset'] = function( $ioc ) {
return new Key_Toolset();
};
$ioc['container_condition_factory'] = function( $ioc ) {
return new ConditionFactory( $ioc['container_conditions'] );
};
$ioc['container_condition_translator_array'] = function( $ioc ) {
return new \Carbon_Fields\Container\Fulfillable\Translator\Array_Translator( $ioc['container_condition_factory'] );
};
$ioc['container_condition_translator_json'] = function( $ioc ) {
return new \Carbon_Fields\Container\Fulfillable\Translator\Json_Translator( $ioc['container_condition_factory'] );
};
$ioc['container_condition_fulfillable_collection'] = $ioc->factory( function( $ioc ) {
return M::mock( 'Carbon_Fields\\Container\\Fulfillable\\Fulfillable_Collection' )->shouldIgnoreMissing();
} );
$ioc['container_conditions'] = function() {
return new PimpleContainer();
};
\Carbon_Fields\Carbon_Fields::instance()->install( $ioc );
$this->containerId = 'PageSettings';
$this->containerTitle = 'Page Settings';
$this->containerType = 'post_meta';
$this->containerTypeUpperCase = 'Post_Meta';
$this->containerTypeSpaced = 'Post Meta';
$this->containerTypeInvalid = '__no_such_container_type__';
$this->containerTypeClass = 'Carbon_Fields\Container\Post_Meta_Container';
$this->containerTypeDatastoreClass = 'Carbon_Fields\Datastore\Post_Meta_Datastore';
$this->containerTypeBrokenClass = 'Carbon_Fields\Container\Broken_Container';
$this->containerConditionFulfillableCollection = $ioc['container_condition_fulfillable_collection'];
$this->containerConditionTranslatorJson = $ioc['container_condition_translator_json'];
}
public function tearDown(): void {
\Carbon_Fields\Carbon_Fields::instance()->install( new PimpleContainer() );
}
/**
* @covers ::make
* @covers ::factory
*/
public function testMakePostMetaContainer() {
$container = Container::factory( $this->containerType, $this->containerTitle );
$this->assertEquals( $this->containerTitle, $container->title );
}
/**
* @covers ::make
* @covers ::factory
*
* @expectedException Carbon_Fields\Exception\Incorrect_Syntax_Exception
* @expectedExceptionMessage Unknown container "".
*/
public function testExceptionIsThrownWhenContainerTypeIsEmpty() {
$container = Container::factory( '', $this->containerTitle );
}
/**
* @covers ::make
* @covers ::factory
*
* @expectedException Carbon_Fields\Exception\Incorrect_Syntax_Exception
* @expectedExceptionMessage Unknown container "__no_such_container_type__".
*/
public function testExceptionIsThrownWhenContainerTypeIsInvalid() {
$container = Container::factory( $this->containerTypeInvalid, $this->containerTitle );
}
/**
* @covers ::make
* @covers ::factory
*/
public function testBrokenContainerIsReturnedWhenDebugIsDisabledAndContainerIsInvalid() {
$old_val = Incorrect_Syntax_Exception::$throw_errors;
Incorrect_Syntax_Exception::$throw_errors = false;
$container = Container::factory( $this->containerTypeInvalid, $this->containerTitle );
$this->assertInstanceOf( $this->containerTypeBrokenClass, $container );
Incorrect_Syntax_Exception::$throw_errors = $old_val;
}
/**
* @covers ::make
* @covers ::factory
*/
public function testContainerTypeCaseIsIgnored() {
$container = Container::factory( $this->containerTypeUpperCase, $this->containerTitle );
$this->assertInstanceOf( $this->containerTypeClass, $container );
}
/**
* @covers ::make
* @covers ::factory
*/
public function testSpacesInContainerTypeAreSupported() {
$container = Container::factory( $this->containerTypeSpaced, $this->containerTitle );
$this->assertTrue(true); // no exception should be thrown
}
/**
* @covers ::make
* @covers ::factory
*/
public function testSameContainerNamesDoNotGenerateIdenticalIds() {
$container1 = Container::factory( $this->containerType, $this->containerTitle );
$container2 = Container::factory( $this->containerType, $this->containerTitle );
$this->assertNotEquals( $container1->get_id(), $container2->get_id() );
}
/**
* @covers ::make
* @covers ::factory
*/
public function testSameContainerNameAddsNumericalSuffix() {
$container1 = Container::factory( $this->containerType, $this->containerTitle );
$container2 = Container::factory( $this->containerType, $this->containerTitle );
$this->assertEquals( $container1->get_id() . '1', $container2->get_id() );
}
/**
* @covers ::__construct
*
* @expectedException Carbon_Fields\Exception\Incorrect_Syntax_Exception
* @expectedExceptionMessage Empty container title is not supported
*/
public function testExceptionIsThrownWhenContainerTitleIsEmpty() {
$container = new $this->containerTypeClass( $this->containerId, '', $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
}
/**
* @covers ::__construct
*/
public function testNonAsciiContainerTitlesAreHandledProperly() {
// This text includes a capital cyrillic letter ... it actually assures that
// container titles in non-english are converted to lowercase
$container = new $this->containerTypeClass( $this->containerId, 'bulgarian: България', $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$this->assertEquals( 'bulgarian: България', $container->title );
}
/**
* @covers ::set_datastore
* @covers ::get_datastore
* @covers ::has_default_datastore
*/
public function testSetDatastore_Default_ReturnDefault() {
$datastore = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$container = new $this->containerTypeClass( $this->containerId, $this->containerTitle, $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$this->assertEquals( true, $container->has_default_datastore() );
$this->assertInstanceOf( $this->containerTypeDatastoreClass, $container->get_datastore() );
$container->set_datastore( $datastore, true );
$this->assertEquals( true, $container->has_default_datastore() );
$this->assertEquals( $datastore, $container->get_datastore() );
}
/**
* @covers ::set_datastore
* @covers ::get_datastore
* @covers ::has_default_datastore
*/
public function testSetDatastore_DefaultDefault2_ReturnDefault2() {
$datastore1 = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$datastore2 = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$container = new $this->containerTypeClass( $this->containerId, $this->containerTitle, $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$container->set_datastore( $datastore1, true );
$container->set_datastore( $datastore2, true );
$this->assertEquals( $datastore2, $container->get_datastore() );
}
/**
* @covers ::set_datastore
* @covers ::get_datastore
* @covers ::has_default_datastore
*/
public function testSetDatastore_Override_ReturnOverride() {
$datastore = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$container = new $this->containerTypeClass( $this->containerId, $this->containerTitle, $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$container->set_datastore( $datastore, false );
$this->assertEquals( false, $container->has_default_datastore() );
$this->assertEquals( $datastore, $container->get_datastore() );
}
/**
* @covers ::set_datastore
* @covers ::get_datastore
* @covers ::has_default_datastore
*/
public function testSetDatastore_OverrideOverride2_ReturnOverride2() {
$datastore1 = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$datastore2 = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$container = new $this->containerTypeClass( $this->containerId, $this->containerTitle, $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$container->set_datastore( $datastore1, false );
$container->set_datastore( $datastore2, false );
$this->assertEquals( false, $container->has_default_datastore() );
$this->assertEquals( $datastore2, $container->get_datastore() );
}
/**
* @covers ::set_datastore
* @covers ::get_datastore
* @covers ::has_default_datastore
*/
public function testSetDatastore_DefaultOverride_ReturnOverride() {
$default_datastore = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$override_datastore = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$container = new $this->containerTypeClass( $this->containerId, $this->containerTitle, $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$container->set_datastore( $default_datastore, true );
$this->assertEquals( true, $container->has_default_datastore() );
$this->assertEquals( $default_datastore, $container->get_datastore() );
$container->set_datastore( $override_datastore, false );
$this->assertEquals( false, $container->has_default_datastore() );
$this->assertEquals( $override_datastore, $container->get_datastore() );
}
/**
* @covers ::set_datastore
* @covers ::get_datastore
* @covers ::has_default_datastore
*/
public function testSetDatastore_DefaultOverrideDefault2_ReturnOverride() {
$default_datastore = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$override_datastore = M::mock( $this->containerTypeDatastoreClass )->shouldIgnoreMissing();
$container = new $this->containerTypeClass( $this->containerId, $this->containerTitle, $this->containerType, $this->containerConditionFulfillableCollection, $this->containerConditionTranslatorJson );
$container->set_datastore( $default_datastore, true );
$container->set_datastore( $override_datastore, false );
$container->set_datastore( $default_datastore, true );
$this->assertEquals( false, $container->has_default_datastore() );
$this->assertEquals( $override_datastore, $container->get_datastore() );
}
}

View file

@ -0,0 +1,343 @@
<?php
use Mockery as M;
use Carbon_Fields\Pimple\Container as PimpleContainer;
use Carbon_Fields\Container\Fulfillable\Fulfillable_Collection;
use Carbon_Fields\Container\Condition\Factory as ConditionFactory;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* @coversDefaultClass Carbon_Fields\Container\Fulfillable\Fulfillable_Collection
*/
class FulfillableCollectionTest extends WP_UnitTestCase {
public $subject;
public $condition_factory;
public function setUp(): void {
$ioc = new PimpleContainer();
$ioc->register( new \Carbon_Fields\Provider\Container_Condition_Provider() );
\Carbon_Fields\Carbon_Fields::instance()->install( $ioc );
$this->subject = $ioc['container_condition_fulfillable_collection'];
$this->condition_factory = $ioc['container_condition_factory'];
}
public function tearDown(): void {
M::close();
$this->condition_factory = null;
$this->subject = null;
}
/**
* @covers ::where
* @covers ::or_where
* @covers ::is_fulfilled
*/
public function testWhereWithBasicArgumentsIsFulfilled() {
$this->subject->where( 'post_type', '=', 'post' );
$environment = array( 'post_type' => 'post' );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithBasicArgumentsIsNotFulfilled() {
$this->subject->where( 'post_type', '=', 'post' );
$environment = array( 'post_type' => 'page' );
$this->assertFalse( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithSkippedComparerIsFulfilled() {
$this->subject->where( 'post_type', 'post' );
$environment = array( 'post_type' => 'post' );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithSkippedComparerIsNotFulfilled() {
$this->subject->where( 'post_type', 'post' );
$environment = array( 'post_type' => 'page' );
$this->assertFalse( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithArrayArgumentsIsFulfilled() {
$this->subject->where( array(
array(
'type' => 'post_type',
'value' => 'post',
),
) );
$environment = array( 'post_type' => 'post' );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithArrayArgumentsIsNotFulfilled() {
$this->subject->where( array(
array(
'type' => 'post_type',
'value' => 'post',
),
) );
$environment = array( 'post_type' => 'page' );
$this->assertFalse( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithCallableIsFulfilled() {
$this->subject->where( function( $c ) {
$c->where( 'post_type', 'post' );
} );
$environment = array( 'post_type' => 'post' );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
*/
public function testWhereWithCallableIsNotFulfilled() {
$this->subject->where( function( $c ) {
$c->where( 'post_type', 'post' );
} );
$environment = array( 'post_type' => 'page' );
$this->assertFalse( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::where
* @covers ::is_fulfilled
* @expectedException Carbon_Fields\Exception\Incorrect_Syntax_Exception
*/
public function testWhereWithUnsupportedConditionTypeThrowsException() {
$this->subject->where( 'unsupported_condition_type', 'post' );
}
/**
* @covers ::add_fulfillable
* @covers ::get_fulfillables
*/
public function testAddFulfillableAddsFulfillable() {
$fulfillable = M::mock( 'Carbon_Fields\\Container\\Fulfillable\\Fulfillable' );
$fulfillable_comparison = 'AND';
$this->assertSame( 0, count( $this->subject->get_fulfillables() ) );
$this->subject->add_fulfillable( $fulfillable, $fulfillable_comparison );
$this->assertSame( 1, count( $this->subject->get_fulfillables() ) );
$fulfillables = $this->subject->get_fulfillables();
$this->assertSame( $fulfillable, $fulfillables[0]['fulfillable'] );
$this->assertSame( $fulfillable_comparison, $fulfillables[0]['fulfillable_comparison'] );
}
/**
* @covers ::add_fulfillable
* @covers ::get_fulfillables
* @expectedException Carbon_Fields\Exception\Incorrect_Syntax_Exception
*/
public function testAddFulfillableThrowsExceptionOnInvalidComparison() {
$fulfillable = M::mock( 'Carbon_Fields\\Container\\Fulfillable\\Fulfillable' );
$fulfillable_comparison = 'UNSUPPORTED_COMPARISON_TYPE';
$this->subject->add_fulfillable( $fulfillable, $fulfillable_comparison );
}
/**
* @covers ::filter
*/
public function testFilterRemovesUnlistedConditionTypes() {
$this->subject->where( 'post_id', 1 );
$this->subject->where( 'post_type', 'post' );
$filtered = $this->subject->filter( array( 'post_type' ) );
$this->assertTrue( count( $filtered->get_fulfillables() ) === 1, 'Filtered collection should contain 1 fulfillable only' );
$this->assertTrue( count( $this->subject->get_fulfillables() ) === 2, 'Subject collection should remain unmodified' );
}
/**
* @covers ::filter
*/
public function testFilterRemovesUnlistedConditionTypesRecursively() {
$this->subject->where( 'post_type', 'post' );
$this->subject->where( function( $c ) {
$c->where( 'post_id', 1 );
$c->where( function( $c ) {
$c->where( 'post_id', 1 );
} );
} );
$filtered = $this->subject->filter( array( 'post_type' ) );
$this->assertTrue( count( $filtered->get_fulfillables() ) === 1, 'Filtered collection should contain 1 fulfillable only' );
$this->assertTrue( count( $this->subject->get_fulfillables() ) === 2, 'Subject collection should remain unmodified' );
}
/**
* @covers ::evaluate
*/
public function testEvaluateReplacesFulfilledConditionsWithBooleanTrue() {
$this->subject->where( 'post_id', 1 );
$this->subject->where( 'post_template', 'default' );
$evaluated = $this->subject->evaluate( array( 'post_id' ), array( 'post_id' => 1 ) );
$ef = $evaluated->get_fulfillables();
$expected = array(
array(
'type' => 'boolean',
'value' => true,
),
array(
'type' => 'post_template',
'value' => 'default',
),
);
$received = array();
foreach ( $ef as $tuple ) {
$received[] = array(
'type' => $this->condition_factory->get_type( get_class( $tuple['fulfillable'] ) ),
'value' => $tuple['fulfillable']->get_value(),
);
}
$this->assertSame( $expected, $received );
}
/**
* @covers ::evaluate
*/
public function testEvaluateReplacesFailingConditionsWithBooleanFalse() {
$this->subject->where( 'post_id', 1 );
$this->subject->where( 'post_template', 'default' );
$evaluated = $this->subject->evaluate( array( 'post_id' ), array( 'post_id' => 2 ) );
$ef = $evaluated->get_fulfillables();
$expected = array(
array(
'type' => 'boolean',
'value' => false,
),
array(
'type' => 'post_template',
'value' => 'default',
),
);
$received = array();
foreach ( $ef as $tuple ) {
$received[] = array(
'type' => $this->condition_factory->get_type( get_class( $tuple['fulfillable'] ) ),
'value' => $tuple['fulfillable']->get_value(),
);
}
$this->assertSame( $expected, $received );
}
/**
* @covers ::evaluate
*/
public function testEvaluateReplacesComparisonOperatorForBoolean() {
$this->subject->where( 'post_id', '>', 1 );
$this->subject->where( 'post_template', 'default' );
$evaluated = $this->subject->evaluate( array( 'post_id' ), array( 'post_id' => 1 ) );
$ef = $evaluated->get_fulfillables();
$expected = array(
array(
'type' => 'boolean',
'comparison_operator' => '=',
'value' => false,
),
array(
'type' => 'post_template',
'comparison_operator' => '=',
'value' => 'default',
),
);
$received = array();
foreach ( $ef as $tuple ) {
$received[] = array(
'type' => $this->condition_factory->get_type( get_class( $tuple['fulfillable'] ) ),
'comparison_operator' => $tuple['fulfillable']->get_comparison_operator(),
'value' => $tuple['fulfillable']->get_value(),
);
}
$this->assertSame( $expected, $received );
}
/**
* @covers ::is_fulfilled
*/
public function testIsFulfilledWhenEmpty() {
$environment = array( );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::is_fulfilled
*/
public function testIsFulfilledWithSingleConditionWhenAllConditionsAreFulfilled() {
$this->subject->where( 'post_type', 'post' );
$environment = array( 'post_type' => 'post' );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::is_fulfilled
*/
public function testIsFulfilledWithMultipleConditionsWhenAllConditionsAreFulfilled() {
$this->subject->where( 'post_type', 'post' );
$this->subject->where( 'post_id', 1 );
$environment = array( 'post_type' => 'post', 'post_id' => 1 );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::is_fulfilled
*/
public function testIsFulfilledWithNestedConditionsWhenAllConditionsAreFulfilled() {
$this->subject->where( 'post_type', 'post' );
$this->subject->where( function( $c ) {
$c->where( 'post_id', 1 );
} );
$environment = array( 'post_type' => 'post', 'post_id' => 1 );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::is_fulfilled
*/
public function testIsFulfilledWhenOneConditionIsFulfilled() {
$this->subject->where( 'post_id', 1 );
$this->subject->or_where( 'post_type', 'post' );
$environment = array( 'post_id' => 1, 'post_type' => 'page' );
$this->assertTrue( $this->subject->is_fulfilled( $environment ) );
}
/**
* @covers ::is_fulfilled
*/
public function testIsNotFulfilledWhenNoConditionsAreFulfilled() {
$this->subject->where( 'post_id', 1 );
$this->subject->or_where( 'post_type', 'post' );
$environment = array( 'post_id' => 0, 'post_type' => 'page' );
$this->assertFalse( $this->subject->is_fulfilled( $environment ) );
}
}

View file

@ -0,0 +1,265 @@
<?php
use Mockery as M;
use Carbon_Fields\Pimple\Container as PimpleContainer;
use Carbon_Fields\Container\Fulfillable\Fulfillable_Collection;
use Carbon_Fields\Container\Condition\Factory as ConditionFactory;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* WARNING: the array translator produces logically identical results but not 100% identical representations when dealing with nested collections
*
* @coversDefaultClass Carbon_Fields\Container\Fulfillable\Translator\Array_Translator
*/
class ArrayTranslatorTest extends WP_UnitTestCase {
public $subject;
public function setUp(): void {
$ioc = new PimpleContainer();
/* Conditions */
$ioc['container_condition_factory'] = function( $ioc ) {
return new ConditionFactory( $ioc['container_conditions'] );
};
$ioc['container_condition_fulfillable_collection'] = $ioc->factory( function( $ioc ) {
return new Fulfillable_Collection( $ioc['container_condition_factory'], $ioc['container_condition_translator_array'] );
} );
$ioc['container_conditions'] = function() {
return new PimpleContainer();
};
$cc_ioc = $ioc['container_conditions'];
$cc_ioc['post_id'] = $cc_ioc->factory( function( $cc_ioc ) use ( $ioc ) {
$condition = new \Carbon_Fields\Container\Condition\Post_ID_Condition();
$condition->set_comparers( $ioc['container_condition_comparer_collections']['generic'] );
return $condition;
} );
$cc_ioc['post_type'] = $cc_ioc->factory( function( $cc_ioc ) use ( $ioc ) {
$condition = new \Carbon_Fields\Container\Condition\Post_Type_Condition();
$condition->set_comparers( $ioc['container_condition_comparer_collections']['nonscalar'] );
return $condition;
} );
/* Comparers */
$ioc['container_condition_comparers'] = function( $ioc ) {
return new PimpleContainer();
};
$ioc['container_condition_comparers']['equality'] = function() {
return new \Carbon_Fields\Container\Condition\Comparer\Equality_Comparer();
};
$ioc['container_condition_comparers']['contain'] = function() {
return new \Carbon_Fields\Container\Condition\Comparer\Contain_Comparer();
};
$ioc['container_condition_comparers']['scalar'] = function() {
return new \Carbon_Fields\Container\Condition\Comparer\Scalar_Comparer();
};
$ioc['container_condition_comparers']['custom'] = function() {
return new \Carbon_Fields\Container\Condition\Comparer\Custom_Comparer();
};
$ioc['container_condition_comparer_collections'] = function( $ioc ) {
return new PimpleContainer();
};
$ioc['container_condition_comparer_collections']['generic'] = function( $cccc_ioc ) use ( $ioc ) {
return array(
$ioc['container_condition_comparers']['equality'],
$ioc['container_condition_comparers']['contain'],
$ioc['container_condition_comparers']['scalar'],
$ioc['container_condition_comparers']['custom'],
);
};
$ioc['container_condition_comparer_collections']['nonscalar'] = function( $cccc_ioc ) use ( $ioc ) {
return array(
$ioc['container_condition_comparers']['equality'],
$ioc['container_condition_comparers']['contain'],
$ioc['container_condition_comparers']['custom'],
);
};
/* Translators */
$ioc['container_condition_translator_array'] = function( $ioc ) {
return new \Carbon_Fields\Container\Fulfillable\Translator\Array_Translator( $ioc['container_condition_factory'] );
};
\Carbon_Fields\Carbon_Fields::instance()->install( $ioc );
$this->subject = $ioc['container_condition_translator_array'];
}
public function tearDown(): void {
M::close();
$this->subject = null;
}
/**
* @covers ::fulfillable_to_foreign
*/
public function testFulfillableToForeignWithCondition() {
$factory = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_factory' );
$condition = $factory->make( 'post_type' );
$condition->set_comparison_operator( '!=' );
$condition->set_value( 'post' );
$expected = array(
'type' => 'post_type',
'compare' => '!=',
'value' => 'post',
);
$received = $this->subject->fulfillable_to_foreign( $condition );
$this->assertSame( $expected, $received );
}
/**
* @covers ::foreign_to_fulfillable
*/
public function testForeignToFulfillableWithCondition() {
$factory = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_factory' );
$condition = $factory->make( 'post_type' );
$condition->set_comparison_operator( '!=' );
$condition->set_value( 'post' );
$expected = $condition;
$received = $this->subject->foreign_to_fulfillable( array(
'type' => 'post_type',
'compare' => '!=',
'value' => 'post',
) );
$this->assertEquals( $expected, $received );
}
/**
* @covers ::fulfillable_to_foreign
*/
public function testFulfillableToForeignWithCollection() {
$fulfillable = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' );
$fulfillable->where( 'post_type', '!=', 'post' );
$fulfillable->where( 'post_id', '!=', 1 );
$expected = array(
'relation' => 'AND',
array(
'type' => 'post_type',
'compare' => '!=',
'value' => 'post',
),
array(
'type' => 'post_id',
'compare' => '!=',
'value' => 1,
),
);
$received = $this->subject->fulfillable_to_foreign( $fulfillable );
$this->assertSame( $expected, $received );
}
/**
* @covers ::foreign_to_fulfillable
*/
public function testForeignToFulfillableWithCollection() {
$fulfillable = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' );
$fulfillable->where( 'post_type', '!=', 'post' );
$fulfillable->where( 'post_id', '!=', 1 );
$expected = $fulfillable;
$received = $this->subject->foreign_to_fulfillable( array(
'relation' => 'AND',
array(
'type' => 'post_type',
'compare' => '!=',
'value' => 'post',
),
array(
'type' => 'post_id',
'compare' => '!=',
'value' => 1,
),
) );
$this->assertEquals( $expected, $received );
}
/**
* @covers ::fulfillable_to_foreign
*/
public function testFulfillableToForeignWithNestedCollection() {
$fulfillable = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' );
$fulfillable->where( 'post_type', '!=', 'post' );
$fulfillable->where( function( $c ) {
$c->where( 'post_id', '!=', 1 );
$c->or_where( 'post_id', '!=', 2 );
} );
$expected = array(
'relation' => 'AND',
array(
'type' => 'post_type',
'compare' => '!=',
'value' => 'post',
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'type' => 'post_id',
'compare' => '!=',
'value' => 1,
),
),
array(
'relation' => 'OR',
array(
'type' => 'post_id',
'compare' => '!=',
'value' => 2,
),
),
),
);
$received = $this->subject->fulfillable_to_foreign( $fulfillable );
$this->assertSame( $expected, $received );
}
/**
* @covers ::foreign_to_fulfillable
*/
public function testForeignToFulfillableWithNestedCollection() {
$fulfillable = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' );
$fulfillable->where( 'post_type', '!=', 'post' );
$fulfillable->where( function( $c ) {
$c->or_where( 'post_id', '!=', 1 );
$c->or_where( 'post_id', '!=', 2 );
} );
$expected = $fulfillable;
$received = $this->subject->foreign_to_fulfillable( array(
'relation' => 'AND',
array(
'type' => 'post_type',
'compare' => '!=',
'value' => 'post',
),
array(
'relation' => 'OR',
array(
'type' => 'post_id',
'compare' => '!=',
'value' => 1,
),
array(
'type' => 'post_id',
'compare' => '!=',
'value' => 2,
),
),
) );
$this->assertEquals( $expected, $received );
}
}

View file

@ -0,0 +1,149 @@
<?php
use Mockery as M;
use Carbon_Fields\Container\Repository;
use Carbon_Fields\Container\Container;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* @coversDefaultClass Carbon_Fields\Container\Repository
*/
class RepositoryTest extends WP_UnitTestCase {
public $containerId;
public $containerTitle;
public $containerType;
public $containerClass;
public $containerDuplicateTitleId;
public $repository;
public function setUp(): void {
$this->containerId = 'carbon_fields_container_page_settings';
$this->containerTitle = 'Page Settings';
$this->containerType = 'post_meta';
$this->containerClass = 'Carbon_Fields\Container\Post_Meta_Container';
$this->containerDuplicateTitleId = 'carbon_fields_container_page_settings1';
$this->repository = new Repository();
}
public function tearDown(): void {
M::close();
$this->repository = null;
}
public function getContainerMock( $callable = null ) {
if ( is_callable( $callable ) ) {
$mock = M::mock( $this->containerClass, $callable );
} else {
$mock = M::mock( $this->containerClass );
}
$mock->shouldIgnoreMissing();
$mock->id = $this->containerId;
$mock->title = $this->containerTitle;
$mock->type = $this->containerType;
return $mock;
}
/**
* @covers ::register_container
* @covers ::initialize_containers
*/
public function testInitializeContainersCallsInitOnce() {
$container = $this->getContainerMock( function( $mock ) {
$mock->shouldReceive( 'init' )->once();
} );
$this->repository->register_container( $container );
$this->repository->initialize_containers();
$this->assertTrue( true ); // rely on Mockery expectations to fail the test
}
/**
* @covers ::register_container
* @covers ::initialize_containers
*/
public function testInitializeContainersReturnsContainers() {
$container = $this->getContainerMock();
$expected = array( $container );
$this->repository->register_container( $container );
$received = $this->repository->initialize_containers();
$this->assertEquals( $expected, $received );
}
/**
* @covers ::register_container
* @covers ::initialize_containers
*/
public function testInitializeContainersReturnsOnlyNewContainers() {
$container1 = $this->getContainerMock();
$container2 = $this->getContainerMock();
$expected1 = array( $container1 );
$expected2 = array( $container2 );
$this->repository->register_container( $container1 );
$received1 = $this->repository->initialize_containers();
$this->assertEquals( $expected1, $received1 );
$this->repository->register_container( $container2 );
$received2 = $this->repository->initialize_containers();
$this->assertEquals( $expected2, $received2 );
}
/**
* @covers ::get_active_containers
*/
public function testGetActiveContainers() {
$container1 = $this->getContainerMock( function( $mock ) {
$mock->shouldReceive( 'is_active' )->andReturn( true );
} );
$container2 = $this->getContainerMock( function( $mock ) {
$mock->shouldReceive( 'is_active' )->andReturn( false );
} );
$expected = array( $container1 );
$this->repository->register_container( $container1 );
$this->repository->register_container( $container2 );
$received = $this->repository->get_active_containers();
$this->assertEquals( $expected, $received );
}
/**
* @covers ::get_unique_container_id
*/
public function testGetUniqueContainerId_InvalidCharacters_Stripped() {
$expected = $this->containerId;
$received = $this->repository->get_unique_container_id( $this->containerTitle );
$this->assertEquals( $expected, $received );
}
/**
* @covers ::get_unique_container_id
* @covers ::register_container
*/
public function testGetUniqueContainerId_IdenticalTitles_ReturnsDifferentIds() {
$container = $this->getContainerMock();
$this->repository->register_container( $container );
$received = $this->repository->get_unique_container_id( $container->title );
$this->assertNotEquals( $container->get_id(), $received );
}
/**
* @covers ::get_unique_container_id
* @covers ::register_container
*/
public function testGetUniqueContainerId_IdenticalTitles_AddsNumericalSuffix() {
$container = $this->getContainerMock();
$container->shouldReceive( 'get_id' )->andReturn( $this->containerId );
$this->repository->register_container( $container );
$received = $this->repository->get_unique_container_id( $container->title );
$this->assertEquals( $this->containerDuplicateTitleId, $received );
}
}