Skip to content

Commit 903bf99

Browse files
committed
Handle different cases for FieldtypeInteger.
1 parent e2a891a commit 903bf99

File tree

6 files changed

+107
-12
lines changed

6 files changed

+107
-12
lines changed
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
<?php namespace ProcessWire\GraphQL\Type\Fieldtype;
22

33
use GraphQL\Type\Definition\Type;
4-
use ProcessWire\GraphQL\Type\Fieldtype\Traits\FieldTrait;
4+
use ProcessWire\Page;
5+
use ProcessWire\GraphQL\Cache;
56
use ProcessWire\GraphQL\Type\Fieldtype\Traits\InputFieldTrait;
67
use ProcessWire\GraphQL\Type\Fieldtype\Traits\SetValueTrait;
78

89
class FieldtypeInteger
910
{
10-
use FieldTrait;
1111
use InputFieldTrait;
1212
use SetValueTrait;
1313
public static function type()
1414
{
1515
return Type::int();
1616
}
17+
18+
public static function field($field)
19+
{
20+
return Cache::field($field->name, function () use ($field) {
21+
// description
22+
$desc = $field->description;
23+
if (!$desc) {
24+
$desc = "Field with the type of {$field->type}";
25+
}
26+
27+
return [
28+
'name' => $field->name,
29+
'description' => $desc,
30+
'type' => self::type($field),
31+
'resolve' => function (Page $page) use ($field) {
32+
$fieldName = $field->name;
33+
$value = $page->$fieldName;
34+
if (empty($value) && !is_int($value)) {
35+
return null;
36+
}
37+
return $value;
38+
}
39+
];
40+
});
41+
}
1742
}

test/Field/Page/Fieldtype/FieldtypeInteger/FieldtypeIntegerTest.php renamed to test/Field/Page/Fieldtype/FieldtypeInteger/CaseOneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use \ProcessWire\GraphQL\Test\Field\Page\Fieldtype\Traits\FieldtypeTestTrait;
77
use \ProcessWire\GraphQL\Utils;
88

9-
class FieldtypeIntegerTest extends GraphqlTestCase {
9+
class FieldtypeIntegerCaseOneTest extends GraphqlTestCase {
1010

1111
const settings = [
1212
'login' => 'admin',
@@ -20,7 +20,7 @@ class FieldtypeIntegerTest extends GraphqlTestCase {
2020

2121
public function testValue()
2222
{
23-
$skyscraper = Utils::pages()->find("template=skyscraper, sort=random")->first();
23+
$skyscraper = Utils::pages()->get("template=skyscraper, sort=random");
2424
$query = "{
2525
skyscraper(s: \"id=$skyscraper->id\") {
2626
list {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Test\Field\Page\Fieldtype;
4+
5+
use \ProcessWire\GraphQL\Test\GraphqlTestCase;
6+
use \ProcessWire\GraphQL\Utils;
7+
8+
class FieldtypeIntegerCaseThreeTest extends GraphqlTestCase {
9+
10+
const settings = [
11+
'login' => 'admin',
12+
'legalTemplates' => ['skyscraper'],
13+
'legalFields' => ['floors'],
14+
'access' => [
15+
'fields' => [
16+
[
17+
'name' => 'floors',
18+
'zeroNotEmpty' => true
19+
]
20+
]
21+
]
22+
];
23+
24+
public function testValue()
25+
{
26+
$skyscraper = Utils::pages()->get("template=skyscraper, sort=random, floors=''");
27+
$query = "{
28+
skyscraper(s: \"id=$skyscraper->id\") {
29+
list {
30+
floors
31+
}
32+
}
33+
}";
34+
$res = self::execute($query);
35+
assertNull($res->data->skyscraper->list[0]->floors, 'Retrieves incorrect value.');
36+
assertObjectNotHasAttribute('errors', $res, 'There are errors.');
37+
}
38+
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Test\Field\Page\Fieldtype;
4+
5+
use \ProcessWire\GraphQL\Test\GraphqlTestCase;
6+
use \ProcessWire\GraphQL\Utils;
7+
8+
class FieldtypeIntegerCaseTwoTest extends GraphqlTestCase {
9+
10+
const settings = [
11+
'login' => 'admin',
12+
'legalTemplates' => ['skyscraper'],
13+
'legalFields' => ['floors'],
14+
];
15+
16+
public function testValue()
17+
{
18+
$skyscraper = Utils::pages()->get("template=skyscraper, sort=random, floors=0");
19+
$query = "{
20+
skyscraper(s: \"id=$skyscraper->id\") {
21+
list {
22+
floors
23+
}
24+
}
25+
}";
26+
$res = self::execute($query);
27+
assertEquals(0, $res->data->skyscraper->list[0]->floors, 'Retrieves incorrect value.');
28+
assertObjectNotHasAttribute('errors', $res, 'There are errors.');
29+
}
30+
31+
}

test/Performance/DBQueryCount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testDbQueryCount() {
118118
$expected = $skyscrapers->get("architects=$architectsWithResumes, sort=random");
119119
assertNotNull($expected, 'No expected skyscraper to check.');
120120
$actual = self::getListItemId($res->data->skyscraper->list, $expected->id);
121-
assertEquals(count($expected->architects), count($actual->architects), 'Incorrect architects amount.');
121+
assertEquals(count($expected->architects), count($actual->architects->list), 'Incorrect architects amount.');
122122
assertEquals($expected->architects[0]->id, $actual->architects->list[0]->id, 'Incorrect architect id.');
123123
assertEquals($expected->architects[0]->title, $actual->architects->list[0]->title, 'Incorrect architect title.');
124124
assertEquals($expected->architects[0]->created, $actual->architects->list[0]->created, 'Incorrect architect created.');

test/skyscrapers.sql

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)