CreateCustomerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Customer;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Model\CustomerRegistry;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Magento\TestFramework\TestCase\GraphQlAbstract;
  12. class CreateCustomerTest extends GraphQlAbstract
  13. {
  14. /**
  15. * @var CustomerRegistry
  16. */
  17. private $customerRegistry;
  18. /**
  19. * @var CustomerRepositoryInterface
  20. */
  21. private $customerRepository;
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
  26. $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
  27. }
  28. /**
  29. * @throws \Exception
  30. */
  31. public function testCreateCustomerAccountWithPassword()
  32. {
  33. $newFirstname = 'Richard';
  34. $newLastname = 'Rowe';
  35. $currentPassword = 'test123#';
  36. $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
  37. $query = <<<QUERY
  38. mutation {
  39. createCustomer(
  40. input: {
  41. firstname: "{$newFirstname}"
  42. lastname: "{$newLastname}"
  43. email: "{$newEmail}"
  44. password: "{$currentPassword}"
  45. is_subscribed: true
  46. }
  47. ) {
  48. customer {
  49. id
  50. firstname
  51. lastname
  52. email
  53. is_subscribed
  54. }
  55. }
  56. }
  57. QUERY;
  58. $response = $this->graphQlQuery($query);
  59. $this->assertEquals($newFirstname, $response['createCustomer']['customer']['firstname']);
  60. $this->assertEquals($newLastname, $response['createCustomer']['customer']['lastname']);
  61. $this->assertEquals($newEmail, $response['createCustomer']['customer']['email']);
  62. $this->assertEquals(true, $response['createCustomer']['customer']['is_subscribed']);
  63. }
  64. /**
  65. * @throws \Exception
  66. */
  67. public function testCreateCustomerAccountWithoutPassword()
  68. {
  69. $newFirstname = 'Richard';
  70. $newLastname = 'Rowe';
  71. $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
  72. $query = <<<QUERY
  73. mutation {
  74. createCustomer(
  75. input: {
  76. firstname: "{$newFirstname}"
  77. lastname: "{$newLastname}"
  78. email: "{$newEmail}"
  79. is_subscribed: true
  80. }
  81. ) {
  82. customer {
  83. id
  84. firstname
  85. lastname
  86. email
  87. is_subscribed
  88. }
  89. }
  90. }
  91. QUERY;
  92. $response = $this->graphQlQuery($query);
  93. $this->assertEquals($newFirstname, $response['createCustomer']['customer']['firstname']);
  94. $this->assertEquals($newLastname, $response['createCustomer']['customer']['lastname']);
  95. $this->assertEquals($newEmail, $response['createCustomer']['customer']['email']);
  96. $this->assertEquals(true, $response['createCustomer']['customer']['is_subscribed']);
  97. }
  98. /**
  99. * @expectedException \Exception
  100. * @expectedExceptionMessage "input" value should be specified
  101. */
  102. public function testCreateCustomerIfInputDataIsEmpty()
  103. {
  104. $query = <<<QUERY
  105. mutation {
  106. createCustomer(
  107. input: {
  108. }
  109. ) {
  110. customer {
  111. id
  112. firstname
  113. lastname
  114. email
  115. is_subscribed
  116. }
  117. }
  118. }
  119. QUERY;
  120. $this->graphQlQuery($query);
  121. }
  122. /**
  123. * @expectedException \Exception
  124. * @expectedExceptionMessage The customer email is missing. Enter and try again.
  125. */
  126. public function testCreateCustomerIfEmailMissed()
  127. {
  128. $newFirstname = 'Richard';
  129. $newLastname = 'Rowe';
  130. $currentPassword = 'test123#';
  131. $query = <<<QUERY
  132. mutation {
  133. createCustomer(
  134. input: {
  135. firstname: "{$newFirstname}"
  136. lastname: "{$newLastname}"
  137. password: "{$currentPassword}"
  138. is_subscribed: true
  139. }
  140. ) {
  141. customer {
  142. id
  143. firstname
  144. lastname
  145. email
  146. is_subscribed
  147. }
  148. }
  149. }
  150. QUERY;
  151. $this->graphQlQuery($query);
  152. }
  153. /**
  154. * @expectedException \Exception
  155. * @expectedExceptionMessage "Email" is not a valid email address.
  156. */
  157. public function testCreateCustomerIfEmailIsNotValid()
  158. {
  159. $newFirstname = 'Richard';
  160. $newLastname = 'Rowe';
  161. $currentPassword = 'test123#';
  162. $newEmail = 'email';
  163. $query = <<<QUERY
  164. mutation {
  165. createCustomer(
  166. input: {
  167. firstname: "{$newFirstname}"
  168. lastname: "{$newLastname}"
  169. email: "{$newEmail}"
  170. password: "{$currentPassword}"
  171. is_subscribed: true
  172. }
  173. ) {
  174. customer {
  175. id
  176. firstname
  177. lastname
  178. email
  179. is_subscribed
  180. }
  181. }
  182. }
  183. QUERY;
  184. $this->graphQlQuery($query);
  185. }
  186. /**
  187. * @expectedException \Exception
  188. * @expectedExceptionMessage Field "test123" is not defined by type CustomerInput.
  189. */
  190. public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput()
  191. {
  192. $newFirstname = 'Richard';
  193. $newLastname = 'Rowe';
  194. $currentPassword = 'test123#';
  195. $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
  196. $query = <<<QUERY
  197. mutation {
  198. createCustomer(
  199. input: {
  200. firstname: "{$newFirstname}"
  201. lastname: "{$newLastname}"
  202. test123: "123test123"
  203. email: "{$newEmail}"
  204. password: "{$currentPassword}"
  205. is_subscribed: true
  206. }
  207. ) {
  208. customer {
  209. id
  210. firstname
  211. lastname
  212. email
  213. is_subscribed
  214. }
  215. }
  216. }
  217. QUERY;
  218. $this->graphQlQuery($query);
  219. }
  220. }