Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Merge pull request #556 from dimsav/ft-laravel-58
Browse files Browse the repository at this point in the history
upgrade everything for Laravel 5.8
  • Loading branch information
Gummibeer authored Feb 28, 2019
2 parents 0c1f96d + a38cc39 commit 77da527
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 134 deletions.
45 changes: 40 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,46 @@ version: 2

jobs:
build:
docker:
- image: 'circleci/php:latest'
environment:
LARAVEL_VERSION: '@stable'
- image: 'circleci/mysql:5.7'
command: 'mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin --innodb-large-prefix=true --innodb-file-format=Barracuda'
environment:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: true
<<: *defaults

build-php73-laravel58:
docker:
- image: 'circleci/php:7.3'
environment:
LARAVEL_VERSION: '5.7.*'
LARAVEL_VERSION: '5.8.*'
- image: 'circleci/mysql:5.7'
command: 'mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin --innodb-large-prefix=true --innodb-file-format=Barracuda'
environment:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: true
<<: *defaults

build-php72-laravel58:
docker:
- image: 'circleci/php:7.2'
environment:
LARAVEL_VERSION: '5.8.*'
- image: 'circleci/mysql:5.7'
command: 'mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin --innodb-large-prefix=true --innodb-file-format=Barracuda'
environment:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: true
<<: *defaults

build-php71-laravel58:
docker:
- image: 'circleci/php:7.1'
environment:
LARAVEL_VERSION: '5.8.*'
- image: 'circleci/mysql:5.7'
command: 'mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin --innodb-large-prefix=true --innodb-file-format=Barracuda'
environment:
Expand Down Expand Up @@ -151,9 +187,8 @@ workflows:
version: 2
build_all:
jobs:
- build-php73-laravel57
- build-php72-laravel57
- build-php73-laravel58
- build-php72-laravel58
- build-php71-laravel58
- build-php71-laravel57
- build-php73-laravel56
- build-php72-laravel56
- build-php71-laravel56
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@
{
"name": "Dimitrios Savvopoulos",
"email": "[email protected]",
"homepage": "http://dimsav.com"
"homepage": "http://dimsav.com",
"role": "Developer"
},
{
"name": "Tom Witkowski",
"email": "[email protected]",
"homepage": "https://gummibeer.de",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/dimsav/laravel-translatable/issues"
},
"require": {
"php": ">=7.1.3",
"illuminate/support": "5.6.*|5.7.*|5.8.*"
},
"require-dev": {
"dms/phpunit-arraysubset-asserts": "^0.1.0",
"orchestra/testbench": "3.6.*|3.7.*|3.8.*"
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
>
<testsuites>
<testsuite name="Package Test Suite">
Expand All @@ -16,7 +16,7 @@
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<file>./Translatable/Translatable.php</file>
<file>./src/Translatable/Translatable.php</file>
</whitelist>
</filter>
</phpunit>
128 changes: 9 additions & 119 deletions tests/TestsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,30 @@ class TestsBase extends TestCase

protected function setUp(): void
{
$this->makeSureDatabaseExists(static::DB_NAME);

if (! static::$db2Setup) {
$this->makeSureDatabaseExists(static::DB_NAME2);
}

parent::setUp();

if (! static::$db2Setup) {
$this->makeSureSchemaIsCreated('mysql2');
$this->truncateAllTablesButMigrations(static::DB_NAME2);
static::$db2Setup = true;
}

$this->makeSureSchemaIsCreated('mysql');
$this->enableQueryCounter();
$this->migrate('mysql');
$this->refreshSeedData();
}

private function refreshSeedData()
protected function refreshSeedData()
{
$this->truncateAllTablesButMigrations(static::DB_NAME);
$seeder = new AddFreshSeeds;
$seeder->run();
}

private function makeSureDatabaseExists($dbName)
{
$this->runQuery('CREATE DATABASE IF NOT EXISTS '.$dbName);
}

private function makeSureSchemaIsCreated($dbConnectionName)
protected function migrate($dbConnectionName)
{
$migrationsPath = '../../../../tests/migrations';
$artisan = $this->app->make('Illuminate\Contracts\Console\Kernel');
$artisan = $this->app->make(\Illuminate\Contracts\Console\Kernel::class);

// Makes sure the migrations table is created
$artisan->call('migrate', [
$artisan->call('migrate:fresh', [
'--database' => $dbConnectionName,
'--path' => $migrationsPath,
]);
}

private function truncateAllTablesButMigrations($dbName)
{
$db = $this->app->make('db');
$db->statement('SET FOREIGN_KEY_CHECKS=0;');

foreach ($tables = $db->select('SHOW TABLES') as $table) {
$table = $table->{'Tables_in_'.$dbName};
if ($table != 'migrations') {
$db->table($table)->truncate();
}
}
$db->statement('SET FOREIGN_KEY_CHECKS=1;');
}

/**
* @param $query
* return void
*/
private function runQuery($query)
{
$dbUsername = static::DB_USERNAME;
$dbPassword = static::DB_PASSWORD;

$command = "mysql -u $dbUsername ";
$command .= $dbPassword ? " -p$dbPassword" : '';
$command .= " -e '$query'";

exec($command.' 2>/dev/null');
}

public function testRunningMigration()
{
$country = Country::find(1);
Expand All @@ -96,7 +47,9 @@ public function testRunningMigration()

protected function getPackageProviders($app)
{
return ['Dimsav\Translatable\TranslatableServiceProvider'];
return [
\Dimsav\Translatable\TranslatableServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
Expand Down Expand Up @@ -129,69 +82,6 @@ protected function getEnvironmentSetUp($app)

protected function getPackageAliases($app)
{
return ['Eloquent' => 'Illuminate\Database\Eloquent\Model'];
}

protected function enableQueryCounter()
{
$that = $this;
DB::listen(function (\Illuminate\Database\Events\QueryExecuted $query) use ($that) {
$that->queriesCount++;
// echo("\n--- Query {$that->queriesCount} --- {$this->beautifyQuery($this->insertBindingsIntoQuery($query->sql, $this->formatBindingsForSqlInjection($query->bindings)))}\n");
});
}

private function beautifyQuery($query)
{
$capitalizeWords = ['select ', ' from ', ' where ', ' on ', ' join '];
$newLineWords = ['select ', 'from ', 'where ', 'join '];
foreach ($capitalizeWords as $word) {
$query = str_replace($word, strtoupper($word), $query);
}

foreach ($newLineWords as $word) {
$query = str_replace($word, "\n$word", $query);
$word = strtoupper($word);
$query = str_replace($word, "\n$word", $query);
}

return $query;
}

/**
* @param $bindings
*
* @return mixed
*/
private function formatBindingsForSqlInjection($bindings)
{
foreach ($bindings as $i => $binding) {
if ($binding instanceof DateTime) {
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
} else {
if (is_string($binding)) {
$bindings[$i] = "'$binding'";
}
}
}

return $bindings;
}

/**
* @param $query
* @param $bindings
*
* @return string
*/
private function insertBindingsIntoQuery($query, $bindings)
{
if (empty($bindings)) {
return $query;
}

$query = str_replace(['%', '?'], ['%%', '%s'], $query);

return vsprintf($query, $bindings);
return ['Eloquent' => \Illuminate\Database\Eloquent\Model::class];
}
}
16 changes: 10 additions & 6 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ public function test_it_always_uses_value_when_fallback_not_available()

public function test_translation_with_multiconnection()
{
$this->migrate('mysql2');

// Add country & translation in second db
$country = new Country();
$country->setConnection('mysql2');
Expand All @@ -718,18 +720,20 @@ public function test_translation_with_multiconnection()
// Verify added country & translation in second db
$country = new Country();
$country->setConnection('mysql2');
$sgCountry = $country->find($countryId);
$this->assertEquals('Singapore', $sgCountry->translate('sg')->name);
$sgCountry2 = $country->newQuery()->find($countryId);
$this->assertEquals('Singapore', $sgCountry2->translate('sg')->name);

// Verify added country not in default db
$country = new Country();
$sgCountry = $country::where('code', 'sg')->get();
$this->assertEmpty($sgCountry);
$country->setConnection('mysql');
$sgCountry1 = $country->newQuery()->where('code', 'sg')->get();
$this->assertEmpty($sgCountry1);

// Verify added translation not in default db
$country = new Country();
$sgCountry = $country->find($countryId);
$this->assertEmpty($sgCountry->translate('sg'));
$country->setConnection('mysql');
$sgCountry1 = $country->newQuery()->find($countryId);
$this->assertEmpty($sgCountry1->translate('sg'));
}

public function test_empty_translated_attribute()
Expand Down

0 comments on commit 77da527

Please sign in to comment.