无忧卡密自助提取系统

tech2022-08-29  174

无忧卡密自助提取系统

When working with the filesystem in our tests suites, a big concern is cleaning up the temporary files after each test runs. However, if for any reason the test’s execution is interrupted before the cleanup phase, further tests might fail, as the environment has not been cleaned up.

在我们的测试套件中使用文件系统时,一个大问题是在每次测试运行后清理临时文件。 但是,如果由于某种原因在清理阶段之前中断了测试的执行,则进一步的测试可能会失败,因为尚未清理环境。

In this post, we will use a library named vfsStream to create filesystem mocks. It’s little more than a wrapper around a virtual filesystem, which also works very nicely with PHPUnit.

在本文中,我们将使用名为vfsStream的库来创建文件系统模拟 。 它只不过是围绕虚拟文件系统的包装程序,它与PHPUnit配合也很好。

Note This post requires a basic understanding of unit testing and PHPUnit.

注意:这篇文章需要对单元测试和PHPUnit有基本的了解。

To have something to test, we’ll write a simple class for creating files:

为了进行测试,我们将编写一个用于创建文件的简单类:

<?php namespace App\Tests; class FileCreator extends PHPUnit_Framework_TestCase { public static function create($path, $name, $content) { $file_name = rtrim($path, '/') . '/' . $name; if (file_put_contents($filename, $content)) { return true; } return false; } }

Inside the class, we have a create() method, which accepts the filename, content, and a path to which to save it.

在类内部,我们有一个create()方法,该方法接受文件名,内容以及保存文件的路径。

To test the class, we first take the traditional approach, and create a test class without using a virtual filesystem. Then, we’ll bring vfsStream into the game to see how it can simplify the testing process for us.

为了测试该类,我们首先采用传统方法,并在不使用虚拟文件系统的情况下创建一个测试类。 然后,我们将vfsStream引入游戏中,以了解它如何为我们简化测试过程。

传统方法 (Traditional Approach)

<?php namespace App\Tests; class FileCreatorTest extends PHPUnit_Framework_TestCase { protected $path; public function setUp() { $this->path = sys_get_temp_dir(); } public function tearDown() { if (file_exists($this->path . '/test.txt')) { unlink($this->path . '/test.txt'); } } public function testCreate() { $this->assertTrue(FileCreator::create($this->path, 'test.txt', 'Lorem ipsum dolor sit amet')); $this->assertFileExists($this->path . '/test.txt'); } }

In the above, we define the temporary directory (in setUp()) which we’ll use for our temporary files by using PHP’s sys_get_temp_dir(). The function returns the system’s directory path for temporary files.

在上面,我们定义了临时目录(在setUp() ),该目录将通过使用PHP的sys_get_temp_dir()用于临时文件。 该函数返回系统的临时文件目录路径。

Then, we test our class’ functionality by making two assertions and finally, we clean the environment up (in tearDown()) for the next set of tests.

然后,我们通过发出两个断言来测试类的功能,最后,我们为下一组测试清理环境(在tearDown() )。

There’s nothing wrong with this code in its current state. But what if we have to work with many files in each test run? What if the code breaks before we get the chance to remove the temporary files? vfsStream has the answers to these questions.

在当前状态下,此代码没有任何问题。 但是,如果我们每次测试都必须处理多个文件怎么办? 如果在我们有机会删除临时文件之前代码中断了怎么办? vfsStream可以回答这些问题。

使用vfsStream (Using vfsStream)

In this section, we’ll rewrite FileCreatorTest with the help of vfsStream.

在本节中,我们将在vfsStream的帮助下重写FileCreatorTest 。

To install it, we use Composer:

要安装它,我们使用Composer:

composer require mikey179/vfsStream

Here’s the test class with vfsStream:

这是vfsStream的测试类:

<?php use org\bovigo\vfs\vfsStream; class FileCreatorTest extends PHPUnit_Framework_TestCase { /** * @var org\bovigo\vfs\vfsStreamDirectory */ protected $vfs; public function setUp() { // Setting up vfsStream in our test class $this->vfs = vfsStream::setup('testDirectory'); } public function testCreate() { $this->assertTrue(FileCreator::create(vfsStream::url('testDirectory'), 'test.txt', 'Lorem ipsum dolor sit amet')); $this->assertFileExists($this->vfs->getChild('test.txt')->url()); } }

In setUp(), we set up vfsStream by calling vfsStream::setup(). This method sets up vfsStream, and returns an instance of vfsStreamDirectory. This is our directory container.

在setUp() ,我们通过调用vfsStream::setup() 。 此方法设置vfsStream,并返回vfsStreamDirectory的实例。 这是我们的目录容器 。

The directory container is actually a filesystem mock with one main directory called testDirectory.

该目录容器实际上是一个文件系统模型,具有一个名为testDirectory主目录。

In the first assertion, we pass the main virtual path (by using vfsStream::url()) to FileCreator::create() along with the filename and its content:

在第一个断言中,我们将主虚拟路径(通过使用vfsStream::url() )与文件名及其内容一起传递到FileCreator::create() :

<?php // ... $this->assertTrue(FileCreator::create(vfsStream::url('testDirectory'), 'test.txt', 'Lorem ipsum dolor sit amet'));

In the next assertion, we check for the existence of test.txt. To do this, we retrieve the file object (test.txt) from the directory container and get its full path:

在下一个断言中,我们检查是否存在test.txt 。 为此,我们从目录容器中检索文件对象( test.txt )并获取其完整路径:

<?php // ... $this->assertFileExists($this->vfs->getChild('test.txt')->url());

As you can see, there’s no sign of any cleanup in our test class as all the filesystem operations (creation and cleanup) are handled by vfsStream. If for any reason the code breaks, there’s still no need to do any cleanup as everything is happening in memory.

如您所见,在我们的测试类中没有任何清除的迹象,因为所有文件系统操作(创建和清除)均由vfsStream处理。 如果由于某种原因代码中断了,那么由于内存中发生的所有事情仍然不需要进行任何清理。

结论 (Conclusion)

Apart from what we’ve seen here, vfsStream has a lot more to offer, including but not limited to handling permissions, ownerships, handling large files and complex directory structures, etc. To learn more about it, see their official documentation or wait for some upcoming advanced tutorials on SitePoint!

除了我们在这里看到的内容以外,vfsStream还提供了更多功能,包括但不限于处理权限,所有权,处理大文件和复杂的目录结构等。要了解更多信息,请参阅其官方文档或等待SitePoint上即将发布的一些高级教程!

What do you think about this approach to testing filesystem operations? Do you use an alternative solution? Anticipate any limitations you might run into when using this approach? Let us know!

您如何看待这种测试文件系统操作的方法? 您是否使用替代解决方案? 预期使用此方法时可能遇到的任何限制? 让我们知道!

翻译自: https://www.sitepoint.com/hassle-free-filesystem-operations-during-testing/

无忧卡密自助提取系统

相关资源:域名授权系统全新一键安装源码+卡密自助授权+全新UI界面
最新回复(0)