Now factories are used in all tests

This commit is contained in:
weryques 2017-12-04 16:20:49 -02:00
parent 0b00392f7a
commit 3b0d738fc2
6 changed files with 289 additions and 231 deletions

View File

@ -34,8 +34,10 @@ class Entity_Factory {
$this->entity_type = "\Tainacan\Entities\\$type";
$type_size = strlen($type);
if($type[$type_size-1] == 'y'){
$type[$type_size-1] = 'ies';
$type[$type_size-1] = 'i';
$this->repository_type = "\Tainacan\Repositories\\$type".'es';
} else {
$this->repository_type = "\Tainacan\Repositories\\$type".'s';
}
@ -45,8 +47,14 @@ class Entity_Factory {
if (!empty($args) && $is_validated_and_in_db) {
foreach ($args as $attribute => $content) {
$set_ = 'set_'.$attribute;
$this->entity->$set_($content);
if($attribute == 'add_metadata'){
foreach ($content as $in){
$this->entity->$attribute($in[0], $in[1]);
}
} else {
$set_ = 'set_' . $attribute;
$this->entity->$set_( $content );
}
}
if ($this->entity->validate()) {
@ -57,15 +65,21 @@ class Entity_Factory {
} elseif (!empty($args) && !$is_validated_and_in_db){
foreach ($args as $attribute => $content) {
$set_ = 'set_'.$attribute;
$this->entity->$set_($content);
if($attribute == 'add_metadata'){
foreach ($content as $in){
$this->entity->$attribute($in[0], $in[1]);
}
} else {
$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){
} catch (\Exception $exception){
$this->entity->set_title( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
@ -74,7 +88,7 @@ class Entity_Factory {
try {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Error $exception){
} catch (\Exception $exception){
$this->entity->set_title( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
@ -89,7 +103,7 @@ class Entity_Factory {
} else {
throw new \InvalidArgumentException( __( 'One or more arguments are invalid.', 'tainacan' ) );
}
} catch (\Error $exception){
} catch (\Exception $exception){
echo($exception->getMessage());
echo($exception->getTraceAsString());
}

View File

@ -193,7 +193,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
$type = $this->tainacan_field_factory->create_field('text');
$metadata = $this->tainacan_entity_factory->create_entity(
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',

View File

@ -8,97 +8,129 @@ namespace Tainacan\Tests;
* @package Test_Tainacan
*/
use Tainacan\Entities\Entity;
use Tainacan\Entities;
/**
* Sample test case.
*/
class Items extends \WP_UnitTestCase {
class Items extends TAINACAN_UnitTestCase {
function teste_query(){
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$type = $this->tainacan_field_factory->create_field('text');
$type = new \Tainacan\Field_Types\Text();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'status' => 'publish'
),
true
);
$collection = new \Tainacan\Entities\Collection();
$collection2 = new \Tainacan\Entities\Collection();
$collection->set_name('teste');
$collection->set_status('publish');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
$collection2->set_name('teste2');
$collection2->set_status('publish');
$collection2->validate();
$collection2 = $Tainacan_Collections->insert($collection2);
$metadata = new \Tainacan\Entities\Metadata();
$metadata2 = new \Tainacan\Entities\Metadata();
$metadata3 = new \Tainacan\Entities\Metadata();
$metadata->set_name('metadado');
$metadata->set_collection( $collection );
$metadata->set_status('publish');
$metadata->set_field_type_object( $type );
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata2->set_name('metadado2');
$metadata2->set_collection( $collection2 );
$metadata2->set_status('publish');
$metadata2->set_field_type_object( $type );
$metadata2->validate();
$metadata2 = $Tainacan_Metadatas->insert($metadata2);
$metadata3->set_name('metadado3');
$metadata3->set_collection( $collection2 );
$metadata3->set_status('publish');
$metadata3->set_field_type_object( $type );
$metadata3->validate();
$metadata3 = $Tainacan_Metadatas->insert($metadata3);
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste2',
'status' => 'publish'
),
true
);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'field_type_object' => $type
),
true
);
$metadata2 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado2',
'status' => 'publish',
'collection' => $collection,
'field_type_object' => $type
),
true
);
$metadata3 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado3',
'status' => 'publish',
'collection' => $collection,
'field_type_object' => $type
),
true
);
global $Tainacan_Items;
$i = new \Tainacan\Entities\Item();
$i->set_title('orange');
$i->set_collection($collection);
$i->add_metadata($metadata, 'value_1');
$i->validate();
/**
* @var \Tainacan\Entities\Item $item
*/
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->fetch($item->get_id());
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'orange',
'collection' => $collection,
'add_metadata' => [
[$metadata, 'value_1']
]
),
true
);
$item = $Tainacan_Items->fetch($i->get_id());
$meta_test = $item->get_metadata();
$this->assertTrue( isset($meta_test[$metadata->get_id()]) );
$this->assertTrue( $meta_test[$metadata->get_id()] instanceof \Tainacan\Entities\Item_Metadata_Entity );
$this->assertTrue( $meta_test[$metadata->get_id()] instanceof Entities\Item_Metadata_Entity );
$this->assertEquals( 'value_1', $meta_test[$metadata->get_id()]->get_value());
$i = new \Tainacan\Entities\Item();
$i->set_title('apple');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_2');
$i->add_metadata($metadata3, 'value_2');
$i->validate();
$item = $Tainacan_Items->insert($i);
$i = new \Tainacan\Entities\Item();
$i->set_title('lemon');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_2');
$i->add_metadata($metadata2, 'value_3'); // if we set twice, value is overridden
$i->add_metadata($metadata3, 'value_3');
$i->validate();
$item = $Tainacan_Items->insert($i);
$i = new \Tainacan\Entities\Item();
$i->set_title('pinapple');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_3');
$i->add_metadata($metadata3, 'value_6');
$i->validate();
$item = $Tainacan_Items->insert($i);
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'apple',
'collection' => $collection2,
'add_metadata' => [
[$metadata2, 'value_2'],
[$metadata3, 'value_2']
]
),
true
);
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'lemon',
'collection' => $collection2,
'add_metadata' => [
[$metadata2, 'value_2'],
[$metadata2, 'value_3'],
[$metadata3, 'value_3']
]
),
true
);
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'pineapple',
'collection' => $collection2,
'add_metadata' => [
[$metadata2, 'value_3'],
[$metadata3, 'value_6']
]
),
true
);
// should return all 4 items
$test_query = $Tainacan_Items->fetch([]);
$this->assertEquals(4, $test_query->post_count );
@ -116,15 +148,15 @@ class Items extends \WP_UnitTestCase {
$this->assertEquals(1,$test_query->post_count);
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals('orange', $item->get_title() );
$item1 = new Entities\Item( get_the_ID() );
$this->assertEquals('orange', $item1->get_title() );
$test_query = $Tainacan_Items->fetch(['title' => 'orange']);
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$item2 = new Entities\Item( get_the_ID() );
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals('orange', $item->get_title());
$this->assertEquals('orange', $item2->get_title());
// should return the other 3 items
$test_query = $Tainacan_Items->fetch([], $collection2);
@ -132,16 +164,10 @@ class Items extends \WP_UnitTestCase {
$test_query = $Tainacan_Items->fetch(['title' => 'apple']);
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$item3 = new Entities\Item( get_the_ID() );
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals('apple', $item->get_title());
$apple_meta = $item->get_metadata();
$this->assertEquals(2, sizeof( $apple_meta ));
$apple_meta_values = [];
foreach ($apple_meta as $am) {
$this->assertEquals('value_2', $am->get_value());
}
$this->assertEquals('apple', $item3->get_title());
// should return 1 item
$test_query = $Tainacan_Items->fetch([

View File

@ -2,7 +2,6 @@
namespace Tainacan\Tests;
use Tainacan\Entities\Log;
use Tainacan\Entities\Collection;
/**
* Class TestCollections
@ -13,7 +12,7 @@ use Tainacan\Entities\Collection;
/**
* Sample test case.
*/
class Logs extends \WP_UnitTestCase {
class Logs extends TAINACAN_UnitTestCase {
/**
@ -22,19 +21,18 @@ class Logs extends \WP_UnitTestCase {
function test_add() {
global $Tainacan_Logs;
$log = new Log();
//setando os valores na classe do tainacan
$log->set_title('blame someone');
$log->set_description('someone did that');
$log = $this->tainacan_entity_factory->create_entity(
'log',
array(
'title' => 'blame someone',
'description' => 'someone did that'
),
true
);
$user_id = get_current_user_id();
$blog_id = get_current_blog_id();
//inserindo
$log->validate();
$log = $Tainacan_Logs->insert($log);
//retorna a taxonomia
$test = $Tainacan_Logs->fetch($log->get_id());
@ -43,14 +41,17 @@ class Logs extends \WP_UnitTestCase {
$this->assertEquals( $user_id, $test->get_user_id() );
$this->assertEquals( $blog_id, $test->get_blog_id() );
$value = new Collection();
$value->set_name('testeLogs');
$value->set_description('adasdasdsa123');
$value->set_default_order('DESC');
$value = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeLogs',
'description' => 'adasdasdsa123',
'default_order' => 'DESC'
),
true
);
global $Tainacan_Collections;
$value->validate();
$value = $Tainacan_Collections->insert($value);
$value->set_name('new_testeLogs');
$value->validate();

View File

@ -11,63 +11,68 @@ namespace Tainacan\Tests;
/**
* Metadata test case.
*/
class Metadata extends \WP_UnitTestCase {
class Metadata extends TAINACAN_UnitTestCase {
/**
* Test insert a regular metadata with type
*/
function test_add() {
global $Tainacan_Collections, $Tainacan_Metadatas;
global $Tainacan_Metadatas;
$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());
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao');
$this->assertEquals($test->get_collection_id(), $collection->get_id());
}
/**
* Test insert a regular metadata with type
*/
function test_add_type(){
global $Tainacan_Collections, $Tainacan_Metadatas;
global $Tainacan_Metadatas;
$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_collection_id( $collection->get_id() );
$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_id' => $collection->get_id(),
'field_type_object' => $type
),
true
);
$test = $Tainacan_Metadatas->fetch($metadata->get_id());
@ -79,73 +84,84 @@ class Metadata extends \WP_UnitTestCase {
function test_hierarchy_metadata(){
global $Tainacan_Collections, $Tainacan_Metadatas;
global $Tainacan_Metadatas;
$metadata_default = new \Tainacan\Entities\Metadata();
$collection_grandfather = new \Tainacan\Entities\Collection();
$metadata_grandfather = new \Tainacan\Entities\Metadata();
$collection_father = new \Tainacan\Entities\Collection();
$metadata_father = new \Tainacan\Entities\Metadata();
$collection_son = new \Tainacan\Entities\Collection();
$metadata_son = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$type = $this->tainacan_field_factory->create_field('text');
//creating metadata default
$metadata_default->set_name('metadata default');
$metadata_default->set_collection_id( $Tainacan_Metadatas->get_default_metadata_attribute() );
$metadata_default->set_field_type_object( $type );
$metadata_default->set_status('publish');
$metadata_default->validate();
$Tainacan_Metadatas->insert($metadata_default);
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata default',
'collection_id' => $Tainacan_Metadatas->get_default_metadata_attribute(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
//creating collection grandfather
$collection_grandfather->set_name('collection grandfather');
$collection_grandfather->validate();
$collection_grandfather = $Tainacan_Collections->insert($collection_grandfather);
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection grandfather'
),
true
);
//creating metadata grandfather
$metadata_grandfather->set_name('metadata grandfather');
$metadata_grandfather->set_collection_id( $collection_grandfather->get_id() );
$metadata_grandfather->set_field_type_object( $type );
$metadata_grandfather->set_status('publish');
$metadata_grandfather->validate();
$Tainacan_Metadatas->insert($metadata_grandfather);
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata grandfather',
'collection_id' => $collection_grandfather->get_id(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
//creating collection father
$collection_father->set_name('collection father');
$collection_father->set_parent( $collection_grandfather->get_id() );
$collection_father->validate();
$collection_father = $Tainacan_Collections->insert( $collection_father );
$collection_father = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection father',
'parent' => $collection_grandfather->get_id()
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata father',
'collection_id' => $collection_father->get_id(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
$collection_son = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection son',
'parent' => $collection_father->get_id()
),
true
);
$this->assertEquals( $collection_grandfather->get_id(), $collection_father->get_parent() );
//creating metadata father
$metadata_father->set_name('metadata father');
$metadata_father->set_collection_id( $collection_father->get_id() );
$metadata_father->set_field_type_object( $type );
$metadata_father->set_status('publish');
$metadata_father->validate();
$Tainacan_Metadatas->insert($metadata_father);
//creating collection son
$collection_son->set_name('collection son');
$collection_son->set_parent( $collection_father->get_id() );
$collection_son->validate();
$collection_son = $Tainacan_Collections->insert($collection_son);
$this->assertEquals( $collection_father->get_id(), $collection_son->get_parent() );
//creating metadata son
$metadata_son->set_name('metadata son');
$metadata_son->set_collection_id( $collection_son->get_id() );
$metadata_son->set_field_type_object( $type );
$metadata_son->set_status('publish');
$metadata_son->validate();
$Tainacan_Metadatas->insert($metadata_son);
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata son',
'collection_id' => $collection_son->get_id(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
$retrieve_metadata = $Tainacan_Metadatas->fetch_by_collection( $collection_son, [], 'OBJECT' );
$this->assertEquals( 4, sizeof( $retrieve_metadata ) );
}
}

View File

@ -11,7 +11,7 @@ namespace Tainacan\Tests;
/**
* Sample test case.
*/
class Taxonomies extends \WP_UnitTestCase {
class Taxonomies extends TAINACAN_UnitTestCase {
/**
@ -20,17 +20,15 @@ class Taxonomies extends \WP_UnitTestCase {
function test_add() {
global $Tainacan_Taxonomies;
$taxonomy = new \Tainacan\Entities\Taxonomy();
//setando os valores na classe do tainacan
$taxonomy->set_name('genero');
$taxonomy->set_description('tipos de musica');
$taxonomy->set_allow_insert('yes');
//inserindo
$taxonomy->validate();
$taxonomy = $Tainacan_Taxonomies->insert($taxonomy);
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'genero',
'description' => 'tipos de musica',
'allow_insert' => 'yes'
),
true
);
//retorna a taxonomia
$test = $Tainacan_Taxonomies->fetch($taxonomy->get_id());
@ -43,27 +41,30 @@ class Taxonomies extends \WP_UnitTestCase {
function test_add_term_taxonomy(){
global $Tainacan_Taxonomies, $Tainacan_Terms;
$taxonomy = new \Tainacan\Entities\Taxonomy();
$term = new \Tainacan\Entities\Term();
//setando os valores na classe de taxonomia
$taxonomy->set_name('genero');
//insere a taxonomia
$taxonomy->validate();
$taxonomy = $Tainacan_Taxonomies->insert($taxonomy);
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'genero',
),
true
);
//retorna a taxonomia
$taxonomy_test = $Tainacan_Taxonomies->fetch($taxonomy->get_id());
//insere um termo na taxonomia
$term->set_taxonomy( $taxonomy_test->get_db_identifier() );
$term->set_name('Rock');
$term->set_user(56);
$term_id = $Tainacan_Terms->insert( $term ) ;
$term = $this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy_test->get_db_identifier(),
'name' => 'Rock',
'user' => 56
),
true
);
//retorna um objeto da classe Tainacan_Term
$test = $Tainacan_Terms->fetch($term_id, $taxonomy_test);
$test = $Tainacan_Terms->fetch($term, $taxonomy_test);
$this->assertEquals( $test->get_name(), 'Rock' );
$this->assertEquals( $test->get_user(), 56 );
}