Skip to content

Commit

Permalink
Adds tests for forced data type
Browse files Browse the repository at this point in the history
  • Loading branch information
doctordesh committed Dec 29, 2014
1 parent f7a5e84 commit c9c941f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Extender.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function extendPosts(array $posts, $data) {
}

private function loopHelper(\WP_Post $post, $row) {
$row->meta_value = $this->factory->create($row->field_type, $row->meta_value);
$row->meta_value = $this->factory->create($row->field_type, $row->meta_value, (array)@$row->options);

if($row->group_duplicated) {
$post = $this->setDuplicatedGroup($post, $row);
Expand Down
6 changes: 5 additions & 1 deletion src/Queryer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public function getMetaFor(array $posts) {
foreach($posts as $post) {
$ids[] = $post->ID;
}
return $this->sql($ids);
return array_map(function($item) {
$item->options = unserialize($item->options);
return $item;
}, $this->sql($ids));
}

private function sql($ids) {
Expand All @@ -32,6 +35,7 @@ private function sql($ids) {
" . $wpdb->prefix . "mf_post_meta.group_count,
" . $table_prefix . "mf_custom_fields.duplicated as field_duplicated,
" . $table_prefix . "mf_custom_fields.type as field_type,
" . $table_prefix . "mf_custom_fields.options as options,
" . $table_prefix . "mf_custom_groups.name as group_name,
" . $table_prefix . "mf_custom_groups.duplicated as group_duplicated
FROM
Expand Down
7 changes: 7 additions & 0 deletions tests/DataTypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ public function testCreateWithNonExistingForcedType() {
$this->assertTrue($obj === $data);
}



public function testForcedDataType() {
$obj = Page::find(2);
$this->assertTrue($obj->test[0]['related_super'] instanceof GoBrave\PostExtender\DataTypes\Related);
}

}
6 changes: 1 addition & 5 deletions tests/ExtenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ public function setUp() {
new WP_Post(['ID' => 4, 'post_title' => 'Lorem ipsum']),
new WP_Post(['ID' => 6, 'post_title' => 'Dolor sit amet'])
];
$this->e = new Extender(new GoBrave\PostExtender\Config([
'files_url' => MF_FILES_URL,
'files_dir' => MF_FILES_DIR,
'struct_dir' => __DIR__ . '/data'
]));
$this->e = new Extender(new GoBrave\PostExtender\Config(DATA::$CONFIG));
}

public function testExtendsWithBasicFields() {
Expand Down
6 changes: 3 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

define('MF_FILES_URL', 'http://files_mf');
define('MF_FILES_DIR', __DIR__ . '/data');

require_once(__DIR__ . '/bootstrap_namespace.php');
require_once(__DIR__ . '/bootstrap_data.php');

Expand Down Expand Up @@ -35,9 +38,6 @@ public function get_results($sql) {
}
}

define('MF_FILES_URL', 'http://files_mf');
define('MF_FILES_DIR', __DIR__ . '/data');

GoBrave\PostExtender\PostExtender::setConfig(new GoBrave\PostExtender\Config([
'files_url' => MF_FILES_URL,
'files_dir' => MF_FILES_DIR,
Expand Down
19 changes: 19 additions & 0 deletions tests/bootstrap_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ class DATA
{
public static $POST_1;
public static $POSTS;
public static $CONFIG = [
'files_url' => MF_FILES_URL,
'files_dir' => MF_FILES_DIR,
'struct_dir' => __DIR__ . '/data',
'namespace' => ''
];
}

DATA::$POST_1 = [
Expand Down Expand Up @@ -102,6 +108,19 @@ class DATA
'group_duplicated' => "1",
'options' => serialize([
])
],
(object)[
'ID' => "2",
'meta_key' => "test_related_super",
'meta_value' => "2",
'group_count' => "1",
'field_duplicated' => "0",
'field_type' => "textbox",
'group_name' => "test",
'group_duplicated' => "1",
'options' => serialize([
"force_type" => "related_type"
])
]
];

Expand Down

0 comments on commit c9c941f

Please sign in to comment.