<commit_before><?php
/*
 * PSX is a open source PHP framework to develop RESTful APIs.
 * For the current version and informations visit <http://phpsx.org>
 *
 * Copyright 2010-2015 <NAME> <<EMAIL>>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace PSX\Api\Documentation\Parser;

use PSX\Data\SchemaInterface;

/**
 * RamlTest
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @link    http://phpsx.org
 */
class RamlTest extends \PHPUnit_Framework_TestCase
{
	public function testParse()
	{
		$doc = Raml::fromFile(__DIR__ . '/test.raml', '/foo');

		$this->assertInstanceOf('PSX\Api\DocumentationInterface', $doc);
		$this->assertEquals('World Music API', $doc->getDescription());
		$this->assertEquals(2, $doc->getLatestVersion());

		$resource = $doc->getResource(2);

		$this->assertInstanceOf('PSX\Api\Resource', $resource);
		$this->assertEquals(array('GET', 'POST'), $resource->getAllowedMethods());
		$this->assertEquals('Bar', $resource->getTitle());
		$this->assertEquals('Some description', $resource->getDescription());

		// check GET
		$this->assertEquals('Informations about the method', $resource->getMethod('GET')->getDescription());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getMethod('GET')->getQueryParameters());
		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $resource->getMethod('GET')->getQueryParameters()->getDefinition());
		$this->assertInstanceOf('PSX\Data\Schema\Property\FloatType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('pages'));
		$this->assertEquals('The number of pages to return', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('pages')->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\IntegerType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_integer'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\FloatType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_number'));
		$this->assertEquals('The number', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_number')->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\DateTimeType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_date'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\BooleanType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_boolean'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_string'));
		$this->assertParameters($resource->getMethod('GET')->getQueryParameters());

		// check POST
		$this->assertInstanceOf('PSX\Api\Resource\Post', $resource->getMethod('POST'));

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $resource->getMethod('POST')->getQueryParameters()->getDefinition());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getMethod('POST')->getResponse(200));

		$property = $resource->getMethod('POST')->getRequest()->getDefinition();

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $property);
		$this->assertEquals('A canonical song', $property->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('title'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('artist'));

		$property = $resource->getMethod('POST')->getResponse(200)->getDefinition();

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $property);
		$this->assertEquals('A canonical song', $property->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('title'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('artist'));
	}

	public function testParsePath()
	{
		$doc = Raml::fromFile(__DIR__ . '/test.raml', '/bar/:bar_id');

		$this->assertInstanceOf('PSX\Api\DocumentationInterface', $doc);
		$this->assertEquals('World Music API', $doc->getDescription());
		$this->assertEquals(2, $doc->getLatestVersion());

		$resource = $doc->getResource(2);

		$this->assertInstanceOf('PSX\Api\Resource', $resource);
		$this->assertEquals(array('GET'), $resource->getAllowedMethods());
		$this->assertEquals('Returns details about bar', $resource->getDescription());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getPathParameters());
		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $resource->getPathParameters()->getDefinition());
		$this->assertParameters($resource->getPathParameters());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getMethod('GET')->getResponse(200));

		$property = $resource->getMethod('GET')->getResponse(200)->getDefinition();

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $property);
		$this->assertEquals('A canonical song', $property->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('title'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('artist'));
	}

	public function testParseNested()
	{
		$doc = Raml::fromFile(__DIR__ . '/test.raml', '/foo/bar');

		$this->assertInstanceOf('PSX\Api\DocumentationInterface', $doc);
		$this->assertEquals('World Music API', $doc->getDescription());
		$this->assertEquals(2, $doc->getLatestVersion());

		$resource = $doc->getResource(2);

		$this->assertInstanceOf('PSX\Api\Resource', $resource);
		$this->assertEquals(array('GET'), $resource->getAllowedMethods());
		$this->assertEquals('Some description', $resource->getDescription());
	}

	public function testParseResponseWithoutSchema()
	{
		$doc      = Raml::fromFile(__DIR__ . '/test.raml', '/foo');
		$resource = $doc->getResource(2);

		$resource->getMethod('POST')->getResponse(500);<commit_msg>fix raml test add missing annotation<commit_after><?php
/*
 * PSX is a open source PHP framework to develop RESTful APIs.
 * For the current version and informations visit <http://phpsx.org>
 *
 * Copyright 2010-2015 <NAME> <<EMAIL>>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace PSX\Api\Documentation\Parser;

use PSX\Data\SchemaInterface;

/**
 * RamlTest
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @link    http://phpsx.org
 */
class RamlTest extends \PHPUnit_Framework_TestCase
{
	public function testParse()
	{
		$doc = Raml::fromFile(__DIR__ . '/test.raml', '/foo');

		$this->assertInstanceOf('PSX\Api\DocumentationInterface', $doc);
		$this->assertEquals('World Music API', $doc->getDescription());
		$this->assertEquals(2, $doc->getLatestVersion());

		$resource = $doc->getResource(2);

		$this->assertInstanceOf('PSX\Api\Resource', $resource);
		$this->assertEquals(array('GET', 'POST'), $resource->getAllowedMethods());
		$this->assertEquals('Bar', $resource->getTitle());
		$this->assertEquals('Some description', $resource->getDescription());

		// check GET
		$this->assertEquals('Informations about the method', $resource->getMethod('GET')->getDescription());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getMethod('GET')->getQueryParameters());
		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $resource->getMethod('GET')->getQueryParameters()->getDefinition());
		$this->assertInstanceOf('PSX\Data\Schema\Property\FloatType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('pages'));
		$this->assertEquals('The number of pages to return', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('pages')->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\IntegerType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_integer'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\FloatType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_number'));
		$this->assertEquals('The number', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_number')->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\DateTimeType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_date'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\BooleanType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_boolean'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $resource->getMethod('GET')->getQueryParameters()->getDefinition()->get('param_string'));
		$this->assertParameters($resource->getMethod('GET')->getQueryParameters());

		// check POST
		$this->assertInstanceOf('PSX\Api\Resource\Post', $resource->getMethod('POST'));

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $resource->getMethod('POST')->getQueryParameters()->getDefinition());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getMethod('POST')->getResponse(200));

		$property = $resource->getMethod('POST')->getRequest()->getDefinition();

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $property);
		$this->assertEquals('A canonical song', $property->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('title'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('artist'));

		$property = $resource->getMethod('POST')->getResponse(200)->getDefinition();

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $property);
		$this->assertEquals('A canonical song', $property->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('title'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('artist'));
	}

	public function testParsePath()
	{
		$doc = Raml::fromFile(__DIR__ . '/test.raml', '/bar/:bar_id');

		$this->assertInstanceOf('PSX\Api\DocumentationInterface', $doc);
		$this->assertEquals('World Music API', $doc->getDescription());
		$this->assertEquals(2, $doc->getLatestVersion());

		$resource = $doc->getResource(2);

		$this->assertInstanceOf('PSX\Api\Resource', $resource);
		$this->assertEquals(array('GET'), $resource->getAllowedMethods());
		$this->assertEquals('Returns details about bar', $resource->getDescription());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getPathParameters());
		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $resource->getPathParameters()->getDefinition());
		$this->assertParameters($resource->getPathParameters());

		$this->assertInstanceOf('PSX\Data\SchemaInterface', $resource->getMethod('GET')->getResponse(200));

		$property = $resource->getMethod('GET')->getResponse(200)->getDefinition();

		$this->assertInstanceOf('PSX\Data\Schema\Property\ComplexType', $property);
		$this->assertEquals('A canonical song', $property->getDescription());
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('title'));
		$this->assertInstanceOf('PSX\Data\Schema\Property\StringType', $property->get('artist'));
	}

	public function testParseNested()
	{
		$doc = Raml::fromFile(__DIR__ . '/test.raml', '/foo/bar');

		$this->assertInstanceOf('PSX\Api\DocumentationInterface', $doc);
		$this->assertEquals('World Music API', $doc->getDescription());
		$this->assertEquals(2, $doc->getLatestVersion());

		$resource = $doc->getResource(2);

		$this->assertInstanceOf('PSX\Api\Resource', $resource);
		$this->assertEquals(array('GET'), $resource->getAllowedMethods());
		$this->assertEquals('Some description', $resource->getDescription());
	}

	/**
	 * @expectedException RuntimeException
	 */
	public function testParseResponseWithoutSchema()
	{
		$doc      = Raml::fromFile(__DIR__ . '/test.raml', '/foo');
		$resource = $doc->getResource(2);

		$resource->getMethod('POST')->getResponse(500);
{'fraction_non_alphanumeric': 0.19187428133384438, 'fraction_numerical': 0.0055193560751245685, 'mean_word_length': 6.656103286384977, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 301, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7479710', 'n_tokens_mistral': 3926, 'n_tokens_neox': 3702, 'n_words': 586}