public function testCreateCustomerWithErrors() { $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH, 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => self::SERVICE_NAME . 'CreateAccount']]; $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray($this->customerHelper->createSampleCustomerDataObject(), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $invalidEmail = 'invalid'; $customerDataArray['email'] = $invalidEmail; $requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD]; try { $this->_webApiCall($serviceInfo, $requestData); $this->fail('Expected exception did not occur.'); } catch (\Exception $e) { if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { $expectedException = new InputException(); $expectedException->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'email', 'value' => $invalidEmail])); $this->assertInstanceOf('SoapFault', $e); $this->checkSoapFault($e, $expectedException->getRawMessage(), 'env:Sender', $expectedException->getParameters()); } else { $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode()); $exceptionData = $this->processRestExceptionResult($e); $expectedExceptionData = ['message' => InputException::INVALID_FIELD_VALUE, 'parameters' => ['fieldName' => 'email', 'value' => $invalidEmail]]; $this->assertEquals($expectedExceptionData, $exceptionData); } } }
/** * Test creating a customer with absent required address fields */ public function testCreateCustomerWithoutAddressRequiresException() { $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray($this->customerHelper->createSampleCustomerDataObject(), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); foreach ($customerDataArray[Customer::KEY_ADDRESSES] as &$address) { $address[Address::FIRSTNAME] = null; } $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH, 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => self::SERVICE_NAME . 'Save']]; $requestData = ['customer' => $customerDataArray]; try { $this->_webApiCall($serviceInfo, $requestData); $this->fail('Expected exception did not occur.'); } catch (\Exception $e) { if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { $expectedException = new InputException(); $expectedException->addError(__('%fieldName is a required field.', ['fieldName' => Address::FIRSTNAME])); $this->assertInstanceOf('SoapFault', $e); $this->checkSoapFault($e, $expectedException->getRawMessage(), 'env:Sender', $expectedException->getParameters()); } else { $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode()); $exceptionData = $this->processRestExceptionResult($e); $expectedExceptionData = ['message' => '%fieldName is a required field.', 'parameters' => ['fieldName' => Address::FIRSTNAME]]; $this->assertEquals($expectedExceptionData, $exceptionData); } } try { $this->customerRegistry->retrieveByEmail($customerDataArray[Customer::EMAIL], $customerDataArray[Customer::WEBSITE_ID]); $this->fail('An expected NoSuchEntityException was not thrown.'); } catch (NoSuchEntityException $e) { $exception = NoSuchEntityException::doubleField('email', $customerDataArray[Customer::EMAIL], 'websiteId', $customerDataArray[Customer::WEBSITE_ID]); $this->assertEquals($exception->getMessage(), $e->getMessage(), 'Exception message does not match expected message.'); } }