Skip to content

Commit

Permalink
Merge branch '6.x' into 7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Apr 3, 2020
2 parents 7d21e26 + f0f977a commit e225087
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 15 deletions.
148 changes: 148 additions & 0 deletions src/Illuminate/Foundation/Testing/Assert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Illuminate\Foundation\Testing;

use ArrayAccess;
use Illuminate\Foundation\Testing\Constraints\ArraySubset;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\Constraint\DirectoryExists;
use PHPUnit\Framework\Constraint\FileExists;
use PHPUnit\Framework\Constraint\LogicalNot;
use PHPUnit\Framework\Constraint\RegularExpression;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Runner\Version;
use PHPUnit\Util\InvalidArgumentHelper;

if (class_exists(Version::class) && (int) Version::series()[0] >= 8) {
/**
* @internal This class is not meant to be used or overwritten outside the framework itself.
*/
abstract class Assert extends PHPUnit
{
/**
* Asserts that an array has a specified subset.
*
* @param \ArrayAccess|array $subset
* @param \ArrayAccess|array $array
* @param bool $checkForIdentity
* @param string $msg
* @return void
*/
public static function assertArraySubset($subset, $array, bool $checkForIdentity = false, string $msg = ''): void
{
if (! (is_array($subset) || $subset instanceof ArrayAccess)) {
if (class_exists(InvalidArgumentException::class)) {
throw InvalidArgumentException::create(1, 'array or ArrayAccess');
} else {
throw InvalidArgumentHelper::factory(1, 'array or ArrayAccess');
}
}

if (! (is_array($array) || $array instanceof ArrayAccess)) {
if (class_exists(InvalidArgumentException::class)) {
throw InvalidArgumentException::create(2, 'array or ArrayAccess');
} else {
throw InvalidArgumentHelper::factory(2, 'array or ArrayAccess');
}
}

$constraint = new ArraySubset($subset, $checkForIdentity);

PHPUnit::assertThat($array, $constraint, $msg);
}

/**
* Asserts that a file does not exist.
*
* @param string $filename
* @param string $message
* @return void
*/
public static function assertFileDoesNotExist(string $filename, string $message = ''): void
{
static::assertThat($filename, new LogicalNot(new FileExists), $message);
}

/**
* Asserts that a directory does not exist.
*
* @param string $directory
* @param string $message
* @return void
*/
public static function assertDirectoryDoesNotExist(string $directory, string $message = ''): void
{
static::assertThat($directory, new LogicalNot(new DirectoryExists), $message);
}

/**
* Asserts that a string matches a given regular expression.
*
* @param string $pattern
* @param string $string
* @param string $message
* @return void
*/
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
static::assertThat($string, new RegularExpression($pattern), $message);
}
}
} else {
/**
* @internal This class is not meant to be used or overwritten outside the framework itself.
*/
abstract class Assert extends PHPUnit
{
/**
* Asserts that an array has a specified subset.
*
* @param \ArrayAccess|array $subset
* @param \ArrayAccess|array $array
* @param bool $checkForIdentity
* @param string $msg
* @return void
*/
public static function assertArraySubset($subset, $array, bool $checkForIdentity = false, string $msg = ''): void
{
PHPUnit::assertArraySubset($subset, $array, $checkForIdentity, $msg);
}

/**
* Asserts that a file does not exist.
*
* @param string $filename
* @param string $message
* @return void
*/
public static function assertFileDoesNotExist(string $filename, string $message = ''): void
{
static::assertThat($filename, new LogicalNot(new FileExists), $message);
}

/**
* Asserts that a directory does not exist.
*
* @param string $directory
* @param string $message
* @return void
*/
public static function assertDirectoryDoesNotExist(string $directory, string $message = ''): void
{
static::assertThat($directory, new LogicalNot(new DirectoryExists), $message);
}

/**
* Asserts that a string matches a given regular expression.
*
* @param string $pattern
* @param string $string
* @param string $message
* @return void
*/
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
static::assertThat($string, new RegularExpression($pattern), $message);
}
}
}
5 changes: 3 additions & 2 deletions tests/Filesystem/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Filesystem\FileExistsException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Foundation\Testing\Assert;
use Illuminate\Http\UploadedFile;
use InvalidArgumentException;
use League\Flysystem\Adapter\Local;
Expand Down Expand Up @@ -145,7 +146,7 @@ public function testDelete()
file_put_contents($this->tempDir.'/file.txt', 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertTrue($filesystemAdapter->delete('file.txt'));
$this->assertFileNotExists($this->tempDir.'/file.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/file.txt');
}

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

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

$this->assertFileExists($this->tempDir.'/foo/foo2.txt');
$this->assertEquals($data, file_get_contents($this->tempDir.'/foo/foo2.txt'));
Expand Down
23 changes: 12 additions & 11 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\Assert;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use SplFileInfo;
Expand Down Expand Up @@ -110,11 +111,11 @@ public function testDeleteRemovesFiles()

$files = new Filesystem;
$files->delete($this->tempDir.'/file1.txt');
$this->assertFileNotExists($this->tempDir.'/file1.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/file1.txt');

$files->delete([$this->tempDir.'/file2.txt', $this->tempDir.'/file3.txt']);
$this->assertFileNotExists($this->tempDir.'/file2.txt');
$this->assertFileNotExists($this->tempDir.'/file3.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/file2.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/file3.txt');
}

public function testPrependExistingFiles()
Expand Down Expand Up @@ -144,8 +145,8 @@ public function testDeleteDirectory()
file_put_contents($this->tempDir.'/foo/file.txt', 'Hello World');
$files = new Filesystem;
$files->deleteDirectory($this->tempDir.'/foo');
$this->assertDirectoryNotExists($this->tempDir.'/foo');
$this->assertFileNotExists($this->tempDir.'/foo/file.txt');
Assert::assertDirectoryDoesNotExist($this->tempDir.'/foo');
Assert::assertFileDoesNotExist($this->tempDir.'/foo/file.txt');
}

public function testDeleteDirectoryReturnFalseWhenNotADirectory()
Expand All @@ -163,7 +164,7 @@ public function testCleanDirectory()
$files = new Filesystem;
$files->cleanDirectory($this->tempDir.'/foo');
$this->assertDirectoryExists($this->tempDir.'/foo');
$this->assertFileNotExists($this->tempDir.'/foo/file.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/foo/file.txt');
}

public function testMacro()
Expand Down Expand Up @@ -228,7 +229,7 @@ public function testMoveDirectoryMovesEntireDirectory()
$this->assertFileExists($this->tempDir.'/tmp2/bar.txt');
$this->assertDirectoryExists($this->tempDir.'/tmp2/nested');
$this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt');
$this->assertDirectoryNotExists($this->tempDir.'/tmp');
Assert::assertDirectoryDoesNotExist($this->tempDir.'/tmp');
}

public function testMoveDirectoryMovesEntireDirectoryAndOverwrites()
Expand All @@ -249,9 +250,9 @@ public function testMoveDirectoryMovesEntireDirectoryAndOverwrites()
$this->assertFileExists($this->tempDir.'/tmp2/bar.txt');
$this->assertDirectoryExists($this->tempDir.'/tmp2/nested');
$this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt');
$this->assertFileNotExists($this->tempDir.'/tmp2/foo2.txt');
$this->assertFileNotExists($this->tempDir.'/tmp2/bar2.txt');
$this->assertDirectoryNotExists($this->tempDir.'/tmp');
Assert::assertFileDoesNotExist($this->tempDir.'/tmp2/foo2.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/tmp2/bar2.txt');
Assert::assertDirectoryDoesNotExist($this->tempDir.'/tmp');
}

public function testMoveDirectoryReturnsFalseWhileOverwritingAndUnableToDeleteDestinationDirectory()
Expand Down Expand Up @@ -304,7 +305,7 @@ public function testMoveMovesFiles()
$files = new Filesystem;
$files->move($this->tempDir.'/foo.txt', $this->tempDir.'/bar.txt');
$this->assertFileExists($this->tempDir.'/bar.txt');
$this->assertFileNotExists($this->tempDir.'/foo.txt');
Assert::assertFileDoesNotExist($this->tempDir.'/foo.txt');
}

public function testNameReturnsName()
Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/Mail/SendingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Foundation\Testing\Assert;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
Expand Down Expand Up @@ -86,7 +87,7 @@ public function testMailIsSentWithLocaleUpdatedListenersCalled()

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

$this->assertRegExp('/nombre (en|dentro de) (un|1) día/',
Assert::assertMatchesRegularExpression('/nombre (en|dentro de) (un|1) día/',
app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Foundation\Testing\Assert;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Channels\MailChannel;
use Illuminate\Notifications\Messages\MailMessage;
Expand Down Expand Up @@ -150,7 +151,7 @@ public function testMailIsSentWithLocaleUpdatedListenersCalled()
app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
);

$this->assertRegExp('/dans (1|un) jour/',
Assert::assertMatchesRegularExpression('/dans (1|un) jour/',
app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
);

Expand Down

0 comments on commit e225087

Please sign in to comment.