-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathFileTest.php
152 lines (133 loc) · 4.09 KB
/
FileTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Framework\DB\Test\Unit\Logger;
use Magento\Framework\DB\Logger\File;
use Magento\Framework\DB\LoggerInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\File\WriteInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class FileTest extends TestCase
{
const DEBUG_FILE = 'debug.file.log';
/**
* @var WriteInterface|MockObject
*/
private $stream;
/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface|MockObject
*/
private $dir;
/**
* @var File
*/
private $object;
protected function setUp(): void
{
$this->stream = $this->getMockForAbstractClass(WriteInterface::class);
$this->dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
$this->dir->expects($this->any())
->method('openFile')
->with(self::DEBUG_FILE, 'a')
->willReturn($this->stream);
$filesystem = $this->createMock(Filesystem::class);
$filesystem->expects($this->any())
->method('getDirectoryWrite')
->willReturn($this->dir);
$this->object = new File(
$filesystem,
self::DEBUG_FILE
);
}
public function testLog()
{
$input = 'message';
$expected = '%amessage';
$this->stream->expects($this->once())
->method('write')
->with($this->matches($expected));
$this->object->log($input);
}
/**
* @param $type
*
* @param string $q
* @param array $bind
* @param \Zend_Db_Statement_Pdo|null $result
* @param string $expected
* @dataProvider logStatsDataProvider
*/
public function testLogStats($type, $q, array $bind, $result, $expected)
{
$this->stream->expects($this->once())
->method('write')
->with($this->matches($expected));
$this->object->logStats($type, $q, $bind, $result);
}
/**
* @return array
*/
public function logStatsDataProvider()
{
return [
[LoggerInterface::TYPE_CONNECT, '', [], null, '%aCONNECT%a'],
[
LoggerInterface::TYPE_TRANSACTION,
'SELECT something',
[],
null,
'%aTRANSACTION SELECT something%a'
],
[
LoggerInterface::TYPE_QUERY,
'SELECT something',
[],
null,
'%aSQL: SELECT something%a'
],
[
LoggerInterface::TYPE_QUERY,
'SELECT something',
['data'],
null,
"%aQUERY%aSQL: SELECT something%aBIND: array (%a0 => 'data',%a)%a"
],
];
}
public function testLogStatsWithResult()
{
$result = $this->createMock(\Zend_Db_Statement_Pdo::class);
$result->expects($this->once())
->method('rowCount')
->willReturn(10);
$this->stream->expects($this->once())
->method('write')
->with($this->logicalNot($this->matches('%aSQL: SELECT something%aAFF: 10')));
$this->object->logStats(
LoggerInterface::TYPE_QUERY,
'SELECT something',
[],
$result
);
}
public function testLogStatsUnknownType()
{
$this->stream->expects($this->once())
->method('write')
->with($this->logicalNot($this->matches('%aSELECT something%a')));
$this->object->logStats('unknown', 'SELECT something');
}
public function testcritical()
{
$exception = new \Exception('error message');
$expected = "%aEXCEPTION%aException%aerror message%a";
$this->stream->expects($this->once())
->method('write')
->with($this->matches($expected));
$this->object->critical($exception);
}
}