在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
In the good old days, I often test PHP applications by accessing it directly from the browser and input the data in the forms. Today, with the explosion of awesome PHP libraries you can now generate most kinds of data by using code alone. The data can then be directly inserted into the database. This reduces the need to input data directly into the app. In this tutorial, I’ll be walking you through Faker , a PHP library that generates fake data for you. InstallationYou can install Faker by executing the following command. Note that this requires you to have Composer installed.
ConceptsHere are a few concepts that you need to remember before moving on.
UsageTo use Faker from your file, you need to include the vendor autoload file and create a new Faker instance.
LocalizationSince Faker is an open-source project that anyone can contribute to, lots of localized providers has already been added. You can take advantage of this by passing in the locale when you create a new Faker instance. For example, if you live in the Philippines:
You can then generate an address in the Philippines by using the
Note that each provider doesn’t have generators for every possible formatter. For example, the Philippine provider has only generators for the Address and PhoneNumber. This means that you can only have localized values for those. All the other formatters will utilize the default ones provided by Faker. For a list of providers, check out this page in their Github repo. FormattersHere are the formatters that I commonly use in my projects.
Creating New ProvidersIf you want to create a provider for your own project, you can easily extend Faker. For example, if you want to generate random pokemon names. The first thing that you need to do is to declare the namespace in which the class belongs. Next, declare a new class and have it extend the faker provider base class. Inside the class, create an array of Pokemon names. Create a new function and call it
Save the file and name it On your main file, include the vendor autoload together with the file that you’ve just created.
Create a new faker generator. This is a bare bones generator with no providers assigned to it. So if you use
If you want to use the default providers, you can include them by calling the
To add the new Pokemon provider.
Once that’s done, you can now call the new
Integration with Your PHP ApplicationMost PHP frameworks today already comes with a database seeding feature. If you’re using Laravel, it has a database migration and seeding functionality . You can simply install Faker into your project, generate a new seeder and then use Faker inside the seeder. This allows you to seed your database with Fake data in a single command by using Artisan CLI . If your framework doesn’t include a seeding feature, you can use Phinx, a database-migration tool for PHP. This tool also allows you to create seeders for your database . ConclusionThat’s it! In this tutorial, you’ve learned how to work with the Faker library to generate fake and random data for testing your PHP applications. Check out the official github page for more information regarding its usage. |
2022-08-30
2022-07-18
2022-08-17
2022-11-06
2022-07-29
请发表评论