Skip to content

Commit 20328c1

Browse files
author
Zacchaeus Bolaji
committed
Added test class
1 parent 9a3cc41 commit 20328c1

File tree

2 files changed

+161
-1
lines changed

2 files changed

+161
-1
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

tests/BatchInsertUpdateTest.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
use Carbon\Carbon;
3+
use Tests\TestCase;
4+
use Illuminate\Support\Facades\Schema;
5+
use Illuminate\Database\Schema\Blueprint;
6+
7+
class TestModel extends \Illuminate\Database\Eloquent\Model
8+
{
9+
protected $table = 'HmKTjCJgLN9bBq7KXzI3';
10+
11+
public function tableName()
12+
{
13+
return $this->table;
14+
}
15+
}
16+
17+
class BatchInsertUpdateTest extends TestCase
18+
{
19+
private $model;
20+
private $tableName;
21+
22+
public function setUp(): void
23+
{
24+
parent::setUp();
25+
$tableName = (new TestModel())->tableName();
26+
if (!Schema::hasTable($tableName)) {
27+
Schema::create($tableName, function (Blueprint $table) {
28+
$table->increments('id');
29+
$table->string('name')->unique();
30+
$table->string('email')->unique();
31+
$table->string('password');
32+
$table->string('status')->nullable();
33+
$table->rememberToken();
34+
$table->timestamps();
35+
});
36+
}
37+
38+
$this->model = new TestModel();
39+
$this->model->truncate();
40+
41+
}
42+
43+
44+
public function tearDown(): void
45+
{
46+
Schema::dropIfExists((new TestModel())->tableName());
47+
}
48+
49+
private function insert()
50+
{
51+
$columns = [
52+
'email',
53+
'password',
54+
'name',
55+
'status',
56+
];
57+
58+
$values = [
59+
[
60+
61+
bcrypt('djunehor'),
62+
'djunehor',
63+
'active'
64+
],
65+
[
66+
67+
bcrypt('samuel'),
68+
'samuel',
69+
'whodey'
70+
],
71+
[
72+
73+
bcrypt('general'),
74+
'general',
75+
'inactive',
76+
]
77+
];
78+
79+
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
80+
81+
$result = Batch::insert($this->model, $columns, $values, $batchSize);
82+
83+
return $result;
84+
}
85+
86+
87+
public function testBatchInsert()
88+
{
89+
$result = $this->insert();
90+
91+
$this->assertIsArray($result);
92+
$this->assertTrue($result['totalRows'] == 3);
93+
$this->assertTrue($result['totalBatch'] == 500);
94+
$this->model->truncate();
95+
}
96+
97+
public function testBatchInsertIncorrectColumnCount()
98+
{
99+
100+
$columns = [
101+
'email',
102+
'password',
103+
'name',
104+
'status',
105+
];
106+
107+
$values = [
108+
[
109+
110+
bcrypt('djunehor'),
111+
'djunehor',
112+
],
113+
[
114+
115+
bcrypt('samuel'),
116+
'samuel',
117+
'whodey'
118+
],
119+
[
120+
121+
bcrypt('general'),
122+
'general',
123+
'inactive',
124+
]
125+
];
126+
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
127+
128+
$result = Batch::insert($this->model, $columns, $values, $batchSize);
129+
$this->assertFalse($result);
130+
}
131+
132+
public function testBatchUpdate()
133+
{
134+
$this->insert();
135+
$columnValues = [
136+
[
137+
'id' => 1,
138+
'status' => 'amala'
139+
],
140+
[
141+
'id' => 2,
142+
'status' => 'deactive',
143+
'name' => 'Ghanbari'
144+
],
145+
[
146+
'id' => 3,
147+
'status' => 'active',
148+
'created_at' => Carbon::now()
149+
]
150+
];
151+
$index = 'id';
152+
153+
$result = Batch::update($this->model, $columnValues, $index);
154+
155+
$this->assertTrue($result == 3);
156+
$this->model->truncate();
157+
}
158+
}

0 commit comments

Comments
 (0)