Skip to content

Commit

Permalink
Tests done
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Aug 1, 2021
1 parent 0049f7f commit 01f8c23
Show file tree
Hide file tree
Showing 19 changed files with 470 additions and 110 deletions.
4 changes: 0 additions & 4 deletions _config/dbargs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Name: graphql-db-args
---
# For the DBFieldArgsPlugin, assign each DBField type an args factory
SilverStripe\ORM\FieldType\DBString:
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBStringArgs
SilverStripe\ORM\FieldType\DBText:
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBTextArgs
SilverStripe\ORM\FieldType\DBHTMLText:
Expand All @@ -18,5 +16,3 @@ SilverStripe\ORM\FieldType\DBDecimal:
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDecimalArgs
SilverStripe\ORM\FieldType\DBFloat:
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBFloatArgs
SilverStripe\ORM\FieldType\DBCurrency:
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBCurrencyArgs
29 changes: 0 additions & 29 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBCurrencyArgs.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Schema/DataObject/Plugin/DBFieldArgs/DBDecimalArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ protected function getResolver(): callable
public function getValues(): array
{
return [
'NICE' => 'Nice',
'INT' => 'Int',
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ public function getIdentifier(): string
public static function updateSchema(Schema $schema): void
{
$schema
->addEnum(DBStringArgs::create()->getEnum())
->addEnum(DBTextArgs::create()->getEnum())
->addEnum(DBHTMLTextArgs::create()->getEnum())
->addEnum(DBDecimalArgs::create()->getEnum())
->addEnum(DBCurrencyArgs::create()->getEnum())
->addEnum(DBFloatArgs::create()->getEnum())
->addEnum(DBDateArgs::create()->getEnum())
->addEnum(DBDatetimeArgs::create()->getEnum())
Expand Down
42 changes: 0 additions & 42 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBStringArgs.php

This file was deleted.

36 changes: 18 additions & 18 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBTextArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use SilverStripe\ORM\FieldType\DBString;
use Exception;

class DBTextArgs extends DBStringArgs
class DBTextArgs extends DBFieldArgs
{
public function getEnum(): Enum
{
Expand All @@ -21,15 +21,12 @@ public function getEnum(): Enum

public function getValues(): array
{
return array_merge(
parent::getValues(),
[
'CONTEXT_SUMMARY' => 'ContextSummary',
'FIRST_PARAGRAPH' => 'FirstParagraph',
'LIMIT_SENTENCES' => 'LimitSentences',
'SUMMARY' => 'Summary',
]
);
return [
'CONTEXT_SUMMARY' => 'ContextSummary',
'FIRST_PARAGRAPH' => 'FirstParagraph',
'LIMIT_SENTENCES' => 'LimitSentences',
'SUMMARY' => 'Summary',
];
}

public function applyToField(ModelField $field): void
Expand Down Expand Up @@ -62,23 +59,26 @@ protected function getResolver(): callable
*/
public static function resolve(DBString $obj, array $args)
{
$result = DBFieldArgs::baseFormatResolver($obj, $args);

// If no referential equality, the parent did something, so we're done.
if ($result !== $obj) {
return $result;
}

$format = $args['format'] ?? null;
$limit = $args['limit'] ?? null;

if (!$format) {
return $obj;
}

if ($limit && in_array($format, ['FirstParagraph'])) {
$noArgMethods = ['FirstParagraph'];

if ($limit && in_array($format, $noArgMethods)) {
throw new Exception(sprintf('Arg "limit" is not allowed for format "%s"', $format));
}

$result = DBFieldArgs::baseFormatResolver($obj, $args);

// If no referential equality, the parent did something, so we're done.
if ($result !== $obj) {
return $result;
}

if ($format) {
$args = $limit === null ? [] : [$limit];
if ($obj->hasMethod($format)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBTimeArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\ORM\FieldType\DBField;
use Exception;
use SilverStripe\ORM\FieldType\DBTime;

class DBTimeArgs extends DBFieldArgs
{
Expand Down Expand Up @@ -41,12 +42,12 @@ protected function getResolver(): callable
}

/**
* @param DBDate $obj
* @param DBTime $obj
* @param array $args
* @return DBField | string
* @throws Exception
*/
public static function resolve(DBDate $obj, array $args)
public static function resolve(DBTime $obj, array $args)
{
$format = $args['format'] ?? null;
$custom = $args['customFormat'] ?? null;
Expand Down
2 changes: 2 additions & 0 deletions tests/Fake/DataObjectFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class DataObjectFake extends DataObject implements TestOnly
'MyDate' => 'Datetime',
'MyCurrency' => 'Currency',
'MyText' => 'Text',
'MyEnum' => "Enum('ONE, TWO')",
'MyMoney' => 'Money',
];

private static $has_one = [
Expand Down
55 changes: 55 additions & 0 deletions tests/Schema/DataObject/Plugin/DBFieldArgs/DBDateArgsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace SilverStripe\GraphQL\Tests\Schema\DataObject\Plugin\DBFieldArgs;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\DataObject\DataObjectModel;
use SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDateArgs;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\SchemaConfig;
use SilverStripe\GraphQL\Tests\Fake\DataObjectFake;
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\ORM\FieldType\DBField;

class DBDateArgsTest extends SapphireTest
{
public function testApply()
{
$field = new ModelField('test', [], new DataObjectModel(DataObjectFake::class, new SchemaConfig()));
$factory = new DBDateArgs();
$factory->applyToField($field);
$args = $field->getArgs();

$this->assertArrayHasKey('format', $args);
$arg = $args['format'];
$this->assertEquals($factory->getEnum()->getName(), $arg->getType());

$this->assertArrayHasKey('customFormat', $args);
$arg = $args['customFormat'];
$this->assertEquals('String', $arg->getType());
}

public function testResolve()
{
$fake = $this->getMockBuilder(DBDate::class)
->setMethods(['Nice'])
->getMock();
$fake->expects($this->once())
->method('Nice');

DBDateArgs::resolve($fake, ['format' => 'Nice']);

$date = DBField::create_field('Date', '123445789');
$result = DBDateArgs::resolve($date, ['format' => 'FAIL']);
// Referential equality if method not found
$this->assertEquals($result, $date);

$this->expectExceptionMessage('The "custom" option requires a value for "customFormat"');

DBDateArgs::resolve($date, ['format' => 'Custom']);

$this->expectExceptionMessage('The "customFormat" argument should not be set for formats that are not "custom"');

DBDateArgs::resolve($date, ['customFormat' => 'test']);
}
}
56 changes: 56 additions & 0 deletions tests/Schema/DataObject/Plugin/DBFieldArgs/DBDatetimeArgsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace SilverStripe\GraphQL\Tests\Schema\DataObject\Plugin\DBFieldArgs;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\DataObject\DataObjectModel;
use SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDateArgs;
use SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDatetimeArgs;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\SchemaConfig;
use SilverStripe\GraphQL\Tests\Fake\DataObjectFake;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBField;

class DBDatetimeArgsTest extends SapphireTest
{
public function testApply()
{
$field = new ModelField('test', [], new DataObjectModel(DataObjectFake::class, new SchemaConfig()));
$factory = new DBDatetimeArgs();
$factory->applyToField($field);
$args = $field->getArgs();

$this->assertArrayHasKey('format', $args);
$arg = $args['format'];
$this->assertEquals($factory->getEnum()->getName(), $arg->getType());

$this->assertArrayHasKey('customFormat', $args);
$arg = $args['customFormat'];
$this->assertEquals('String', $arg->getType());
}

public function testResolve()
{
$fake = $this->getMockBuilder(DBDatetime::class)
->setMethods(['Time'])
->getMock();
$fake->expects($this->once())
->method('Time');

DBDatetimeArgs::resolve($fake, ['format' => 'Time']);

$date = DBField::create_field('Datetime', '123445789');
$result = DBDateArgs::resolve($date, ['format' => 'FAIL']);
// Referential equality if method not found
$this->assertEquals($result, $date);

$this->expectExceptionMessage('The "custom" option requires a value for "customFormat"');

DBDateArgs::resolve($date, ['format' => 'Custom']);

$this->expectExceptionMessage('The "customFormat" argument should not be set for formats that are not "custom"');

DBDateArgs::resolve($date, ['customFormat' => 'test']);
}
}
25 changes: 25 additions & 0 deletions tests/Schema/DataObject/Plugin/DBFieldArgs/DBDecimalArgsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SilverStripe\GraphQL\Tests\Schema\DataObject\Plugin\DBFieldArgs;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\DataObject\DataObjectModel;
use SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDecimalArgs;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\SchemaConfig;
use SilverStripe\GraphQL\Tests\Fake\DataObjectFake;

class DBDecimalArgsTest extends SapphireTest
{
public function testApply()
{
$field = new ModelField('test', [], new DataObjectModel(DataObjectFake::class, new SchemaConfig()));
$factory = new DBDecimalArgs();
$factory->applyToField($field);
$args = $field->getArgs();

$this->assertArrayHasKey('format', $args);
$arg = $args['format'];
$this->assertEquals($factory->getEnum()->getName(), $arg->getType());
}
}
Loading

0 comments on commit 01f8c23

Please sign in to comment.