Source of file TypeCreatorTest.php
Size: 4,865 Bytes - Last Modified: 2021-12-23T10:31:47+00:00
/var/www/docs.ssmods.com/process/src/tests/TypeCreatorTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 | <?php namespace SilverStripe\GraphQL\Tests; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Dev\SapphireTest; use SilverStripe\GraphQL\TypeCreator; class TypeCreatorTest extends SapphireTest { public function testGetFields() { $mock = $this->getTypeCreatorMock(); $mock->method('fields')->willReturn([ 'ID' => [ 'type' => Type::nonNull(Type::id()), ], ]); $fields = $mock->getFields(); $this->assertArrayHasKey('ID', $fields); } public function testToArray() { $mock = $this->getTypeCreatorMock(); $actual = $mock->toArray(); $this->assertArrayHasKey('fields', $actual); $fields = $actual['fields'](); $this->assertArrayHasKey('ID', $fields); } public function testToType() { $mock = $this->getTypeCreatorMock(); $actual = $mock->toType(); $this->assertInstanceOf(Type::class, $actual); } public function testToTypeWithInputObject() { $mock = $this->getTypeCreatorMock(['isInputObject']); $mock->method('isInputObject')->willReturn(true); $actual = $mock->toType(); $this->assertInstanceOf(InputObjectType::class, $actual); } public function testGetAttributes() { $mock = $this->getTypeCreatorMock(); $actual = $mock->getAttributes(); $this->assertArrayHasKey('fields', $actual); $fields = $actual['fields'](); $this->assertArrayHasKey('ID', $fields); } public function testGetFieldsUsesResolveConfig() { $mock = $this->getTypeCreatorMock(['resolveFieldAField', 'fields']); $mock->method('fields')->willReturn([ 'fieldA' => [ 'type' => Type::string(), 'resolve' => function () { return 'config'; }, ], 'fieldB' => [ 'type' => Type::string(), ], ]); $mock->method('resolveFieldAField') ->willReturn('method'); $fields = $mock->getFields(); $this->assertArrayHasKey('fieldA', $fields); $this->assertArrayHasKey('fieldB', $fields); $this->assertArrayHasKey('resolve', $fields['fieldA']); $this->assertArrayNotHasKey('resolve', $fields['fieldB']); $this->assertEquals('config', $fields['fieldA']['resolve']()); } public function testGetFieldsUsesResolverMethod() { $mock = $this->getTypeCreatorMock(['resolveFieldAField', 'fields']); $mock->method('fields')->willReturn([ 'fieldA' => [ 'type' => Type::string(), ], 'fieldB' => [ 'type' => Type::string(), ], ]); $mock->method('resolveFieldAField') ->willReturn('resolved'); $fields = $mock->getFields(); $this->assertArrayHasKey('fieldA', $fields); $this->assertArrayHasKey('fieldB', $fields); $this->assertArrayHasKey('resolve', $fields['fieldA']); $this->assertArrayNotHasKey('resolve', $fields['fieldB']); } public function testGetFieldsUsesAllFieldsResolverMethod() { $mock = $this->getTypeCreatorMock(['resolveField', 'fields']); $mock->method('fields')->willReturn([ 'fieldA' => [ 'type' => Type::string(), ], 'fieldB' => [ 'type' => Type::string(), ], ]); $mock->method('resolveField') ->willReturn('resolved'); $fields = $mock->getFields(); $this->assertArrayHasKey('fieldA', $fields); $this->assertArrayHasKey('fieldB', $fields); $this->assertArrayHasKey('resolve', $fields['fieldA']); $this->assertArrayHasKey('resolve', $fields['fieldB']); $this->assertEquals('resolved', $fields['fieldA']['resolve']()); $this->assertEquals('resolved', $fields['fieldB']['resolve']()); } /** * @param array $extraMethods * @return MockObject|TypeCreator */ protected function getTypeCreatorMock($extraMethods = []) { $mock = $this->getMockBuilder(TypeCreator::class) ->setMethods(array_unique(array_merge(['fields', 'attributes'], $extraMethods))) ->getMock(); if (!in_array('fields', $extraMethods)) { $mock->method('fields')->willReturn([ 'ID' => [ 'type' => Type::nonNull(Type::id()), ], ]); } if (!in_array('attributes', $extraMethods)) { $mock->method('attributes') ->willReturn(['name' => 'myType']); } return $mock; } } |