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,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 );
}
}