Skip to content

Commit a46e0ac

Browse files
authored
Merge pull request #27 from djunehor/master
Added Tests
2 parents 9a3cc41 + 7ecc59c commit a46e0ac

File tree

6 files changed

+270
-2
lines changed

6 files changed

+270
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea
1+
.idea
2+
vendor
3+
composer.lock

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,9 @@ $result = batch()->update($userInstance, $value, $index);
141141
// ex: insert
142142

143143
$result = batch()->insert($userInstance, $columns, $values, $batchSize);
144-
```
144+
```
145+
146+
# Tests
147+
If you don't have phpunit installed on your project, first run `composser require phpunit/phpunit`
148+
149+
In the root of your laravel app, run `./vendor/bin/phpunit ./vendor/mavinoo/laravel-batch/tests`

tests/BatchInsertTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
require_once ('BootstrapDatabase.php');
3+
4+
class BatchInsertTest extends BootstrapDatabase
5+
{
6+
public $columns = [
7+
'email',
8+
'password',
9+
'name',
10+
'status',
11+
];
12+
13+
public function testBatchInsertWithFacade()
14+
{
15+
$values = [
16+
[
17+
18+
bcrypt('djunehor'),
19+
'djunehor',
20+
'active'
21+
],
22+
[
23+
24+
bcrypt('samuel'),
25+
'samuel',
26+
'whodey'
27+
],
28+
[
29+
30+
bcrypt('general'),
31+
'general',
32+
'inactive',
33+
]
34+
];
35+
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
36+
37+
$result = Batch::insert($this->model, $this->columns, $values, $batchSize);
38+
39+
$this->assertIsArray($result);
40+
$this->assertTrue($result['totalRows'] == 3);
41+
$this->assertTrue($result['totalBatch'] == 500);
42+
$this->model->truncate();
43+
}
44+
45+
public function testBatchInsertIncorrectColumnCount()
46+
{
47+
48+
$columns = [
49+
'email',
50+
'password',
51+
'name',
52+
'status',
53+
];
54+
55+
$values = [
56+
[
57+
58+
bcrypt('djunehor'),
59+
'djunehor',
60+
],
61+
[
62+
63+
bcrypt('samuel'),
64+
'samuel',
65+
'whodey'
66+
],
67+
[
68+
69+
bcrypt('general'),
70+
'general',
71+
'inactive',
72+
]
73+
];
74+
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
75+
76+
$result = Batch::insert($this->model, $this->columns, $values, $batchSize);
77+
$this->assertFalse($result);
78+
}
79+
80+
81+
public function testBatchInsertWithHelper()
82+
{
83+
$values = [
84+
[
85+
86+
bcrypt('djunehor'),
87+
'djunehor',
88+
'active'
89+
],
90+
[
91+
92+
bcrypt('samuel'),
93+
'samuel',
94+
'whodey'
95+
],
96+
[
97+
98+
bcrypt('general'),
99+
'general',
100+
'inactive',
101+
]
102+
];
103+
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
104+
105+
$result = batch()->insert($this->model, $this->columns, $values, $batchSize);
106+
107+
$this->assertIsArray($result);
108+
$this->assertTrue($result['totalRows'] == 3);
109+
$this->assertTrue($result['totalBatch'] == 500);
110+
$this->model->truncate();
111+
}
112+
}

tests/BatchUpdateTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
require_once ('BootstrapDatabase.php');
3+
use Carbon\Carbon;
4+
5+
class BatchUpdateTest extends BootstrapDatabase
6+
{
7+
public $columns = [
8+
'email',
9+
'password',
10+
'name',
11+
'status',
12+
];
13+
14+
private function insert()
15+
{
16+
$values = [
17+
[
18+
19+
bcrypt('djunehor'),
20+
'djunehor',
21+
'active'
22+
],
23+
[
24+
25+
bcrypt('samuel'),
26+
'samuel',
27+
'whodey'
28+
],
29+
[
30+
31+
bcrypt('general'),
32+
'general',
33+
'inactive',
34+
]
35+
];
36+
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
37+
38+
$result = Batch::insert($this->model, $this->columns, $values, $batchSize);
39+
40+
$this->assertIsArray($result);
41+
$this->assertTrue($result['totalRows'] == 3);
42+
$this->assertTrue($result['totalBatch'] == 500);
43+
}
44+
45+
public function testBatchUpdateWithFacade()
46+
{
47+
$this->insert();
48+
$columnValues = [
49+
[
50+
'id' => 1,
51+
'status' => 'amala'
52+
],
53+
[
54+
'id' => 2,
55+
'status' => 'deactive',
56+
'name' => 'Ghanbari'
57+
],
58+
[
59+
'id' => 3,
60+
'status' => 'active',
61+
'created_at' => Carbon::now()
62+
]
63+
];
64+
$index = 'id';
65+
66+
$result = Batch::update($this->model, $columnValues, $index);
67+
68+
$this->assertTrue($result == 3);
69+
$this->model->truncate();
70+
}
71+
72+
public function testBatchUpdateWithHelper()
73+
{
74+
$this->insert();
75+
$columnValues = [
76+
[
77+
'id' => 1,
78+
'status' => 'amala'
79+
],
80+
[
81+
'id' => 2,
82+
'status' => 'deactive',
83+
'name' => 'Ghanbari'
84+
],
85+
[
86+
'id' => 3,
87+
'status' => 'active',
88+
'created_at' => Carbon::now()
89+
]
90+
];
91+
$index = 'id';
92+
93+
$result = batch()->update($this->model, $columnValues, $index);
94+
95+
$this->assertTrue($result == 3);
96+
$this->model->truncate();
97+
}
98+
}

tests/BootstrapDatabase.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
require_once ('TestModel.php');
3+
4+
use Carbon\Carbon;
5+
use Tests\TestCase;
6+
use Illuminate\Support\Facades\Schema;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Database\Migrations\Migration;
9+
10+
class BootstrapDatabase extends TestCase
11+
{
12+
public $model;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
$tableName = (new TestModel())->tableName();
18+
if (!Schema::hasTable($tableName)) {
19+
Schema::create($tableName, function (Blueprint $table) {
20+
$table->increments('id');
21+
$table->string('name')->unique();
22+
$table->string('email')->unique();
23+
$table->string('password');
24+
$table->string('status')->nullable();
25+
$table->rememberToken();
26+
$table->timestamps();
27+
});
28+
}
29+
30+
$this->model = new TestModel();
31+
$this->model->truncate();
32+
33+
}
34+
35+
36+
public function tearDown(): void
37+
{
38+
Schema::dropIfExists((new TestModel())->tableName());
39+
}
40+
}

tests/TestModel.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class TestModel extends \Illuminate\Database\Eloquent\Model
4+
{
5+
protected $table = 'HmKTjCJgLN9bBq7KXzI3';
6+
7+
public function tableName()
8+
{
9+
return $this->table;
10+
}
11+
}

0 commit comments

Comments
 (0)