Tainacan Entity Factory and other things
Added __toString in all entities; Created Entity Factory; Entity factory added in test add collection;
This commit is contained in:
parent
764db51e97
commit
a17e647d52
|
@ -1,14 +1,7 @@
|
|||
<?php
|
||||
|
||||
const ENDPOINTS_DIR = __DIR__ . '/endpoints/';
|
||||
|
||||
require_once(ENDPOINTS_DIR . 'class-tainacan-rest-collections-controller.php');
|
||||
$rest_collections_controller = new TAINACAN_REST_Collections_Controller();
|
||||
|
||||
require_once(ENDPOINTS_DIR . 'class-tainacan-rest-items-controller.php');
|
||||
$rest_items_controller = new TAINACAN_REST_Items_Controller();
|
||||
|
||||
|
||||
$rest_items_controller = new TAINACAN_REST_Items_Controller();
|
||||
// Add here other endpoints imports
|
||||
|
||||
?>
|
|
@ -206,7 +206,7 @@ class Collection extends Entity {
|
|||
*
|
||||
* @see \Tainacan\Repositories\Metadatas->fetch()
|
||||
*
|
||||
* @return \Tainacan\Repositories\Array
|
||||
* @return Array
|
||||
*/
|
||||
function get_metadata() {
|
||||
$Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas();
|
||||
|
|
|
@ -203,17 +203,12 @@ class Entity {
|
|||
global ${$this->repository};
|
||||
$map = ${$this->repository}->get_map();
|
||||
|
||||
$attributes_names = [];
|
||||
$attributes_values = [];
|
||||
|
||||
foreach ($map as $prop => $content){
|
||||
array_push($attributes_names, $prop);
|
||||
array_push($attributes_values, $this->get_mapped_property($prop));
|
||||
$attributes = [];
|
||||
foreach($map as $prop => $content) {
|
||||
$attributes[$prop] = $this->get_mapped_property($prop);
|
||||
}
|
||||
|
||||
$entity_as_json = array_combine($attributes_names, $attributes_values);
|
||||
|
||||
return json_encode($entity_as_json, JSON_NUMERIC_CHECK, JSON_UNESCAPED_UNICODE);
|
||||
return json_encode($attributes, JSON_NUMERIC_CHECK, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
}
|
|
@ -31,7 +31,11 @@ class Filter extends Entity {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Filter Entity';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna o ID do filtro
|
||||
*
|
||||
* @return integer
|
||||
|
|
|
@ -16,6 +16,10 @@ class Item_Metadata_Entity extends Entity {
|
|||
$this->set_item($item);
|
||||
$this->set_metadata($metadata);
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Item Metadata Entity';
|
||||
}
|
||||
|
||||
/**
|
||||
* Atribui o item
|
||||
|
|
|
@ -28,6 +28,10 @@ class Item extends Entity {
|
|||
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Item Entity';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna o ID do item
|
||||
*
|
||||
|
|
|
@ -32,6 +32,10 @@ class Log extends Entity {
|
|||
}
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Log Entity';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna ID do Log
|
||||
*
|
||||
|
|
|
@ -32,6 +32,10 @@ class Metadata extends Entity {
|
|||
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Metadata Entity';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna o ID do metadado
|
||||
*
|
||||
|
|
|
@ -26,6 +26,10 @@ class Taxonomy extends Entity {
|
|||
$this->WP_Post = new \StdClass();
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Taxonomy Entity';
|
||||
}
|
||||
|
||||
function register_taxonomy() {
|
||||
$labels = array(
|
||||
|
|
|
@ -25,6 +25,10 @@ class Term extends Entity {
|
|||
}
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, I\'m the Term Entity';
|
||||
}
|
||||
|
||||
// Getters
|
||||
function get_id() {
|
||||
return $this->get_mapped_property('term_id');
|
||||
|
|
|
@ -6,6 +6,7 @@ const FILTER_TYPES_DIR = __DIR__ . '/filter-types/';
|
|||
const REPOSITORIES_DIR = __DIR__ . '/repositories/';
|
||||
const TRAITS_DIR = __DIR__ . '/traits/';
|
||||
const VENDOR_DIR = __DIR__ . '/../vendor/';
|
||||
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
|
||||
|
||||
const DIRS = [
|
||||
CLASSES_DIR,
|
||||
|
@ -14,6 +15,7 @@ const DIRS = [
|
|||
FILTER_TYPES_DIR,
|
||||
REPOSITORIES_DIR,
|
||||
TRAITS_DIR,
|
||||
ENDPOINTS_DIR,
|
||||
];
|
||||
|
||||
require_once(VENDOR_DIR . 'autoload.php');
|
||||
|
|
|
@ -14,7 +14,7 @@ const API_DIR = __DIR__ . '/api/';
|
|||
const CLASSES_DIR = __DIR__ . '/classes/';
|
||||
|
||||
require_once(CLASSES_DIR . 'tainacan-creator.php');
|
||||
require_once(API_DIR . 'tainacan-rest-creator.php');
|
||||
require_once(API_DIR . 'tainacan-rest-creator.php');
|
||||
|
||||
require_once('dev-interface/class-tainacan-dev-interface.php');
|
||||
$Tainacan_Dev_interface = new \Tainacan\DevInterface\DevInterface();
|
||||
|
|
|
@ -11,8 +11,9 @@ if ( ! $_tests_dir ) {
|
|||
$_tests_dir = $bootstrap_cfg['tests_dir'];
|
||||
}
|
||||
|
||||
if (isset($bootstrap_cfg['tests_url']))
|
||||
define('TAINACAN_TESTS_URL', $bootstrap_cfg['tests_url']);
|
||||
if (isset($bootstrap_cfg['tests_url'])) {
|
||||
define( 'TAINACAN_TESTS_URL', $bootstrap_cfg['tests_url'] );
|
||||
}
|
||||
|
||||
// Give access to tests_add_filter() function.
|
||||
require_once $_tests_dir . '/includes/functions.php';
|
||||
|
@ -23,5 +24,10 @@ function _manually_load_plugin() {
|
|||
require dirname( dirname( __FILE__ ) ) . '/src/tainacan.php';
|
||||
}
|
||||
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
|
||||
|
||||
// Start up the WP testing environment.
|
||||
require $_tests_dir . '/includes/bootstrap.php';
|
||||
|
||||
require_once(__DIR__ . '/factories/class-tainacan-entity-factory.php');
|
||||
require_once(__DIR__ . '/tainacan-unit-test-case.php');
|
||||
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests\Factories;
|
||||
|
||||
|
||||
class Entity_Factory {
|
||||
|
||||
private $entity;
|
||||
protected $repository;
|
||||
protected $entity_type;
|
||||
protected $repository_type;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param $type
|
||||
* @param array $args
|
||||
* @param bool $is_validated_and_in_db
|
||||
*/
|
||||
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';
|
||||
|
||||
$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 ( $this->entity->validate() ) {
|
||||
$this->entity = $this->repository->insert( $this->entity );
|
||||
} else {
|
||||
throw new Exception( __( '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() ) {
|
||||
$this->entity = $this->repository->insert( $this->entity );
|
||||
} else {
|
||||
throw new Exception( __( 'The entity wasn\'t validated.' ) );
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException( __( 'One or more arguments are invalid.', 'tainacan' ) );
|
||||
}
|
||||
} catch (Error $exception){
|
||||
echo($exception->getMessage());
|
||||
echo($exception->getTraceAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the entity type ordered
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_entity(){
|
||||
return $this->entity;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
|
||||
use Tainacan\Tests\Factories;
|
||||
|
||||
class TAINACAN_UnitTestCase extends \WP_UnitTestCase {
|
||||
protected $tainacan_entity_factory;
|
||||
|
||||
public function setUp(){
|
||||
$this->tainacan_entity_factory = new Factories\Entity_Factory();
|
||||
}
|
||||
}
|
|
@ -11,26 +11,29 @@ namespace Tainacan\Tests;
|
|||
/**
|
||||
* Sample test case.
|
||||
*/
|
||||
class Collections extends \WP_UnitTestCase {
|
||||
class Collections extends TAINACAN_UnitTestCase {
|
||||
|
||||
/**
|
||||
* A single example test.
|
||||
*/
|
||||
function test_add() {
|
||||
|
||||
$x = new \Tainacan\Entities\Collection();
|
||||
|
||||
$x->set_name('teste');
|
||||
$x->set_description('adasdasdsa');
|
||||
$x->set_default_order('DESC');
|
||||
$this->tainacan_entity_factory->create_entity(
|
||||
'Collection',
|
||||
array(
|
||||
'name' => 'teste',
|
||||
'description' => 'adasdasdsa',
|
||||
'default_order' => 'DESC'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$x = $this->tainacan_entity_factory->get_entity();;
|
||||
|
||||
global $Tainacan_Collections;
|
||||
$x->validate();
|
||||
$col = $Tainacan_Collections->insert($x);
|
||||
|
||||
$this->assertEquals('Tainacan\Entities\Collection', get_class($col));
|
||||
$this->assertEquals('Tainacan\Entities\Collection', get_class($x));
|
||||
|
||||
$test = $Tainacan_Collections->fetch($col->get_id());
|
||||
$test = $Tainacan_Collections->fetch($x->get_id());
|
||||
|
||||
$this->assertEquals($test->get_name(), 'teste');
|
||||
$this->assertEquals($test->get_description(), 'adasdasdsa');
|
||||
|
|
Loading…
Reference in New Issue