Skip to content

Commit f0f977a

Browse files
Fixed PHPUnit warnings (#32212)
1 parent 70249ef commit f0f977a

File tree

5 files changed

+97
-15
lines changed

5 files changed

+97
-15
lines changed

src/Illuminate/Foundation/Testing/Assert.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use ArrayAccess;
66
use Illuminate\Foundation\Testing\Constraints\ArraySubset;
77
use PHPUnit\Framework\Assert as PHPUnit;
8+
use PHPUnit\Framework\Constraint\DirectoryExists;
9+
use PHPUnit\Framework\Constraint\FileExists;
10+
use PHPUnit\Framework\Constraint\LogicalNot;
11+
use PHPUnit\Framework\Constraint\RegularExpression;
812
use PHPUnit\Framework\InvalidArgumentException;
913
use PHPUnit\Runner\Version;
1014
use PHPUnit\Util\InvalidArgumentHelper;
@@ -46,6 +50,43 @@ public static function assertArraySubset($subset, $array, bool $checkForIdentity
4650

4751
PHPUnit::assertThat($array, $constraint, $msg);
4852
}
53+
54+
/**
55+
* Asserts that a file does not exist.
56+
*
57+
* @param string $filename
58+
* @param string $message
59+
* @return void
60+
*/
61+
public static function assertFileDoesNotExist(string $filename, string $message = ''): void
62+
{
63+
static::assertThat($filename, new LogicalNot(new FileExists), $message);
64+
}
65+
66+
/**
67+
* Asserts that a directory does not exist.
68+
*
69+
* @param string $directory
70+
* @param string $message
71+
* @return void
72+
*/
73+
public static function assertDirectoryDoesNotExist(string $directory, string $message = ''): void
74+
{
75+
static::assertThat($directory, new LogicalNot(new DirectoryExists), $message);
76+
}
77+
78+
/**
79+
* Asserts that a string matches a given regular expression.
80+
*
81+
* @param string $pattern
82+
* @param string $string
83+
* @param string $message
84+
* @return void
85+
*/
86+
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
87+
{
88+
static::assertThat($string, new RegularExpression($pattern), $message);
89+
}
4990
}
5091
} else {
5192
/**
@@ -66,5 +107,42 @@ public static function assertArraySubset($subset, $array, bool $checkForIdentity
66107
{
67108
PHPUnit::assertArraySubset($subset, $array, $checkForIdentity, $msg);
68109
}
110+
111+
/**
112+
* Asserts that a file does not exist.
113+
*
114+
* @param string $filename
115+
* @param string $message
116+
* @return void
117+
*/
118+
public static function assertFileDoesNotExist(string $filename, string $message = ''): void
119+
{
120+
static::assertThat($filename, new LogicalNot(new FileExists), $message);
121+
}
122+
123+
/**
124+
* Asserts that a directory does not exist.
125+
*
126+
* @param string $directory
127+
* @param string $message
128+
* @return void
129+
*/
130+
public static function assertDirectoryDoesNotExist(string $directory, string $message = ''): void
131+
{
132+
static::assertThat($directory, new LogicalNot(new DirectoryExists), $message);
133+
}
134+
135+
/**
136+
* Asserts that a string matches a given regular expression.
137+
*
138+
* @param string $pattern
139+
* @param string $string
140+
* @param string $message
141+
* @return void
142+
*/
143+
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
144+
{
145+
static::assertThat($string, new RegularExpression($pattern), $message);
146+
}
69147
}
70148
}

tests/Filesystem/FilesystemAdapterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Filesystem\FileExistsException;
77
use Illuminate\Contracts\Filesystem\FileNotFoundException;
88
use Illuminate\Filesystem\FilesystemAdapter;
9+
use Illuminate\Foundation\Testing\Assert;
910
use Illuminate\Http\UploadedFile;
1011
use InvalidArgumentException;
1112
use League\Flysystem\Adapter\Local;
@@ -145,7 +146,7 @@ public function testDelete()
145146
file_put_contents($this->tempDir.'/file.txt', 'Hello World');
146147
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
147148
$this->assertTrue($filesystemAdapter->delete('file.txt'));
148-
$this->assertFileNotExists($this->tempDir.'/file.txt');
149+
Assert::assertFileDoesNotExist($this->tempDir.'/file.txt');
149150
}
150151

151152
public function testDeleteReturnsFalseWhenFileNotFound()
@@ -179,7 +180,7 @@ public function testMove()
179180
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
180181
$filesystemAdapter->move('/foo/foo.txt', '/foo/foo2.txt');
181182

182-
$this->assertFileNotExists($this->tempDir.'/foo/foo.txt');
183+
Assert::assertFileDoesNotExist($this->tempDir.'/foo/foo.txt');
183184

184185
$this->assertFileExists($this->tempDir.'/foo/foo2.txt');
185186
$this->assertEquals($data, file_get_contents($this->tempDir.'/foo/foo2.txt'));

tests/Filesystem/FilesystemTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Filesystem\FilesystemManager;
88
use Illuminate\Foundation\Application;
9+
use Illuminate\Foundation\Testing\Assert;
910
use Mockery as m;
1011
use PHPUnit\Framework\TestCase;
1112
use SplFileInfo;
@@ -110,11 +111,11 @@ public function testDeleteRemovesFiles()
110111

111112
$files = new Filesystem;
112113
$files->delete($this->tempDir.'/file1.txt');
113-
$this->assertFileNotExists($this->tempDir.'/file1.txt');
114+
Assert::assertFileDoesNotExist($this->tempDir.'/file1.txt');
114115

115116
$files->delete([$this->tempDir.'/file2.txt', $this->tempDir.'/file3.txt']);
116-
$this->assertFileNotExists($this->tempDir.'/file2.txt');
117-
$this->assertFileNotExists($this->tempDir.'/file3.txt');
117+
Assert::assertFileDoesNotExist($this->tempDir.'/file2.txt');
118+
Assert::assertFileDoesNotExist($this->tempDir.'/file3.txt');
118119
}
119120

120121
public function testPrependExistingFiles()
@@ -144,8 +145,8 @@ public function testDeleteDirectory()
144145
file_put_contents($this->tempDir.'/foo/file.txt', 'Hello World');
145146
$files = new Filesystem;
146147
$files->deleteDirectory($this->tempDir.'/foo');
147-
$this->assertDirectoryNotExists($this->tempDir.'/foo');
148-
$this->assertFileNotExists($this->tempDir.'/foo/file.txt');
148+
Assert::assertDirectoryDoesNotExist($this->tempDir.'/foo');
149+
Assert::assertFileDoesNotExist($this->tempDir.'/foo/file.txt');
149150
}
150151

151152
public function testDeleteDirectoryReturnFalseWhenNotADirectory()
@@ -163,7 +164,7 @@ public function testCleanDirectory()
163164
$files = new Filesystem;
164165
$files->cleanDirectory($this->tempDir.'/foo');
165166
$this->assertDirectoryExists($this->tempDir.'/foo');
166-
$this->assertFileNotExists($this->tempDir.'/foo/file.txt');
167+
Assert::assertFileDoesNotExist($this->tempDir.'/foo/file.txt');
167168
}
168169

169170
public function testMacro()
@@ -228,7 +229,7 @@ public function testMoveDirectoryMovesEntireDirectory()
228229
$this->assertFileExists($this->tempDir.'/tmp2/bar.txt');
229230
$this->assertDirectoryExists($this->tempDir.'/tmp2/nested');
230231
$this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt');
231-
$this->assertDirectoryNotExists($this->tempDir.'/tmp');
232+
Assert::assertDirectoryDoesNotExist($this->tempDir.'/tmp');
232233
}
233234

234235
public function testMoveDirectoryMovesEntireDirectoryAndOverwrites()
@@ -249,9 +250,9 @@ public function testMoveDirectoryMovesEntireDirectoryAndOverwrites()
249250
$this->assertFileExists($this->tempDir.'/tmp2/bar.txt');
250251
$this->assertDirectoryExists($this->tempDir.'/tmp2/nested');
251252
$this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt');
252-
$this->assertFileNotExists($this->tempDir.'/tmp2/foo2.txt');
253-
$this->assertFileNotExists($this->tempDir.'/tmp2/bar2.txt');
254-
$this->assertDirectoryNotExists($this->tempDir.'/tmp');
253+
Assert::assertFileDoesNotExist($this->tempDir.'/tmp2/foo2.txt');
254+
Assert::assertFileDoesNotExist($this->tempDir.'/tmp2/bar2.txt');
255+
Assert::assertDirectoryDoesNotExist($this->tempDir.'/tmp');
255256
}
256257

257258
public function testMoveDirectoryReturnsFalseWhileOverwritingAndUnableToDeleteDestinationDirectory()
@@ -304,7 +305,7 @@ public function testMoveMovesFiles()
304305
$files = new Filesystem;
305306
$files->move($this->tempDir.'/foo.txt', $this->tempDir.'/bar.txt');
306307
$this->assertFileExists($this->tempDir.'/bar.txt');
307-
$this->assertFileNotExists($this->tempDir.'/foo.txt');
308+
Assert::assertFileDoesNotExist($this->tempDir.'/foo.txt');
308309
}
309310

310311
public function testNameReturnsName()

tests/Integration/Mail/SendingMailWithLocaleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Translation\HasLocalePreference;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Foundation\Events\LocaleUpdated;
8+
use Illuminate\Foundation\Testing\Assert;
89
use Illuminate\Mail\Mailable;
910
use Illuminate\Support\Carbon;
1011
use Illuminate\Support\Facades\Event;
@@ -91,7 +92,7 @@ public function testMailIsSentWithLocaleUpdatedListenersCalled()
9192

9293
Mail::to('test@mail.com')->locale('es')->send(new TimestampTestMail);
9394

94-
$this->assertRegExp('/nombre (en|dentro de) (un|1) día/',
95+
Assert::assertMatchesRegularExpression('/nombre (en|dentro de) (un|1) día/',
9596
app('swift.transport')->messages()[0]->getBody()
9697
);
9798

tests/Integration/Notifications/SendingNotificationsWithLocaleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Schema\Blueprint;
88
use Illuminate\Foundation\Events\LocaleUpdated;
9+
use Illuminate\Foundation\Testing\Assert;
910
use Illuminate\Mail\Mailable;
1011
use Illuminate\Notifications\Channels\MailChannel;
1112
use Illuminate\Notifications\Messages\MailMessage;
@@ -150,7 +151,7 @@ public function testMailIsSentWithLocaleUpdatedListenersCalled()
150151
app('swift.transport')->messages()[0]->getBody()
151152
);
152153

153-
$this->assertRegExp('/dans (1|un) jour/',
154+
Assert::assertMatchesRegularExpression('/dans (1|un) jour/',
154155
app('swift.transport')->messages()[0]->getBody()
155156
);
156157

0 commit comments

Comments
 (0)