Download the PHP package viraj/cakephp-testdummy without Composer
On this page you can find all versions of the php package viraj/cakephp-testdummy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cakephp-testdummy
Cakephp Testdummy
Testdummy provides an easy way of creating random test data. While writing tests you would require random/fake data to run your tests.
Testdummy helps you to create a random set of fake data which you can configure exactly according to your needs in the test.
Step 1: Installation
Install this package using Composer:
Step 2: Create a factories file
Within the config/Factories directory, create a TableFactory.php file with the following contents:
Within a config/Factories directory, you may create any number of PHP files that will automatically be loaded by our package.
Step 3: Write a factory
Before using factories, you must define them in the above file. An example factory definition would look like this:
In TableFactory.php you will have access to $faker variable which is an instance of the Generator class in the Faker package. Using Faker, you can create random data of various types and even get values which are local to your country. Please read the documentation of Faker to understand their API.
Step 4: Using Factories
To use factories, your tests need to extend the \TestDummy\BaseTestCase. This class extends the IntegrationTestCase present in CakePHP core, so you get access to all the core features and assertions.
If you are using the Integrated package, you don't need to extend the
BaseTestCaseJust extend the appropriate class in the Integrated package, and factories will be loaded automatically
Now, you can use your defined factories in the tests:
Step 5: Database Migrations
Fixtures create tables before every test and drop them after every tests. When using fixtures, you would need to define fixture files, plus import or configure the names of fixtures to be used in every test.
Alternatively, you can use the DatabaseMigrations trait which will basically migrate your database before every test and delete all the tables after every test. Here is an example of how to do this:
Overriding attributes
You can even override specific attributes in your tests while using factories:
The above code will generate a post record in the database with the above title and fake data for other fields.
Collection of Factories
If you want to create a collection of 100 posts, you can do so by using the following syntax:
The above code will create 100 post records and return a Cake\Collection\Collection instance containing 100 posts
All versions of cakephp-testdummy with dependencies
fakerphp/faker Version ~1.4
symfony/finder Version ^3.3
cakephp/cakephp Version ^4.0
cakephp/chronos Version ^2.0
cakephp/migrations Version ^3.0