CustomerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Search;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * @magentoAppArea adminhtml
  10. * @magentoDataFixture Magento/Customer/_files/three_customers.php
  11. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  12. */
  13. class CustomerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @dataProvider loadDataProvider
  17. */
  18. public function testLoad($query, $limit, $start, $expectedResult)
  19. {
  20. /** Preconditions */
  21. $objectManager = Bootstrap::getObjectManager();
  22. /** @var \Magento\Backend\Model\Search\Customer $customerSearch */
  23. $customerSearch = $objectManager->create(\Magento\Backend\Model\Search\Customer::class);
  24. $customerSearch->setQuery($query);
  25. $customerSearch->setLimit($limit);
  26. $customerSearch->setStart($start);
  27. $customerSearch->load();
  28. /** SUT Execution */
  29. $searchResults = $customerSearch->getResults();
  30. /** Ensure that search results are correct */
  31. $this->assertCount(count($expectedResult), $searchResults, 'Quantity of search result items is invalid.');
  32. foreach ($expectedResult as $itemIndex => $expectedItem) {
  33. /** Validate URL to item */
  34. $customerId = substr($expectedItem['id'], 11); // 'customer/1/' is added to all actual customer IDs
  35. $this->assertContains(
  36. "customer/index/edit/id/$customerId",
  37. $searchResults[$itemIndex]['url'],
  38. 'Item URL is invalid.'
  39. );
  40. unset($searchResults[$itemIndex]['url']);
  41. /** Validate other item data */
  42. $this->assertEquals($expectedItem, $searchResults[$itemIndex], "Data of item #$itemIndex is invalid.");
  43. }
  44. }
  45. public static function loadDataProvider()
  46. {
  47. return [
  48. 'All items, first page' => [
  49. 'Firstname',
  50. 2, // Items on page
  51. 1, // Page number
  52. [
  53. [
  54. 'id' => 'customer/1/1',
  55. 'type' => 'Customer',
  56. 'name' => 'Firstname Lastname',
  57. 'description' => 'CompanyName',
  58. ],
  59. [
  60. 'id' => 'customer/1/2',
  61. 'type' => 'Customer',
  62. 'name' => 'Firstname2 Lastname2',
  63. 'description' => null
  64. ]
  65. ],
  66. ],
  67. 'All items, second page' => [
  68. 'Firstname',
  69. 2, // Items on page
  70. 2, // Page number
  71. [
  72. [
  73. 'id' => 'customer/1/3',
  74. 'type' => 'Customer',
  75. 'name' => 'Firstname3 Lastname3',
  76. 'description' => null,
  77. ]
  78. ],
  79. ],
  80. 'Search by last name, second item only' => [
  81. 'Lastname2',
  82. 10, // Items on page
  83. 1, // Page number
  84. [
  85. [
  86. 'id' => 'customer/1/2',
  87. 'type' => 'Customer',
  88. 'name' => 'Firstname2 Lastname2',
  89. 'description' => null,
  90. ]
  91. ],
  92. ],
  93. 'No results' => [
  94. 'NotExistingCustomerName',
  95. 10, // Items on page
  96. 1, // Page number
  97. [],
  98. ],
  99. 'Search by company name, first item only' => [
  100. 'CompanyName',
  101. 10, // Items on page
  102. 1, // Page number
  103. [
  104. [
  105. 'id' => 'customer/1/1',
  106. 'type' => 'Customer',
  107. 'name' => 'Firstname Lastname',
  108. 'description' => 'CompanyName',
  109. ],
  110. ],
  111. ],
  112. ];
  113. }
  114. }