thecodingmachine /
DBFaker
| 1 | <?php |
||
| 2 | namespace DBFaker; |
||
| 3 | |||
| 4 | use DBFaker\Generators\BlobGenerator; |
||
| 5 | use DBFaker\Generators\BlobGeneratorFactory; |
||
| 6 | use DBFaker\Generators\Conditions\CallBackCondition; |
||
| 7 | use DBFaker\Generators\Conditions\CheckTypeCondition; |
||
| 8 | use DBFaker\Generators\SimpleGenerator; |
||
| 9 | use DBFaker\Generators\SimpleGeneratorFactory; |
||
| 10 | use DBFaker\Helpers\SchemaHelper; |
||
| 11 | use Doctrine\Common\EventManager; |
||
| 12 | use Doctrine\DBAL\Connection; |
||
| 13 | use Doctrine\DBAL\Driver\PDOMySql\Driver; |
||
| 14 | use Doctrine\DBAL\DriverManager; |
||
| 15 | use Doctrine\DBAL\Types\Type; |
||
| 16 | use PHPUnit\Framework\TestCase; |
||
| 17 | use TheCodingMachine\FluidSchema\DefaultNamingStrategy; |
||
| 18 | use TheCodingMachine\FluidSchema\FluidSchema; |
||
| 19 | |||
| 20 | class DBFakerTest extends TestCase |
||
| 21 | { |
||
| 22 | |||
| 23 | public static function setUpBeforeClass() |
||
| 24 | { |
||
| 25 | $adminConn = self::getAdminConnection(); |
||
| 26 | $adminConn->getSchemaManager()->dropAndCreateDatabase($GLOBALS['db_name']); |
||
| 27 | |||
| 28 | $dbConnection = self::getConnection(); |
||
| 29 | $dbalSchema = $dbConnection->getSchemaManager()->createSchema(); |
||
| 30 | $namingStrategy = new DefaultNamingStrategy(); |
||
| 31 | |||
| 32 | $db = new FluidSchema($dbalSchema, $namingStrategy); |
||
| 33 | $fromSchema = clone $dbalSchema; |
||
| 34 | |||
| 35 | $persons = $db->table("persons") |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 36 | ->id() |
||
| 37 | ->column("email")->string(50)->unique() |
||
| 38 | ->column("password")->string(100)->then() |
||
| 39 | ->timestamps(); |
||
| 40 | |||
| 41 | $users = $db->table("users") |
||
| 42 | ->extends("persons") |
||
| 43 | ->column("lastname")->string(50) |
||
| 44 | ->column("firstname")->string(50) |
||
| 45 | ->column("password")->string(200) |
||
| 46 | ->column("uuid")->guid()->null() |
||
| 47 | ->column("parent_id")->references("users") |
||
| 48 | ->column("birth_date")->date() |
||
| 49 | ->column("access_level")->smallInt() |
||
| 50 | ->column("last_access")->datetime(); |
||
| 51 | |||
| 52 | |||
| 53 | $roles = $db->table("roles") |
||
|
0 ignored issues
–
show
|
|||
| 54 | ->id() |
||
| 55 | ->column("label")->string(20); |
||
| 56 | |||
| 57 | $db->junctionTable("users", "roles"); |
||
| 58 | $usersRolesTableName = $namingStrategy->getJointureTableName("users", "roles"); |
||
|
0 ignored issues
–
show
|
|||
| 59 | |||
| 60 | $countries = $db->table("countries") |
||
|
0 ignored issues
–
show
|
|||
| 61 | ->id() |
||
| 62 | ->column("name")->string(100) |
||
| 63 | ->column("population")->bigInt() |
||
| 64 | ->column("birthrate")->float() |
||
| 65 | ->column("president_id")->references("persons") |
||
| 66 | ->column("population_density")->decimal(10,2) |
||
| 67 | ->column("summary")->text(); |
||
| 68 | |||
| 69 | $users->column("country_id")->references("countries"); |
||
| 70 | |||
| 71 | $regions = $db->table("regions") |
||
|
0 ignored issues
–
show
|
|||
| 72 | ->id() |
||
| 73 | ->column("country_id")->references("countries") |
||
| 74 | ->column("name")->string("50") |
||
| 75 | ->column("is_active")->boolean() |
||
| 76 | ->column("binary")->binary() |
||
| 77 | ->column("datetimeTz")->datetimeTz() |
||
| 78 | ->column("time")->time() |
||
| 79 | ->column("dateImmutable")->dateImmutable() |
||
| 80 | ->column("datetimeImmutable")->datetimeImmutable() |
||
| 81 | ->column("datetimeTzImmutable")->datetimeTzImmutable() |
||
| 82 | ->column("timeImmutable")->timeImmutable() |
||
| 83 | ->column("dateInterval")->dateInterval() |
||
| 84 | ->column("blob")->blob() |
||
| 85 | ->column("array")->array() |
||
| 86 | ->column("json")->json() |
||
| 87 | ->column("jsonArray")->jsonArray() |
||
| 88 | ->column("object")->object(); |
||
| 89 | |||
| 90 | $sqlStmts = $dbalSchema->getMigrateFromSql($fromSchema, $dbConnection->getDatabasePlatform()); |
||
| 91 | foreach ($sqlStmts as $sqlStmt) { |
||
| 92 | $dbConnection->exec($sqlStmt); |
||
| 93 | } |
||
| 94 | |||
| 95 | $dbConnection->insert("roles", ["label" => "ADMIN"]); |
||
| 96 | $dbConnection->insert("roles", ["label" => "SIMPLE USER"]); |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 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 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | private static function getAdminConnection() |
||
| 156 | { |
||
| 157 | $config = new \Doctrine\DBAL\Configuration(); |
||
| 158 | |||
| 159 | $connectionParams = array( |
||
| 160 | 'user' => $GLOBALS['db_username'], |
||
| 161 | 'password' => $GLOBALS['db_password'], |
||
| 162 | 'host' => $GLOBALS['db_host'], |
||
| 163 | 'port' => $GLOBALS['db_port'], |
||
| 164 | 'driver' => $GLOBALS['db_driver'], |
||
| 165 | ); |
||
| 166 | |||
| 167 | return DriverManager::getConnection($connectionParams, $config); |
||
| 168 | } |
||
| 169 | |||
| 170 | private static function getConnection() |
||
| 171 | { |
||
| 172 | $connectionParams = array( |
||
| 173 | 'user' => $GLOBALS['db_username'], |
||
| 174 | 'password' => $GLOBALS['db_password'], |
||
| 175 | 'host' => $GLOBALS['db_host'], |
||
| 176 | 'port' => $GLOBALS['db_port'], |
||
| 177 | 'driver' => $GLOBALS['db_driver'], |
||
| 178 | 'dbname' => $GLOBALS['db_name'], |
||
| 179 | "charset" => "utf8", |
||
| 180 | "driverOptions" => array( |
||
| 181 | 1002 =>"SET NAMES utf8" |
||
| 182 | ) |
||
| 183 | ); |
||
| 184 | |||
| 185 | return new Connection($connectionParams, new Driver(), null, new EventManager()); |
||
| 186 | } |
||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | } |
||
| 191 |