| Conditions | 3 |
| Paths | 2 |
| Total Lines | 52 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | public function testFaker() |
||
| 101 | { |
||
| 102 | $conn = self::getConnection(); |
||
| 103 | |||
| 104 | $generatorFinderBuilder = \DBFaker\Generators\GeneratorFinderBuilder::buildDefaultFinderBuilder(); |
||
| 105 | |||
| 106 | /* Don't hesitate to use the Faker package to generate random data, |
||
| 107 | there is plenty of data types available (IBAN, address, country code, ...). |
||
| 108 | */ |
||
| 109 | // $dataFaker = \Faker\Factory::create();//you could pass the locale to generate localized data ! |
||
| 110 | |||
| 111 | // address.postal_code column is a varchar, so default generated data will be text. Here we want a postal code : |
||
| 112 | $generatorFinderBuilder->addGenerator( |
||
| 113 | new \DBFaker\Generators\Conditions\CallBackCondition(function(\Doctrine\DBAL\Schema\Table $table, \Doctrine\DBAL\Schema\Column $column){ |
||
| 114 | return $table->getName() == "address" && $column->getName() == "postal_code"; |
||
| 115 | }), |
||
| 116 | new SimpleGeneratorFactory("postcode") |
||
| 117 | ); |
||
| 118 | |||
| 119 | // all columns that end with "_email" or are named exactly "email" should be emails |
||
| 120 | $generatorFinderBuilder->addGenerator( |
||
| 121 | new CallBackCondition(function(\Doctrine\DBAL\Schema\Table $table, \Doctrine\DBAL\Schema\Column $column){ |
||
| 122 | return preg_match("/([(.*_)|_|]|^)email$/", $column->getName()) === 1; |
||
| 123 | }), |
||
| 124 | new SimpleGeneratorFactory("email") |
||
| 125 | ); |
||
| 126 | |||
| 127 | $generatorFinderBuilder->addGenerator( |
||
| 128 | new CheckTypeCondition(Type::getType(Type::BLOB)), |
||
| 129 | new BlobGeneratorFactory(__DIR__ . "/fixtures/blob/*") |
||
| 130 | ); |
||
| 131 | $generatorFinderBuilder->addGenerator( |
||
| 132 | new CheckTypeCondition(Type::getType(Type::BINARY)), |
||
| 133 | new BlobGeneratorFactory(__DIR__ . "/fixtures/binary/*") |
||
| 134 | ); |
||
| 135 | |||
| 136 | $faker = new \DBFaker\DBFaker($conn, $generatorFinderBuilder->buildFinder()); |
||
| 137 | |||
| 138 | $tableRowNumbers = [ |
||
| 139 | "users" => 20, |
||
| 140 | "persons" => 30, |
||
| 141 | "users_roles" => 30, |
||
| 142 | "countries" => 10, |
||
| 143 | "regions" => 3 |
||
| 144 | |||
| 145 | ]; |
||
| 146 | $faker->setFakeTableRowNumbers($tableRowNumbers); |
||
| 147 | $faker->fakeDB(); |
||
| 148 | |||
| 149 | foreach ($tableRowNumbers as $tableName => $expectedCount){ |
||
| 150 | $count = $conn->fetchColumn('SELECT count(*) from ' . $tableName); |
||
| 151 | $this->assertEquals($expectedCount, $count); |
||
| 152 | } |
||
| 191 |