[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[PHPUnit] I tried using DataProvider!

Hello!
This is Fukui from the System Development Department!

This time, I tried using an annotation (a useful mechanism) called DataProvider that can be used with PHPUnit, so I would like to introduce how to use it along with the background and background that led to its use!
*In this article, we will omit the explanation of the annotation itself (what is an annotation).

What is DataProvider?

overview

Simply put, a convenient mechanism that allows you to use multiple arbitrary arguments in PHPUnit test methods .
*If you would like a more detailed explanation or explanation of annotations, please refer to the official PHPUnit documentation.

■ PHPUnit official document
https://phpunit.de/documentation.html

■ About DataProvider (annotations)
https://docs.phpunit.de/en/11.0/annotations.html#dataprovider

When to use

As mentioned above, you can pass multiple arguments to a test method,
useful when you want to use multiple different values ​​for the same test method .

Specifically, let's say that there is a process to check the parameters of user information, and that ``login ID is required.''
At this time

・Null and empty strings are treated as abnormalities
・Response parameters are the same (same error response)

In this case,
strictly speaking, the test cases are separated, but the difference required for the test is whether
the login ID is "null" or "empty character" I hope this will be useful when implementing such tests.

So, I think it's hard to convey with just text, so I'll try writing the actual test code later.

How to use

actually write it

For now, I wrote a simple test code using DataProvider.

I would like to see if multiple values ​​set in DataProvider (empty string and null in this case) can be obtained from test method arguments and at least pass the test.

namespace Tests\Feature; use Tests\TestCase; use Illuminate\Testing\Assert; class DataProviderSampleTest extends TestCase { /** * Abnormal: Test when no login ID is specified * @param string $invalidLoginId * @dataProvider invalidLoginIds */ public function testInvalidLoginId($invalidLoginId): void { Assert::assertEmpty($invalidLoginId); } /** * Login ID abnormal test data * * @return array */ public static function invalidLoginIds(): array { return [ [ ''], [null], ]; } }

The points of implementation are as follows.

  1. You can use invalidLoginIds() as a DataProvider by adding @dataProvider comment to the test method.
  2. invalidLoginIds() returns an array, but the elements within the array must also be written as arrays (like [''] and [null])
  3. The argument of testInvalidLoginId() is $invalidLoginId, but it can be determined freely (it is not intended to be singular).

Check test execution results

I was able to confirm that the test passed successfully.

Also, when I actually set the arbitrary value "Beyond Inc." and used the debug function (dd()), I was able to confirm that the intended string was stored in $invalidLoginId.

More practical usage

Up to this point, I have written the minimum test code using DataProvider.
However, since the above code is not very convenient, I would like to introduce some more practical ways to use it.

Below is the changed code.

namespace Tests\Feature; use Tests\TestCase; use Illuminate\Testing\Assert; class DataProviderSampleTest extends TestCase { /** * Login ID string confirmation test * @param string $loginId Login ID string * @param bool $isEmpty empty Expected value of () * @dataProvider loginIds */ public function testLoginId($loginId, $isEmpty): void { Assert::assertSame($isEmpty, empty($loginId)); } /** * Login ID string Test data * * @return array */ public static function loginIds(): array { return [ 'null must be Empty' => [null, true], 'Empty string must be Empty' => ['', true], 'testLoginId must not be Empty' => ['testLoginId', false], ]; } }

The main changes are as follows.

  1. Specify a key for each element of the test data (Specifying a test case or a value for the key makes it easier to see the execution results.I will attach the execution results later.)
  2. Specify multiple elements in the array (The above example specifies the login ID string and the expected value (bool) when executing the assertion.)
  3. Receive multiple values ​​specified by data provider in test method arguments

Check the execution results again

Check the test execution results again.

The test passed successfully.
as I introduced earlier I think it
becomes easier to understand which tests passed Also, by passing a bool as the expected value, is highly versatile, including normal and abnormal systems

personal impressions

Frankly, I thought it was a useful feature for writing DRY code.
I felt that it was a good way to write test code more simply and in a small amount, so I would like to utilize it in future implementations.

Also, a little off-topic, but the reason I looked into DataProvider this time
was when I wanted to execute a loop by changing only the values ​​used for a test in a framework of another language, and I thought, ``Well, PHPUnit has a similar function, right?'' ...! There is a background that led me to this idea.
It was a good opportunity to reaffirm that it is extremely beneficial to increase the number of implementation methods and tools that go beyond languages ​​and frameworks.
There are other annotations that I have not touched yet, so I will continue to try them out.

That’s all for this article!
Thank you for reading to the end!
See you soon!

For web system/application development
(Click here for service page)

If you found this article helpful , please give it a like!
4
Loading...
4 votes, average: 1.00 / 14
987
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Hiroto Fukui

Joined Beyond in June 2020.
He works in the System Development Department (Yokohama office). His work focuses on PHP, developing game APIs and web systems, and developing Shopify private apps.
He likes music in general, mainly Western music, and plays the guitar as a hobby. His favorite TV shows are "Detective! Night Scoop" and "Infestation! Ad Street Heaven."