Factories and Test adaptations

Factories field and filter were created.
Tests from collection, filters and item metadata
were adapted it.
This commit is contained in:
weryques 2017-12-04 12:58:19 -02:00
parent 0083f35f1d
commit ed0c3d9674
8 changed files with 330 additions and 226 deletions

View File

@ -29,5 +29,7 @@ tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
require $_tests_dir . '/includes/bootstrap.php';
require_once(__DIR__ . '/factories/class-tainacan-entity-factory.php');
require_once(__DIR__ . '/factories/class-tainacan-field-factory.php');
require_once(__DIR__ . '/factories/class-tainacan-filter-factory.php');
require_once(__DIR__ . '/tainacan-unit-test-case.php');

View File

@ -2,7 +2,6 @@
namespace Tainacan\Tests\Factories;
class Entity_Factory {
private $entity;
@ -13,60 +12,90 @@ class Entity_Factory {
/**
* Receive a type of the collection as string, an associative array,
* a boolean that say if the values need to be validated and inserted in database, and then
* create the entity type ordered.
* create the entity type ordered and return it.
*
* @param $type
* @param array $args
* @param bool $is_validated_and_in_db
*
* @return mixed
* @throws \ErrorException
*/
public function create_entity($type, $args = [], $is_validated_and_in_db = false){
try {
$this->entity_type = "\Tainacan\Entities\\$type";
$this->repository_type = "\Tainacan\Repositories\\$type".'s';
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {
$type = ucwords(strtolower($type), '_');
}
$this->entity_type = "\Tainacan\Entities\\$type";
$type_size = strlen($type);
if($type[$type_size-1] == 'y'){
$type[$type_size-1] = 'ies';
} else {
$this->repository_type = "\Tainacan\Repositories\\$type".'s';
}
$this->entity = new $this->entity_type();
$this->repository = new $this->repository_type();
if ( ! empty( $args ) && $is_validated_and_in_db ) {
foreach ( $args as $attribute => $content ) {
$this->entity->set_mapped_property( $attribute, $content );
if (!empty($args) && $is_validated_and_in_db) {
foreach ($args as $attribute => $content) {
$set_ = 'set_'.$attribute;
$this->entity->$set_($content);
}
if ( $this->entity->validate() ) {
$this->entity = $this->repository->insert( $this->entity );
if ($this->entity->validate()) {
$this->entity = $this->repository->insert($this->entity);
} else {
throw new Exception( __( 'The entity wasn\'t validated.' ) );
throw new \ErrorException( __( 'The entity wasn\'t validated.' ) );
}
} elseif ( empty( $args ) && ! $is_validated_and_in_db ) {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} elseif ( empty( $args ) && $is_validated_and_in_db ) {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
if ( $this->entity->validate() ) {
} elseif (!empty($args) && !$is_validated_and_in_db){
foreach ($args as $attribute => $content) {
$set_ = 'set_'.$attribute;
$this->entity->$set_($content);
}
} elseif (empty($args) && !$is_validated_and_in_db) {
try {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Error $exception){
$this->entity->set_title( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
} elseif (empty($args) && $is_validated_and_in_db) {
try {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Error $exception){
$this->entity->set_title( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
$this->entity->validate();
if ($this->entity->validate()) {
$this->entity = $this->repository->insert( $this->entity );
} else {
throw new Exception( __( 'The entity wasn\'t validated.' ) );
throw new \ErrorException( __( 'The entity wasn\'t validated.' ) );
}
} else {
throw new InvalidArgumentException( __( 'One or more arguments are invalid.', 'tainacan' ) );
throw new \InvalidArgumentException( __( 'One or more arguments are invalid.', 'tainacan' ) );
}
} catch (Error $exception){
} catch (\Error $exception){
echo($exception->getMessage());
echo($exception->getTraceAsString());
}
}
/**
* Return the entity type ordered
*
* @return mixed
*/
public function get_entity(){
return $this->entity;
}
}
?>

View File

@ -0,0 +1,25 @@
<?php
namespace Tainacan\Tests\Factories;
class Field_Factory {
private $field;
protected $field_type;
public function create_field($type, $primitive_type = []){
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {
$type = ucwords(strtolower($type), '_');
}
$this->field_type = "\Tainacan\Field_Types\\$type";
$this->field = new $this->field_type(/* Here goes the primitive type */);
return $this->field;
}
}
?>

View File

@ -0,0 +1,25 @@
<?php
namespace Tainacan\Tests\Factories;
class Filter_Factory {
private $filter;
protected $filter_type;
public function create_filter($type, $supported_types = []){
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {
$type = ucwords(strtolower($type), '_');
}
$this->filter_type = "\Tainacan\Filter_Types\\$type";
$this->filter = new $this->filter_type(/* Here goes the supported types */);
return $this->filter;
}
}
?>

View File

@ -6,8 +6,12 @@ use Tainacan\Tests\Factories;
class TAINACAN_UnitTestCase extends \WP_UnitTestCase {
protected $tainacan_entity_factory;
protected $tainacan_field_factory;
protected $tainacan_filter_factory;
public function setUp(){
$this->tainacan_entity_factory = new Factories\Entity_Factory();
$this->tainacan_field_factory = new Factories\Field_Factory();
$this->tainacan_filter_factory = new Factories\Filter_Factory();
}
}

View File

@ -17,8 +17,8 @@ class Collections extends TAINACAN_UnitTestCase {
* A single example test.
*/
function test_add() {
$this->tainacan_entity_factory->create_entity(
'Collection',
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
@ -27,8 +27,6 @@ class Collections extends TAINACAN_UnitTestCase {
true
);
$x = $this->tainacan_entity_factory->get_entity();;
global $Tainacan_Collections;
$this->assertEquals('Tainacan\Entities\Collection', get_class($x));
@ -42,47 +40,48 @@ class Collections extends TAINACAN_UnitTestCase {
}
function test_item() {
$x = new \Tainacan\Entities\Collection();
$x->set_name('teste');
$x->set_description('adasdasdsa');
$x->set_default_order('DESC');
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 'DESC'
),
true
);
global $Tainacan_Collections;
$x->validate();
$col = $Tainacan_Collections->insert($x);
$collection = $Tainacan_Collections->fetch($x->get_id());
$collection = $Tainacan_Collections->fetch($col->get_id());
$i = new \Tainacan\Entities\Item();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item test',
'description' => 'adasdasdsa',
'order' => 'DESC',
'collection' => $collection
),
true
);
global $Tainacan_Items;
$i->validate();
$item = $Tainacan_Items->insert( $i );
$item = $Tainacan_Items->fetch( $i->get_id() );
$item = $Tainacan_Items->fetch( $item->get_id() );
$this->assertEquals($item->get_title(), 'item teste');
$this->assertEquals($item->get_title(), 'item test');
$this->assertEquals($item->get_description(), 'adasdasdsa');
$this->assertEquals($item->get_collection_id(), $collection->get_id());
}
function test_validation() {
$x = new \Tainacan\Entities\Collection();
$x->set_name('teste');
$x->set_description('adasdasdsa');
$x->set_default_order(13);
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'default_order' => 13
)
);
$this->assertFalse($x->validate());
$this->assertTrue(sizeof($x->get_errors()) > 0);

View File

@ -11,70 +11,76 @@ namespace Tainacan\Tests;
/**
* Sample test case.
*/
class Filters extends \WP_UnitTestCase {
class Filters extends TAINACAN_UnitTestCase {
function teste_add(){
global $Tainacan_Collections, $Tainacan_Filters;
global $Tainacan_Filters;
$collection = new \Tainacan\Entities\Collection();
$filter = new \Tainacan\Entities\Filter();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'filtro',
'collection' => $collection
),
true
);
//setando os valores na classe do metadado
$filter->set_name('filtro');
$filter->set_collection( $collection );
//inserindo o metadado
$filter->validate();
$filter = $Tainacan_Filters->insert( $filter );
$test = $Tainacan_Filters->fetch( $filter->get_id() );
$test = $Tainacan_Filters->fetch($filter->get_id());
$this->assertEquals('filtro', $test->get_name());
$this->assertEquals($collection->get_id(), $test->get_collection_id());
}
function test_add_with_metadata_and_type(){
global $Tainacan_Collections, $Tainacan_Filters,$Tainacan_Metadatas;
global $Tainacan_Filters;
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$filter = new \Tainacan\Entities\Filter();
$type = new \Tainacan\Field_Types\Text();
$filter_list_type = new \Tainacan\Filter_Types\List_Filter();
$filter_range_type = new \Tainacan\Filter_Types\Range();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
$type = $this->tainacan_field_factory->create_field('text');
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_collection_id( $collection->get_id() );
$metadata->set_field_type_object( $type );
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'collection_id' => $collection->get_id(),
'field_type_object' => $type
),
true
);
$filter_list_type = $this->tainacan_filter_factory->create_filter('list_filter');
//inserindo o metadado
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'filtro',
'collection' => $collection,
'metadata' => $metadata,
'filter_type_object' => $filter_list_type
),
true
);
//inserindo o filtro
$filter->set_name('filtro');
$filter->set_collection( $collection );
$filter->set_metadata( $metadata );
$filter_range_type = $this->tainacan_filter_factory->create_filter('range');
//nao devera permitir um filtro Range para o tipo string
$this->assertTrue( $filter->set_filter_type_object( $filter_range_type ) === null );
$filter->set_filter_type_object( $filter_list_type );
$filter->validate();
$filter = $Tainacan_Filters->insert( $filter );
$test = $Tainacan_Filters->fetch( $filter->get_id() );
$this->assertEquals( 'filtro', $test->get_name() );

View File

@ -11,99 +11,104 @@ namespace Tainacan\Tests;
/**
* Sample test case.
*/
class Item_Metadata extends \WP_UnitTestCase {
class Item_Metadata extends TAINACAN_UnitTestCase {
/**
* Teste da insercao de um metadado simples sem o tipo
*/
function test_add() {
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
global $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
$type = $this->tainacan_field_factory->create_field('text');
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'field_type_object' => $type
),
true
);
$test = $Tainacan_Metadatas->fetch($metadata->get_id());
$i = new \Tainacan\Entities\Item();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item teste',
'description' => 'adasdasdsa',
'collection' => $collection
),
true
);
global $Tainacan_Items;
$i->validate();
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->fetch($item->get_id());
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
$item = $Tainacan_Items->fetch($i->get_id());
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
$item_metadata->set_value('teste_value');
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$this->assertEquals('teste_value', $item_metadata->get_value());
}
/**
* Teste da insercao de um metadado simples com o tipo
*/
function teste_required(){
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
global $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
$type = $this->tainacan_field_factory->create_field('text');
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_required( 'yes' );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'required' => 'yes',
'field_type_object' => $type
),
true
);
$test = $Tainacan_Metadatas->fetch($metadata->get_id());
$i = new \Tainacan\Entities\Item();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item teste',
'description' => 'adasdasdsa',
'collection' => $collection
),
true
);
global $Tainacan_Items;
$i->validate();
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->fetch($item->get_id());
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
$item = $Tainacan_Items->fetch($i->get_id());
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
// false because its required
$this->assertFalse($item_metadata->validate());
@ -118,96 +123,105 @@ class Item_Metadata extends \WP_UnitTestCase {
}
function teste_collection_key(){
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
global $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
$type = $this->tainacan_field_factory->create_field('text');
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_collection_key( 'yes' );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'collection_key' => 'yes',
'field_type_object' => $type
),
true
);
$test = $Tainacan_Metadatas->fetch($metadata->get_id());
$i = new \Tainacan\Entities\Item();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item teste',
'description' => 'adasdasdsa',
'collection' => $collection
),
true
);
global $Tainacan_Items;
$i->validate();
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->fetch($item->get_id());
$item = $Tainacan_Items->fetch($i->get_id());
$value = 'teste_val';
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
$item_metadata->set_value($value);
$this->assertTrue($item_metadata->validate());
$item_metadata->validate();
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$n_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
$n_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
$n_item_metadata->set_value($value);
$this->assertFalse($n_item_metadata->validate());
}
function teste_fetch(){
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
global $Tainacan_Item_Metadata;
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_status('publish');
$metadata->set_field_type_object( $type );
$type = $this->tainacan_field_factory->create_field('text');
//inserindo o metadado
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'status' => 'publish',
'field_type_object' => $type
),
true
);
//$test = $Tainacan_Metadatas->fetch($metadata->get_id());
$i = new \Tainacan\Entities\Item();
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item teste',
'description' => 'adasdasdsa',
'collection' => $collection
),
true
);
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
$item_metadatas = $Tainacan_Item_Metadata->fetch($i, 'OBJECT');
global $Tainacan_Items;
$i->validate();
$item = $Tainacan_Items->insert($i);
$ItemMetadatas = $Tainacan_Item_Metadata->fetch($item, 'OBJECT');
$this->assertTrue(is_array($ItemMetadatas));
$this->assertEquals(1, sizeof($ItemMetadatas));
$this->assertEquals('metadado', $ItemMetadatas[0]->get_metadata()->get_name());
$this->assertTrue(is_array($item_metadatas));
$this->assertEquals(1, sizeof($item_metadatas));
$this->assertEquals('metadado', $item_metadatas[0]->get_metadata()->get_name());
}
}