| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 | <?phpnamespace Guzzle\Tests\Service\Description;use GuzzleHttp\Command\Guzzle\Description;use GuzzleHttp\Command\Guzzle\Parameter;/** * @covers \GuzzleHttp\Command\Guzzle\Parameter */class ParameterTest extends \PHPUnit_Framework_TestCase{    protected $data = [        'name'            => 'foo',        'type'            => 'bar',        'required'        => true,        'default'         => '123',        'description'     => '456',        'minLength'       => 2,        'maxLength'       => 5,        'location'        => 'body',        'static'          => true,        'filters'         => ['trim', 'json_encode']    ];    public function testCreatesParamFromArray()    {        $p = new Parameter($this->data);        $this->assertEquals('foo', $p->getName());        $this->assertEquals('bar', $p->getType());        $this->assertTrue($p->isRequired());        $this->assertEquals('123', $p->getDefault());        $this->assertEquals('456', $p->getDescription());        $this->assertEquals(2, $p->getMinLength());        $this->assertEquals(5, $p->getMaxLength());        $this->assertEquals('body', $p->getLocation());        $this->assertTrue($p->isStatic());        $this->assertEquals(['trim', 'json_encode'], $p->getFilters());        $p->setName('abc');        $this->assertEquals('abc', $p->getName());    }    /**     * @expectedException \InvalidArgumentException     */    public function testValidatesDescription()    {        new Parameter($this->data, ['description' => 'foo']);    }    public function testCanConvertToArray()    {        $p = new Parameter($this->data);        $this->assertEquals($this->data, $p->toArray());    }    public function testUsesStatic()    {        $d = $this->data;        $d['default'] = 'booboo';        $d['static'] = true;        $p = new Parameter($d);        $this->assertEquals('booboo', $p->getValue('bar'));    }    public function testUsesDefault()    {        $d = $this->data;        $d['default'] = 'foo';        $d['static'] = null;        $p = new Parameter($d);        $this->assertEquals('foo', $p->getValue(null));    }    public function testReturnsYourValue()    {        $d = $this->data;        $d['static'] = null;        $p = new Parameter($d);        $this->assertEquals('foo', $p->getValue('foo'));    }    public function testZeroValueDoesNotCauseDefaultToBeReturned()    {        $d = $this->data;        $d['default'] = '1';        $d['static'] = null;        $p = new Parameter($d);        $this->assertEquals('0', $p->getValue('0'));    }    public function testFiltersValues()    {        $d = $this->data;        $d['static'] = null;        $d['filters'] = 'strtoupper';        $p = new Parameter($d);        $this->assertEquals('FOO', $p->filter('foo'));    }    /**     * @expectedException \RuntimeException     * @expectedExceptionMessage No service description     */    public function testRequiresServiceDescriptionForFormatting()    {        $d = $this->data;        $d['format'] = 'foo';        $p = new Parameter($d);        $p->filter('bar');    }    public function testConvertsBooleans()    {        $p = new Parameter(['type' => 'boolean']);        $this->assertEquals(true, $p->filter('true'));        $this->assertEquals(false, $p->filter('false'));    }    public function testUsesArrayByDefaultForFilters()    {        $d = $this->data;        $d['filters'] = null;        $p = new Parameter($d);        $this->assertEquals([], $p->getFilters());    }    public function testAllowsSimpleLocationValue()    {        $p = new Parameter(['name' => 'myname', 'location' => 'foo', 'sentAs' => 'Hello']);        $this->assertEquals('foo', $p->getLocation());        $this->assertEquals('Hello', $p->getSentAs());    }    public function testParsesTypeValues()    {        $p = new Parameter(['type' => 'foo']);        $this->assertEquals('foo', $p->getType());    }    /**     * @expectedException \InvalidArgumentException     * @expectedExceptionMessage A [method] value must be specified for each complex filter     */    public function testValidatesComplexFilters()    {        $p = new Parameter(['filters' => [['args' => 'foo']]]);    }    public function testAllowsComplexFilters()    {        $that = $this;        $param = new Parameter([            'filters' => [                [                    'method' => function ($a, $b, $c, $d) use ($that, &$param) {                        $that->assertEquals('test', $a);                        $that->assertEquals('my_value!', $b);                        $that->assertEquals('bar', $c);                        $that->assertSame($param, $d);                        return 'abc' . $b;                    },                    'args' => ['test', '@value', 'bar', '@api']                ]            ]        ]);        $this->assertEquals('abcmy_value!', $param->filter('my_value!'));    }    public function testAddsAdditionalProperties()    {        $p = new Parameter([            'type' => 'object',            'additionalProperties' => ['type' => 'string']        ]);        $this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $p->getAdditionalProperties());        $this->assertNull($p->getAdditionalProperties()->getAdditionalProperties());        $p = new Parameter(['type' => 'object']);        $this->assertTrue($p->getAdditionalProperties());    }    public function testAddsItems()    {        $p = new Parameter([            'type'  => 'array',            'items' => ['type' => 'string']        ]);        $this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $p->getItems());        $out = $p->toArray();        $this->assertEquals('array', $out['type']);        $this->assertInternalType('array', $out['items']);    }    public function testCanRetrieveKnownPropertiesUsingDataMethod()    {        $p = new Parameter(['data' => ['name' => 'test'], 'extra' => 'hi!']);        $this->assertEquals('test', $p->getData('name'));        $this->assertEquals(['name' => 'test'], $p->getData());        $this->assertNull($p->getData('fjnweefe'));        $this->assertEquals('hi!', $p->getData('extra'));    }    public function testHasPattern()    {        $p = new Parameter(['pattern' => '/[0-9]+/']);        $this->assertEquals('/[0-9]+/', $p->getPattern());    }    public function testHasEnum()    {        $p = new Parameter(['enum' => ['foo', 'bar']]);        $this->assertEquals(['foo', 'bar'], $p->getEnum());    }    public function testSerializesItems()    {        $p = new Parameter([            'type'  => 'object',            'additionalProperties' => ['type' => 'string']        ]);        $this->assertEquals([            'type'  => 'object',            'additionalProperties' => ['type' => 'string']        ], $p->toArray());    }    public function testResolvesRefKeysRecursively()    {        $description = new Description([            'models' => [                'JarJar' => ['type' => 'string', 'default' => 'Mesa address tha senate!'],                'Anakin' => ['type' => 'array', 'items' => ['$ref' => 'JarJar']]            ],        ]);        $p = new Parameter(['$ref' => 'Anakin', 'description' => 'added'], ['description' => $description]);        $this->assertEquals([            'description' => 'added',            '$ref' => 'Anakin'        ], $p->toArray());    }    public function testResolvesExtendsRecursively()    {        $jarJar = ['type' => 'string', 'default' => 'Mesa address tha senate!', 'description' => 'a'];        $anakin = ['type' => 'array', 'items' => ['extends' => 'JarJar', 'description' => 'b']];        $description = new Description([            'models' => ['JarJar' => $jarJar, 'Anakin' => $anakin]        ]);        // Description attribute will be updated, and format added        $p = new Parameter(['extends' => 'Anakin', 'format' => 'date'], ['description' => $description]);        $this->assertEquals([            'format' => 'date',            'extends' => 'Anakin'        ], $p->toArray());    }    public function testHasKeyMethod()    {        $p = new Parameter(['name' => 'foo', 'sentAs' => 'bar']);        $this->assertEquals('bar', $p->getWireName());    }    public function testIncludesNameInToArrayWhenItemsAttributeHasName()    {        $p = new Parameter([            'type' => 'array',            'name' => 'Abc',            'items' => [                'name' => 'Foo',                'type' => 'object'            ]        ]);        $result = $p->toArray();        $this->assertEquals([            'type' => 'array',            'name' => 'Abc',            'items' => [                'name' => 'Foo',                'type' => 'object'            ]        ], $result);    }    public function dateTimeProvider()    {        $d = 'October 13, 2012 16:15:46 UTC';        return [            [$d, 'date-time', '2012-10-13T16:15:46Z'],            [$d, 'date', '2012-10-13'],            [$d, 'timestamp', strtotime($d)],            [new \DateTime($d), 'timestamp', strtotime($d)]        ];    }    /**     * @dataProvider dateTimeProvider     */    public function testAppliesFormat($d, $format, $result)    {        $p = new Parameter(['format' => $format], ['description' => new Description([])]);        $this->assertEquals($format, $p->getFormat());        $this->assertEquals($result, $p->filter($d));    }    public function testHasMinAndMax()    {        $p = new Parameter([            'minimum' => 2,            'maximum' => 3,            'minItems' => 4,            'maxItems' => 5,        ]);        $this->assertEquals(2, $p->getMinimum());        $this->assertEquals(3, $p->getMaximum());        $this->assertEquals(4, $p->getMinItems());        $this->assertEquals(5, $p->getMaxItems());    }    public function testHasProperties()    {        $data = [            'type' => 'object',            'properties' => [                'foo' => ['type' => 'string'],                'bar' => ['type' => 'string'],            ]        ];        $p = new Parameter($data);        $this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Parameter', $p->getProperty('foo'));        $this->assertSame($p->getProperty('foo'), $p->getProperty('foo'));        $this->assertNull($p->getProperty('wefwe'));        $properties = $p->getProperties();        $this->assertInternalType('array', $properties);        foreach ($properties as $prop) {            $this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Parameter', $prop);        }        $this->assertEquals($data, $p->toArray());    }    /**     * @expectedException \InvalidArgumentException     * @expectedExceptionMessage Expected a string. Got: array     */    public function testThrowsWhenNotPassString()    {        $emptyParam = new Parameter();        $this->assertFalse($emptyParam->has([]));        $this->assertFalse($emptyParam->has(new \stdClass()));        $this->assertFalse($emptyParam->has('1'));        $this->assertFalse($emptyParam->has(1));    }    public function testHasReturnsFalseForWrongOrEmptyValues()    {        $emptyParam = new Parameter();        $this->assertFalse($emptyParam->has(''));        $this->assertFalse($emptyParam->has('description'));        $this->assertFalse($emptyParam->has('noExisting'));    }    public function testHasReturnsTrueForCorrectValues()    {        $p = new Parameter([            'minimum' => 2,            'maximum' => 3,            'minItems' => 4,            'maxItems' => 5,        ]);        $this->assertTrue($p->has('minimum'));        $this->assertTrue($p->has('maximum'));        $this->assertTrue($p->has('minItems'));        $this->assertTrue($p->has('maxItems'));    }}
 |