/** * @param array $accounts * @param string $account * @param int $serial * @param string $code * * @dataProvider provideIdentCodeData */ public function testIdentCode(array $accounts, $account, $serial, $code) { $identCode = new IdentCode(); $identCode->setAccounts($accounts); $identCode->setSerial($serial); $this->assertEquals($code, $identCode->get($account)); }
/** * Returns an ident code of the given type (see config) * * @Method ("GET") * @Route ( * "/identcode/{account}/{id}.{_format}", * name="wk_dhl_api_b2b_ident_code", * requirements={ * "id"="\d+", * "account"="\w+", * "_format"="(xml|json)" * }, * defaults={ * "_format"="json" * } * ) * @param Request $request * @return Response */ public function getIdentCodeAction(Request $request) { try { $serial = intval($request->attributes->get('id')); $account = strval($request->attributes->get('account')); $identCode = $this->get('wk_dhl_api.b2b.ident_code'); $identCode->setSerial($serial); $code = $identCode->get($account); $response = new IdentCodeResponse($code, IdentCode::format($code)); return $this->generateResponse($response); } catch (BadRequestHttpException $exception) { return $this->generateError($exception->getMessage(), $exception->getCode(), 400); } catch (\InvalidArgumentException $exception) { return $this->generateError($exception->getMessage(), 1001, 400); } catch (\Exception $exception) { return $this->generateError($exception->getMessage(), 1000); } }
/** * Data provider to provide data to test getting ident codes via the controller * * @return array */ public function provideIdentCodeData() { $dataSet = array(); // Start with a test with a likely not configured account name $errorAccount = 'alikelyneverusedkey'; $errorResponse = new ErrorResponse(1001, "Account '{$errorAccount}' is not configured"); $dataSet[] = array(400, $errorAccount, 99999, $errorResponse); // Loop and test all configured accounts $accounts = $this->createClient()->getKernel()->getContainer()->getParameter('wk_dhl_api.b2b.accounts'); foreach ($accounts as $key => $account) { // Add items to data set with valid data $code = $account . '0099999'; $code .= IdentCode::getParityNumber($code); $dataSet[] = array(200, $key, 99999, new IdentCodeResponse($code, IdentCode::format($code))); $code = $account . '0000815'; $code .= IdentCode::getParityNumber($code); $dataSet[] = array(200, $key, 815, new IdentCodeResponse($code, IdentCode::format($code))); $code = $account . '0000003'; $code .= IdentCode::getParityNumber($code); $dataSet[] = array(200, $key, 3, new IdentCodeResponse($code, IdentCode::format($code))); // add data sets with invalid parameters $errorResponse = new ErrorResponse(1001, 'Serial number contains more than 5 digits. Start with 1 again or use modulus 100000'); $dataSet[] = array(400, $key, 999999, $errorResponse); $errorResponse = new ErrorResponse(1001, 'Serial number is no unsigned integer'); $dataSet[] = array(400, $key, 0, $errorResponse); } return $dataSet; }