]> git.mxchange.org Git - friendica.git/blob - tests/Util/DBAMockTrait.php
Replace misuses ItemDeliveryData::FIELD_LIST with LEGACY_FIELD_LIST
[friendica.git] / tests / Util / DBAMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use Mockery\MockInterface;
6
7 /**
8  * Trait to mock the DBA connection status
9  */
10 trait DBAMockTrait
11 {
12         /**
13          * @var MockInterface The mocking interface of Friendica\Database\DBA
14          */
15         private $dbaMock;
16
17         /**
18          * Mocking DBA::connect()
19          *
20          * @param bool $return True, if the connect was successful, otherwise false
21          * @param null|int $times How often the method will get used
22          */
23         public function mockConnect($return = true, $times = null)
24         {
25                 if (!isset($this->dbaMock)) {
26                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
27                 }
28
29                 $this->dbaMock
30                         ->shouldReceive('connect')
31                         ->times($times)
32                         ->andReturn($return);
33         }
34
35         /**
36          * Mocking DBA::connected()
37          *
38          * @param bool $return True, if the DB is connected, otherwise false
39          * @param null|int $times How often the method will get used
40          */
41         public function mockConnected($return = true, $times = null)
42         {
43                 if (!isset($this->dbaMock)) {
44                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
45                 }
46
47                 $this->dbaMock
48                         ->shouldReceive('connected')
49                         ->times($times)
50                         ->andReturn($return);
51         }
52
53         /**
54          * Mocking DBA::fetchFirst()
55          *
56          * @param string $arg The argument of fetchFirst
57          * @param bool $return True, if the DB is connected, otherwise false
58          * @param null|int $times How often the method will get used
59          */
60         public function mockFetchFirst($arg, $return = true, $times = null)
61         {
62                 if (!isset($this->dbaMock)) {
63                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
64                 }
65
66                 $this->dbaMock
67                         ->shouldReceive('fetchFirst')
68                         ->with($arg)
69                         ->times($times)
70                         ->andReturn($return);
71         }
72
73
74         /**
75          * Mocking DBA::select()
76          *
77          * @param string $tableName The name of the table
78          * @param array $select The Select Array (Default is [])
79          * @param array $where The Where Array (Default is [])
80          * @param object $return The array to return (Default is [])
81          * @param null|int $times How often the method will get used
82          */
83         public function mockSelect($tableName, $select = [], $where = [], $return = null, $times = null)
84         {
85                 if (!isset($this->dbaMock)) {
86                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
87                 }
88
89                 $this->dbaMock
90                         ->shouldReceive('select')
91                         ->with($tableName, $select, $where)
92                         ->times($times)
93                         ->andReturn($return);
94         }
95
96         /**
97          * Mocking DBA::selectFirst()
98          *
99          * @param string $tableName The name of the table
100          * @param array $select The Select Array (Default is [])
101          * @param array $where The Where Array (Default is [])
102          * @param array $return The array to return (Default is [])
103          * @param null|int $times How often the method will get used
104          */
105         public function mockSelectFirst($tableName, $select = [], $where = [], $return = [], $times = null)
106         {
107                 if (!isset($this->dbaMock)) {
108                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
109                 }
110
111                 $this->dbaMock
112                         ->shouldReceive('selectFirst')
113                         ->with($tableName, $select, $where)
114                         ->times($times)
115                         ->andReturn($return);
116         }
117
118         /**
119          * Mocking DBA::isResult()
120          *
121          * @param object $record The record to test
122          * @param bool $return True, if the DB is connected, otherwise false
123          * @param null|int $times How often the method will get used
124          */
125         public function mockIsResult($record, $return = true, $times = null)
126         {
127                 if (!isset($this->dbaMock)) {
128                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
129                 }
130
131                 $this->dbaMock
132                         ->shouldReceive('isResult')
133                         ->with($record)
134                         ->times($times)
135                         ->andReturn($return);
136         }
137
138         /**
139          * Mocking DBA::isResult()
140          *
141          * @param object $record The record to test
142          * @param array $return The array to return
143          * @param null|int $times How often the method will get used
144          */
145         public function mockToArray($record = null, $return = [], $times = null)
146         {
147                 if (!isset($this->dbaMock)) {
148                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
149                 }
150
151                 $this->dbaMock
152                         ->shouldReceive('toArray')
153                         ->with($record)
154                         ->times($times)
155                         ->andReturn($return);
156         }
157
158
159         /**
160          * Mocking DBA::p()
161          *
162          * @param string $sql The SQL statement
163          * @param object $return The object to return
164          * @param null|int $times How often the method will get used
165          */
166         public function mockP($sql = null, $return = null, $times = null)
167         {
168                 if (!isset($this->dbaMock)) {
169                         $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
170                 }
171
172                 if (!isset($sql)) {
173                         $this->dbaMock
174                                 ->shouldReceive('p')
175                                 ->times($times)
176                                 ->andReturn($return);
177                 } else {
178                         $this->dbaMock
179                                 ->shouldReceive('p')
180                                 ->with($sql)
181                                 ->times($times)
182                                 ->andReturn($return);
183                 }
184         }
185 }