Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProfileTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class ProfileTest extends \PHPUnit\Framework\TestCase
14 {
18  private $connectionMock;
19 
23  private $dbContext;
24 
28  private $profileFactory;
29 
33  private $meta;
34 
38  private $profile;
39 
43  private $resource;
44 
48  protected $resourceMock;
49 
53  private $select;
54 
58  protected function setUp()
59  {
60  $this->connectionMock = $this->getMockForAbstractClass(
61  \Magento\Framework\DB\Adapter\AdapterInterface::class,
62  [],
63  '',
64  false,
65  false,
66  true,
67  ['query']
68  );
69  $this->dbContext = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
70  $this->profileFactory = $this->createPartialMock(
71  \Magento\SalesSequence\Model\ProfileFactory::class,
72  ['create']
73  );
74  $this->resourceMock = $this->createPartialMock(
75  \Magento\Framework\App\ResourceConnection::class,
76  ['getConnection', 'getTableName']
77  );
78  $this->dbContext->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
79  $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
80  $this->meta = $this->createMock(\Magento\SalesSequence\Model\Meta::class);
81  $this->profile = $this->createMock(\Magento\SalesSequence\Model\Profile::class);
82  $this->resource = new Profile(
83  $this->dbContext,
84  $this->profileFactory
85  );
86  }
87 
88  public function testLoadActiveProfile()
89  {
90  $profileTableName = 'sequence_profile';
91  $profileIdFieldName = 'profile_id';
92  $metaId = 1;
93  $profileId = 20;
94  $profileData = [
95  'profile_id' => 20,
96  'meta_id' => 1
97  ];
98  $this->profileFactory->expects($this->once())->method('create')->willReturn($this->profile);
99  $this->resourceMock->expects($this->any())
100  ->method('getConnection')
101  ->willReturn($this->connectionMock);
102  $this->resourceMock->expects($this->once())
103  ->method('getTableName')
104  ->willReturn($profileTableName);
105  $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select);
106  $this->select->expects($this->at(0))
107  ->method('from')
108  ->with($profileTableName, [$profileIdFieldName])
109  ->willReturn($this->select);
110  $this->select->expects($this->at(1))
111  ->method('where')
112  ->with('meta_id = :meta_id')
113  ->willReturn($this->select);
114  $this->select->expects($this->at(2))
115  ->method('where')
116  ->with('is_active = 1')
117  ->willReturn($this->select);
118  $this->connectionMock->expects($this->once())
119  ->method('fetchOne')
120  ->with($this->select, ['meta_id' => $metaId])
121  ->willReturn($profileId);
122  $this->select->expects($this->at(3))
123  ->method('from')
124  ->with($profileTableName, '*', null)
125  ->willReturn($this->select);
126  $this->connectionMock->expects($this->any())
127  ->method('quoteIdentifier');
128  $this->connectionMock->expects($this->once())->method('fetchRow')->willReturn($profileData);
129  $this->profile->expects($this->at(1))->method('setData')->with($profileData);
130  $this->assertEquals($this->profile, $this->resource->loadActiveProfile($metaId));
131  }
132 }