123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\GraphQl\Customer;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- use Magento\Customer\Model\CustomerRegistry;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\TestFramework\TestCase\GraphQlAbstract;
- class CreateCustomerTest extends GraphQlAbstract
- {
- /**
- * @var CustomerRegistry
- */
- private $customerRegistry;
- /**
- * @var CustomerRepositoryInterface
- */
- private $customerRepository;
- protected function setUp()
- {
- parent::setUp();
- $this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
- $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
- }
- /**
- * @throws \Exception
- */
- public function testCreateCustomerAccountWithPassword()
- {
- $newFirstname = 'Richard';
- $newLastname = 'Rowe';
- $currentPassword = 'test123#';
- $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
- $query = <<<QUERY
- mutation {
- createCustomer(
- input: {
- firstname: "{$newFirstname}"
- lastname: "{$newLastname}"
- email: "{$newEmail}"
- password: "{$currentPassword}"
- is_subscribed: true
- }
- ) {
- customer {
- id
- firstname
- lastname
- email
- is_subscribed
- }
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertEquals($newFirstname, $response['createCustomer']['customer']['firstname']);
- $this->assertEquals($newLastname, $response['createCustomer']['customer']['lastname']);
- $this->assertEquals($newEmail, $response['createCustomer']['customer']['email']);
- $this->assertEquals(true, $response['createCustomer']['customer']['is_subscribed']);
- }
- /**
- * @throws \Exception
- */
- public function testCreateCustomerAccountWithoutPassword()
- {
- $newFirstname = 'Richard';
- $newLastname = 'Rowe';
- $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
- $query = <<<QUERY
- mutation {
- createCustomer(
- input: {
- firstname: "{$newFirstname}"
- lastname: "{$newLastname}"
- email: "{$newEmail}"
- is_subscribed: true
- }
- ) {
- customer {
- id
- firstname
- lastname
- email
- is_subscribed
- }
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertEquals($newFirstname, $response['createCustomer']['customer']['firstname']);
- $this->assertEquals($newLastname, $response['createCustomer']['customer']['lastname']);
- $this->assertEquals($newEmail, $response['createCustomer']['customer']['email']);
- $this->assertEquals(true, $response['createCustomer']['customer']['is_subscribed']);
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage "input" value should be specified
- */
- public function testCreateCustomerIfInputDataIsEmpty()
- {
- $query = <<<QUERY
- mutation {
- createCustomer(
- input: {
-
- }
- ) {
- customer {
- id
- firstname
- lastname
- email
- is_subscribed
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query);
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage The customer email is missing. Enter and try again.
- */
- public function testCreateCustomerIfEmailMissed()
- {
- $newFirstname = 'Richard';
- $newLastname = 'Rowe';
- $currentPassword = 'test123#';
- $query = <<<QUERY
- mutation {
- createCustomer(
- input: {
- firstname: "{$newFirstname}"
- lastname: "{$newLastname}"
- password: "{$currentPassword}"
- is_subscribed: true
- }
- ) {
- customer {
- id
- firstname
- lastname
- email
- is_subscribed
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query);
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage "Email" is not a valid email address.
- */
- public function testCreateCustomerIfEmailIsNotValid()
- {
- $newFirstname = 'Richard';
- $newLastname = 'Rowe';
- $currentPassword = 'test123#';
- $newEmail = 'email';
- $query = <<<QUERY
- mutation {
- createCustomer(
- input: {
- firstname: "{$newFirstname}"
- lastname: "{$newLastname}"
- email: "{$newEmail}"
- password: "{$currentPassword}"
- is_subscribed: true
- }
- ) {
- customer {
- id
- firstname
- lastname
- email
- is_subscribed
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query);
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage Field "test123" is not defined by type CustomerInput.
- */
- public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput()
- {
- $newFirstname = 'Richard';
- $newLastname = 'Rowe';
- $currentPassword = 'test123#';
- $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
- $query = <<<QUERY
- mutation {
- createCustomer(
- input: {
- firstname: "{$newFirstname}"
- lastname: "{$newLastname}"
- test123: "123test123"
- email: "{$newEmail}"
- password: "{$currentPassword}"
- is_subscribed: true
- }
- ) {
- customer {
- id
- firstname
- lastname
- email
- is_subscribed
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query);
- }
- }
|