|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\AstGenerator\Tests\Hydrate\Type; |
| 13 | + |
| 14 | +use PhpParser\Node\Expr; |
| 15 | +use PhpParser\PrettyPrinter\Standard; |
| 16 | +use Prophecy\Argument; |
| 17 | +use Symfony\Component\AstGenerator\AstGeneratorInterface; |
| 18 | +use Symfony\Component\AstGenerator\Hydrate\Type\CollectionTypeGenerator; |
| 19 | +use Symfony\Component\PropertyInfo\Type; |
| 20 | + |
| 21 | +class CollectionTypeGeneratorTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + /** @var Standard */ |
| 24 | + protected $printer; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->printer = new Standard(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @expectedException \Symfony\Component\AstGenerator\Exception\MissingContextException |
| 33 | + */ |
| 34 | + public function testNoInput() |
| 35 | + { |
| 36 | + $itemGenerator = $this->prophesize(AstGeneratorInterface::class); |
| 37 | + $hydrateGenerator = new CollectionTypeGenerator($itemGenerator->reveal()); |
| 38 | + $hydrateGenerator->generate(new Type('array', false, null, true)); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @expectedException \Symfony\Component\AstGenerator\Exception\MissingContextException |
| 43 | + */ |
| 44 | + public function testNoOutput() |
| 45 | + { |
| 46 | + $itemGenerator = $this->prophesize(AstGeneratorInterface::class); |
| 47 | + $hydrateGenerator = new CollectionTypeGenerator($itemGenerator->reveal()); |
| 48 | + $hydrateGenerator->generate(new Type('array', false, null, true), ['input' => new Expr\Variable('test')]); |
| 49 | + } |
| 50 | + |
| 51 | + public function testDefaultWithNumericalArray() |
| 52 | + { |
| 53 | + $collectionKeyType = new Type('int'); |
| 54 | + $collectionValueType = new Type('string'); |
| 55 | + $type = new Type('array', false, null, true, $collectionKeyType, $collectionValueType); |
| 56 | + |
| 57 | + $itemGenerator = $this->prophesize(AstGeneratorInterface::class); |
| 58 | + $itemGenerator->supportsGeneration($collectionValueType)->willReturn(true); |
| 59 | + $itemGenerator->generate($collectionValueType, Argument::type('array'))->will(function ($args) { |
| 60 | + return [new Expr\Assign($args[1]['output'], $args[1]['input'])]; |
| 61 | + }); |
| 62 | + |
| 63 | + $generator = new CollectionTypeGenerator($itemGenerator->reveal()); |
| 64 | + |
| 65 | + $this->assertTrue($generator->supportsGeneration($type)); |
| 66 | + |
| 67 | + $input = [ |
| 68 | + 'foo', |
| 69 | + 'bar', |
| 70 | + ]; |
| 71 | + |
| 72 | + eval($this->printer->prettyPrint($generator->generate($type, [ |
| 73 | + 'input' => new Expr\Variable('input'), |
| 74 | + 'output' => new Expr\Variable('output'), |
| 75 | + ]))); |
| 76 | + |
| 77 | + $this->assertInternalType('array', $output); |
| 78 | + $this->assertCount(2, $output); |
| 79 | + $this->assertEquals('foo', $output[0]); |
| 80 | + $this->assertEquals('bar', $output[1]); |
| 81 | + } |
| 82 | + |
| 83 | + public function testDefaultWithMapArray() |
| 84 | + { |
| 85 | + $collectionKeyType = new Type('string'); |
| 86 | + $collectionValueType = new Type('string'); |
| 87 | + $type = new Type('array', false, null, true, $collectionKeyType, $collectionValueType); |
| 88 | + |
| 89 | + $itemGenerator = $this->prophesize(AstGeneratorInterface::class); |
| 90 | + $itemGenerator->supportsGeneration($collectionValueType)->willReturn(true); |
| 91 | + $itemGenerator->generate($collectionValueType, Argument::type('array'))->will(function ($args) { |
| 92 | + return [new Expr\Assign($args[1]['output'], $args[1]['input'])]; |
| 93 | + }); |
| 94 | + |
| 95 | + $generator = new CollectionTypeGenerator($itemGenerator->reveal()); |
| 96 | + |
| 97 | + $this->assertTrue($generator->supportsGeneration($type)); |
| 98 | + |
| 99 | + $input = [ |
| 100 | + 'foo' => 'foo', |
| 101 | + 'bar' => 'bar', |
| 102 | + ]; |
| 103 | + |
| 104 | + eval($this->printer->prettyPrint($generator->generate($type, [ |
| 105 | + 'input' => new Expr\Variable('input'), |
| 106 | + 'output' => new Expr\Variable('output'), |
| 107 | + ]))); |
| 108 | + |
| 109 | + $this->assertInstanceOf('\stdClass', $output); |
| 110 | + $this->assertObjectHasAttribute('foo', $output); |
| 111 | + $this->assertObjectHasAttribute('bar', $output); |
| 112 | + $this->assertEquals('foo', $output->foo); |
| 113 | + $this->assertEquals('bar', $output->bar); |
| 114 | + } |
| 115 | + |
| 116 | + public function testCustomObject() |
| 117 | + { |
| 118 | + $collectionKeyType = new Type('string'); |
| 119 | + $collectionValueType = new Type('string'); |
| 120 | + $type = new Type('array', false, null, true, $collectionKeyType, $collectionValueType); |
| 121 | + |
| 122 | + $itemGenerator = $this->prophesize(AstGeneratorInterface::class); |
| 123 | + $itemGenerator->supportsGeneration($collectionValueType)->willReturn(true); |
| 124 | + $itemGenerator->generate($collectionValueType, Argument::type('array'))->will(function ($args) { |
| 125 | + return [new Expr\Assign($args[1]['output'], $args[1]['input'])]; |
| 126 | + }); |
| 127 | + |
| 128 | + $generator = new CollectionTypeGenerator( |
| 129 | + $itemGenerator->reveal(), |
| 130 | + CollectionTypeGenerator::COLLECTION_WITH_OBJECT, |
| 131 | + '\ArrayObject', |
| 132 | + CollectionTypeGenerator::OBJECT_ASSIGNMENT_ARRAY |
| 133 | + ); |
| 134 | + |
| 135 | + $this->assertTrue($generator->supportsGeneration($type)); |
| 136 | + |
| 137 | + $input = [ |
| 138 | + 'foo' => 'foo', |
| 139 | + 'bar' => 'bar', |
| 140 | + ]; |
| 141 | + |
| 142 | + eval($this->printer->prettyPrint($generator->generate($type, [ |
| 143 | + 'input' => new Expr\Variable('input'), |
| 144 | + 'output' => new Expr\Variable('output'), |
| 145 | + ]))); |
| 146 | + |
| 147 | + $this->assertInstanceOf('\ArrayObject', $output); |
| 148 | + $this->assertArrayHasKey('foo', $output); |
| 149 | + $this->assertArrayHasKey('bar', $output); |
| 150 | + $this->assertEquals('foo', $output['foo']); |
| 151 | + $this->assertEquals('bar', $output['bar']); |
| 152 | + } |
| 153 | +} |
0 commit comments