You are here

public function ServicesRESTServerTests::testGetControllerArgumentsFromSources in Services 7.3

Test for getControllerArgumentsFromSources() method.

File

servers/rest_server/tests/ServicesRESTServerTests.test, line 270

Class

ServicesRESTServerTests
Unit tests for RESTServer class.

Code

public function testGetControllerArgumentsFromSources() {
  $test_resources = $this
    ->getTestResource();
  $factory_args = $this
    ->getDefaultFactoryArguments();
  $rest_server = $this
    ->getRESTSever($factory_args);
  $retrieve_controller = $test_resources['operations']['retrieve'];
  $sources = array(
    'path' => array(
      '1',
    ),
    'param' => array(),
    'data' => array(),
    'headers' => array(),
  );

  // Arguments from path.
  $arguments = $rest_server
    ->protectedGetControllerArgumentsFromSources($retrieve_controller, $sources);
  $this
    ->assertEqual($arguments, array(
    0 => 1,
  ), 'Path argument retrieved.');

  //    $sources['path'][0] = $this->randomName();
  //    $arguments = $rest_server->protectedGetControllerArgumentsFromSources($retrieve_controller, $sources);
  //    $this->assertEqual($arguments, array(0 => 0), 'Path argument retrieved and converted to integer.');
  // Arguments from 'data'
  $create_controller = $test_resources['operations']['create'];
  $sources['data'] = array(
    1,
    2,
  );
  $arguments = $rest_server
    ->protectedGetControllerArgumentsFromSources($create_controller, $sources);
  $this
    ->assertEqual($arguments, array(
    $sources['data'],
  ), 'Data argument that is array retrieved.');
  $sources['data'] = $this
    ->randomName();
  $arguments = $rest_server
    ->protectedGetControllerArgumentsFromSources($create_controller, $sources);
  $this
    ->assertEqual($arguments, array(
    array(
      $sources['data'],
    ),
  ), 'Data argument that is string retrieved and converted to array.');
  unset($create_controller['args'][0]['type']);
  $arguments = $rest_server
    ->protectedGetControllerArgumentsFromSources($create_controller, $sources);
  $this
    ->assertEqual($arguments, array(
    $sources['data'],
  ), 'Data argument that is string retrieved and converted to array.');

  // Arguments from 'get'
  $create_controller['args'][0]['source'] = array(
    'get' => 'node',
  );
  $sources['data'] = array();
  $sources['get'] = array(
    'node' => $this
      ->randomString(),
  );
  $arguments = $rest_server
    ->protectedGetControllerArgumentsFromSources($create_controller, $sources);
  $this
    ->assertEqual($arguments, array(
    $sources['get']['node'],
  ), 'Get argument retrieved.');

  // Arguments combination.
  $sources['data'] = array(
    'comment' => $this
      ->randomString(),
  );
  $create_controller['args'][] = array(
    'name' => 'comment',
    'optional' => FALSE,
    'source' => array(
      'data' => 'comment',
    ),
    'type' => 'string',
  );
  $arguments = $rest_server
    ->protectedGetControllerArgumentsFromSources($create_controller, $sources);
  $this
    ->assertEqual($arguments, array(
    $sources['get']['node'],
    $sources['data']['comment'],
  ), 'Get and Data arguments retrieved.');

  // Required argument.
  $sources['get'] = array();
  try {
    $rest_server
      ->protectedGetControllerArgumentsFromSources($create_controller, $sources);
    $this
      ->assert(FALSE, 'Missing required argument no error thrown.');
  } catch (Exception $e) {
    $this
      ->assert(TRUE, 'Missing required argument error thrown.');
  }
}