diff --git a/tests/Validator/DisableIntrospectionTest.php b/tests/Validator/DisableIntrospectionTest.php new file mode 100644 index 0000000..88d771c --- /dev/null +++ b/tests/Validator/DisableIntrospectionTest.php @@ -0,0 +1,128 @@ +expectFailsRule(new DisableIntrospection(DisableIntrospection::ENABLED), ' + query { + __schema { + queryType { + name + } + } + } + ', + [$this->error(3, 9)] + ); + } + + /** + * @it fails if the query contains __type + */ + public function testQueryContainsType() + { + $this->expectFailsRule(new DisableIntrospection(DisableIntrospection::ENABLED), ' + query { + __type( + name: "Query" + ){ + name + } + } + ', + [$this->error(3, 9)] + ); + } + + /** + * @it does not fail on a query that does not contain __type + */ + public function testValidQuery() + { + $this->expectPassesRule(new DisableIntrospection(DisableIntrospection::ENABLED), ' + query { + user { + name + email + friends { + name + } + } + } + '); + } + + /** + * @it does not fail when not enabled + */ + public function testQueryWhenDisabled() + { + $this->expectPassesRule(new DisableIntrospection(DisableIntrospection::DISABLED), ' + query { + __type( + name: "Query" + ){ + name + } + } + '); + } + + /** + * @it has a public interface for enabeling the rule + */ + public function testPublicEnableInterface() + { + $disableIntrospection = new DisableIntrospection(DisableIntrospection::DISABLED); + $disableIntrospection->setEnabled(DisableIntrospection::ENABLED); + $this->expectFailsRule($disableIntrospection, ' + query { + __type( + name: "Query" + ){ + name + } + } + ', + [$this->error(3, 9)] + ); + } + + /** + * @it has a public interface for disableing the rule + */ + public function testPublicDisableInterface() + { + $disableIntrospection = new DisableIntrospection(DisableIntrospection::ENABLED); + $disableIntrospection->setEnabled(DisableIntrospection::DISABLED); + $this->expectPassesRule($disableIntrospection, ' + query { + __type( + name: "Query" + ){ + name + } + } + '); + } + + + private function error($line, $column) + { + return FormattedError::create( + DisableIntrospection::introspectionDisabledMessage(), + [ new SourceLocation($line, $column) ] + ); + } +}