Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BookmarkManagementTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class BookmarkManagementTest extends \PHPUnit\Framework\TestCase
16 {
21 
26 
30  protected $filterBuilder;
31 
36 
40  protected $userContext;
41 
42  protected function setUp()
43  {
44  $this->bookmarkRepository = $this->getMockBuilder(\Magento\Ui\Api\BookmarkRepositoryInterface::class)
45  ->getMockForAbstractClass();
46  $this->filterBuilder = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
47  ->disableOriginalConstructor()
48  ->setMethods(['create'])
49  ->getMock();
50  $this->searchCriteriaBuilder =$this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->userContext = $this->getMockBuilder(\Magento\Authorization\Model\UserContextInterface::class)
54  ->getMockForAbstractClass();
55  $this->bookmarkManagement = new BookmarkManagement(
56  $this->bookmarkRepository,
57  $this->filterBuilder,
58  $this->searchCriteriaBuilder,
59  $this->userContext
60  );
61  }
62 
63  public function testLoadByNamespace()
64  {
65  $userId = 1;
66  $namespace = 'some_namespace';
67  $this->userContext->expects($this->once())
68  ->method('getUserId')
69  ->willReturn($userId);
70  $fieldUserId = new Filter(
71  [
72  Filter::KEY_FIELD => 'user_id',
73  Filter::KEY_VALUE => $userId,
75  ]
76  );
77  $fieldNamespace = new Filter(
78  [
79  Filter::KEY_FIELD => 'namespace',
80  Filter::KEY_VALUE => $namespace,
82  ]
83  );
84  $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
85  ->getMockForAbstractClass();
86  $this->filterBuilder->expects($this->at(0))
87  ->method('create')
88  ->willReturn($fieldUserId);
89  $this->filterBuilder->expects($this->at(1))
90  ->method('create')
91  ->willReturn($fieldNamespace);
92  $this->searchCriteriaBuilder->expects($this->exactly(2))
93  ->method('addFilters')
94  ->withConsecutive([[$fieldUserId]], [[$fieldNamespace]]);
95  $this->searchCriteriaBuilder->expects($this->once())
96  ->method('create')
97  ->willReturn($searchCriteria);
98  $searchResult = $this->getMockBuilder(\Magento\Ui\Api\Data\BookmarkSearchResultsInterface::class)
99  ->getMockForAbstractClass();
100  $this->bookmarkRepository->expects($this->once())
101  ->method('getList')
102  ->with($searchCriteria)
103  ->willReturn($searchResult);
104  $this->assertEquals($searchResult, $this->bookmarkManagement->loadByNamespace($namespace));
105  }
106 
108  {
109  $userId = 1;
110  $namespace = 'some_namespace';
111  $identifier ='current';
112  $this->userContext->expects($this->once())
113  ->method('getUserId')
114  ->willReturn($userId);
115  $fieldUserId = new Filter(
116  [
117  Filter::KEY_FIELD => 'user_id',
118  Filter::KEY_VALUE => $userId,
120  ]
121  );
122  $fieldIdentifier = new Filter(
123  [
124  Filter::KEY_FIELD => 'identifier',
125  Filter::KEY_VALUE => $identifier,
127  ]
128  );
129  $fieldNamespace = new Filter(
130  [
131  Filter::KEY_FIELD => 'namespace',
132  Filter::KEY_VALUE => $namespace,
134  ]
135  );
136  $bookmarkId = 1;
137  $bookmark = $this->getMockBuilder(\Magento\Ui\Api\Data\BookmarkInterface::class)->getMockForAbstractClass();
138  $bookmark->expects($this->once())->method('getId')->willReturn($bookmarkId);
139  $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
140  ->getMockForAbstractClass();
141  $this->filterBuilder->expects($this->at(0))
142  ->method('create')
143  ->willReturn($fieldUserId);
144  $this->filterBuilder->expects($this->at(1))
145  ->method('create')
146  ->willReturn($fieldIdentifier);
147  $this->filterBuilder->expects($this->at(2))
148  ->method('create')
149  ->willReturn($fieldNamespace);
150  $this->searchCriteriaBuilder->expects($this->exactly(3))
151  ->method('addFilters')
152  ->withConsecutive([[$fieldUserId]], [[$fieldIdentifier]], [[$fieldNamespace]]);
153  $this->searchCriteriaBuilder->expects($this->once())
154  ->method('create')
155  ->willReturn($searchCriteria);
156  $searchResult = $this->getMockBuilder(\Magento\Ui\Api\Data\BookmarkSearchResultsInterface::class)
157  ->getMockForAbstractClass();
158  $searchResult->expects($this->once())->method('getTotalCount')->willReturn(1);
159  $searchResult->expects($this->once())->method('getItems')->willReturn([$bookmark]);
160  $this->bookmarkRepository->expects($this->once())
161  ->method('getList')
162  ->with($searchCriteria)
163  ->willReturn($searchResult);
164  $this->bookmarkRepository->expects($this->once())
165  ->method('getById')
166  ->with($bookmarkId)
167  ->willReturn($bookmark);
168  $this->assertEquals(
169  $bookmark,
170  $this->bookmarkManagement->getByIdentifierNamespace($identifier, $namespace)
171  );
172  }
173 }
$searchCriteria