Skip to content

Commit

Permalink
Merge pull request mavinoo#27 from djunehor/master
Browse files Browse the repository at this point in the history
Added Tests
  • Loading branch information
mavinoo authored Nov 4, 2019
2 parents 9a3cc41 + 7ecc59c commit a46e0ac
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
.idea
vendor
composer.lock
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ $result = batch()->update($userInstance, $value, $index);
// ex: insert

$result = batch()->insert($userInstance, $columns, $values, $batchSize);
```
```

# Tests
If you don't have phpunit installed on your project, first run `composser require phpunit/phpunit`

In the root of your laravel app, run `./vendor/bin/phpunit ./vendor/mavinoo/laravel-batch/tests`
112 changes: 112 additions & 0 deletions tests/BatchInsertTest.php
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();
}
}
98 changes: 98 additions & 0 deletions tests/BatchUpdateTest.php
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();
}
}
40 changes: 40 additions & 0 deletions tests/BootstrapDatabase.php
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());
}
}
11 changes: 11 additions & 0 deletions tests/TestModel.php
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;
}
}

0 comments on commit a46e0ac

Please sign in to comment.