Chris@13: createParamBuilder('test') Chris@0: ->setDefault($value) Chris@0: ->getNode() Chris@0: ; Chris@0: Chris@0: $this->assertEquals($expectedValueNode, $node->default); Chris@0: } Chris@0: Chris@0: public function provideTestDefaultValues() { Chris@13: return [ Chris@13: [ Chris@0: null, Chris@0: new Expr\ConstFetch(new Node\Name('null')) Chris@13: ], Chris@13: [ Chris@0: true, Chris@0: new Expr\ConstFetch(new Node\Name('true')) Chris@13: ], Chris@13: [ Chris@0: false, Chris@0: new Expr\ConstFetch(new Node\Name('false')) Chris@13: ], Chris@13: [ Chris@0: 31415, Chris@0: new Scalar\LNumber(31415) Chris@13: ], Chris@13: [ Chris@0: 3.1415, Chris@0: new Scalar\DNumber(3.1415) Chris@13: ], Chris@13: [ Chris@0: 'Hallo World', Chris@0: new Scalar\String_('Hallo World') Chris@13: ], Chris@13: [ Chris@13: [1, 2, 3], Chris@13: new Expr\Array_([ Chris@0: new Expr\ArrayItem(new Scalar\LNumber(1)), Chris@0: new Expr\ArrayItem(new Scalar\LNumber(2)), Chris@0: new Expr\ArrayItem(new Scalar\LNumber(3)), Chris@13: ]) Chris@13: ], Chris@13: [ Chris@13: ['foo' => 'bar', 'bar' => 'foo'], Chris@13: new Expr\Array_([ Chris@0: new Expr\ArrayItem( Chris@0: new Scalar\String_('bar'), Chris@0: new Scalar\String_('foo') Chris@0: ), Chris@0: new Expr\ArrayItem( Chris@0: new Scalar\String_('foo'), Chris@0: new Scalar\String_('bar') Chris@0: ), Chris@13: ]) Chris@13: ], Chris@13: [ Chris@0: new Scalar\MagicConst\Dir, Chris@0: new Scalar\MagicConst\Dir Chris@13: ] Chris@13: ]; Chris@0: } Chris@0: Chris@0: /** Chris@17: * @dataProvider provideTestTypes Chris@0: */ Chris@17: public function testTypes($typeHint, $expectedType) { Chris@0: $node = $this->createParamBuilder('test') Chris@0: ->setTypeHint($typeHint) Chris@0: ->getNode() Chris@0: ; Chris@0: $type = $node->type; Chris@0: Chris@0: /* Manually implement comparison to avoid __toString stupidity */ Chris@0: if ($expectedType instanceof Node\NullableType) { Chris@0: $this->assertInstanceOf(get_class($expectedType), $type); Chris@0: $expectedType = $expectedType->type; Chris@0: $type = $type->type; Chris@0: } Chris@0: Chris@13: $this->assertInstanceOf(get_class($expectedType), $type); Chris@13: $this->assertEquals($expectedType, $type); Chris@0: } Chris@0: Chris@17: public function provideTestTypes() { Chris@13: return [ Chris@13: ['array', new Node\Identifier('array')], Chris@13: ['callable', new Node\Identifier('callable')], Chris@13: ['bool', new Node\Identifier('bool')], Chris@13: ['int', new Node\Identifier('int')], Chris@13: ['float', new Node\Identifier('float')], Chris@13: ['string', new Node\Identifier('string')], Chris@13: ['iterable', new Node\Identifier('iterable')], Chris@13: ['object', new Node\Identifier('object')], Chris@13: ['Array', new Node\Identifier('array')], Chris@13: ['CALLABLE', new Node\Identifier('callable')], Chris@13: ['Some\Class', new Node\Name('Some\Class')], Chris@13: ['\Foo', new Node\Name\FullyQualified('Foo')], Chris@13: ['self', new Node\Name('self')], Chris@13: ['?array', new Node\NullableType(new Node\Identifier('array'))], Chris@13: ['?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))], Chris@13: [new Node\Name('Some\Class'), new Node\Name('Some\Class')], Chris@13: [ Chris@13: new Node\NullableType(new Node\Identifier('int')), Chris@13: new Node\NullableType(new Node\Identifier('int')) Chris@13: ], Chris@13: [ Chris@0: new Node\NullableType(new Node\Name('Some\Class')), Chris@0: new Node\NullableType(new Node\Name('Some\Class')) Chris@13: ], Chris@13: ]; Chris@0: } Chris@0: Chris@0: public function testVoidTypeError() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Parameter type cannot be void'); Chris@17: $this->createParamBuilder('test')->setType('void'); Chris@0: } Chris@0: Chris@0: public function testInvalidTypeError() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or NullableType'); Chris@17: $this->createParamBuilder('test')->setType(new \stdClass); Chris@0: } Chris@0: Chris@0: public function testByRef() { Chris@0: $node = $this->createParamBuilder('test') Chris@0: ->makeByRef() Chris@0: ->getNode() Chris@0: ; Chris@0: Chris@0: $this->assertEquals( Chris@13: new Node\Param(new Expr\Variable('test'), null, null, true), Chris@0: $node Chris@0: ); Chris@0: } Chris@0: Chris@0: public function testVariadic() { Chris@0: $node = $this->createParamBuilder('test') Chris@0: ->makeVariadic() Chris@0: ->getNode() Chris@0: ; Chris@0: Chris@0: $this->assertEquals( Chris@13: new Node\Param(new Expr\Variable('test'), null, null, false, true), Chris@0: $node Chris@0: ); Chris@0: } Chris@0: }