forked from mavinoo/laravelBatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mavinoo#27 from djunehor/master
Added Tests
- Loading branch information
Showing
6 changed files
with
270 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
require_once ('BootstrapDatabase.php'); | ||
|
||
class BatchInsertTest extends BootstrapDatabase | ||
{ | ||
public $columns = [ | ||
'email', | ||
'password', | ||
'name', | ||
'status', | ||
]; | ||
|
||
public function testBatchInsertWithFacade() | ||
{ | ||
$values = [ | ||
[ | ||
'[email protected]', | ||
bcrypt('djunehor'), | ||
'djunehor', | ||
'active' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('samuel'), | ||
'samuel', | ||
'whodey' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('general'), | ||
'general', | ||
'inactive', | ||
] | ||
]; | ||
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query | ||
|
||
$result = Batch::insert($this->model, $this->columns, $values, $batchSize); | ||
|
||
$this->assertIsArray($result); | ||
$this->assertTrue($result['totalRows'] == 3); | ||
$this->assertTrue($result['totalBatch'] == 500); | ||
$this->model->truncate(); | ||
} | ||
|
||
public function testBatchInsertIncorrectColumnCount() | ||
{ | ||
|
||
$columns = [ | ||
'email', | ||
'password', | ||
'name', | ||
'status', | ||
]; | ||
|
||
$values = [ | ||
[ | ||
'[email protected]', | ||
bcrypt('djunehor'), | ||
'djunehor', | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('samuel'), | ||
'samuel', | ||
'whodey' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('general'), | ||
'general', | ||
'inactive', | ||
] | ||
]; | ||
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query | ||
|
||
$result = Batch::insert($this->model, $this->columns, $values, $batchSize); | ||
$this->assertFalse($result); | ||
} | ||
|
||
|
||
public function testBatchInsertWithHelper() | ||
{ | ||
$values = [ | ||
[ | ||
'[email protected]', | ||
bcrypt('djunehor'), | ||
'djunehor', | ||
'active' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('samuel'), | ||
'samuel', | ||
'whodey' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('general'), | ||
'general', | ||
'inactive', | ||
] | ||
]; | ||
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query | ||
|
||
$result = batch()->insert($this->model, $this->columns, $values, $batchSize); | ||
|
||
$this->assertIsArray($result); | ||
$this->assertTrue($result['totalRows'] == 3); | ||
$this->assertTrue($result['totalBatch'] == 500); | ||
$this->model->truncate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
require_once ('BootstrapDatabase.php'); | ||
use Carbon\Carbon; | ||
|
||
class BatchUpdateTest extends BootstrapDatabase | ||
{ | ||
public $columns = [ | ||
'email', | ||
'password', | ||
'name', | ||
'status', | ||
]; | ||
|
||
private function insert() | ||
{ | ||
$values = [ | ||
[ | ||
'[email protected]', | ||
bcrypt('djunehor'), | ||
'djunehor', | ||
'active' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('samuel'), | ||
'samuel', | ||
'whodey' | ||
], | ||
[ | ||
'[email protected]', | ||
bcrypt('general'), | ||
'general', | ||
'inactive', | ||
] | ||
]; | ||
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query | ||
|
||
$result = Batch::insert($this->model, $this->columns, $values, $batchSize); | ||
|
||
$this->assertIsArray($result); | ||
$this->assertTrue($result['totalRows'] == 3); | ||
$this->assertTrue($result['totalBatch'] == 500); | ||
} | ||
|
||
public function testBatchUpdateWithFacade() | ||
{ | ||
$this->insert(); | ||
$columnValues = [ | ||
[ | ||
'id' => 1, | ||
'status' => 'amala' | ||
], | ||
[ | ||
'id' => 2, | ||
'status' => 'deactive', | ||
'name' => 'Ghanbari' | ||
], | ||
[ | ||
'id' => 3, | ||
'status' => 'active', | ||
'created_at' => Carbon::now() | ||
] | ||
]; | ||
$index = 'id'; | ||
|
||
$result = Batch::update($this->model, $columnValues, $index); | ||
|
||
$this->assertTrue($result == 3); | ||
$this->model->truncate(); | ||
} | ||
|
||
public function testBatchUpdateWithHelper() | ||
{ | ||
$this->insert(); | ||
$columnValues = [ | ||
[ | ||
'id' => 1, | ||
'status' => 'amala' | ||
], | ||
[ | ||
'id' => 2, | ||
'status' => 'deactive', | ||
'name' => 'Ghanbari' | ||
], | ||
[ | ||
'id' => 3, | ||
'status' => 'active', | ||
'created_at' => Carbon::now() | ||
] | ||
]; | ||
$index = 'id'; | ||
|
||
$result = batch()->update($this->model, $columnValues, $index); | ||
|
||
$this->assertTrue($result == 3); | ||
$this->model->truncate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
require_once ('TestModel.php'); | ||
|
||
use Carbon\Carbon; | ||
use Tests\TestCase; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class BootstrapDatabase extends TestCase | ||
{ | ||
public $model; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$tableName = (new TestModel())->tableName(); | ||
if (!Schema::hasTable($tableName)) { | ||
Schema::create($tableName, function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name')->unique(); | ||
$table->string('email')->unique(); | ||
$table->string('password'); | ||
$table->string('status')->nullable(); | ||
$table->rememberToken(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
$this->model = new TestModel(); | ||
$this->model->truncate(); | ||
|
||
} | ||
|
||
|
||
public function tearDown(): void | ||
{ | ||
Schema::dropIfExists((new TestModel())->tableName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class TestModel extends \Illuminate\Database\Eloquent\Model | ||
{ | ||
protected $table = 'HmKTjCJgLN9bBq7KXzI3'; | ||
|
||
public function tableName() | ||
{ | ||
return $this->table; | ||
} | ||
} |