Skip to content

Commit 7ecc59c

Browse files
author
Zacchaeus Bolaji
committed
use require instead of namespacing
1 parent 20328c1 commit 7ecc59c

File tree

6 files changed

+267
-159
lines changed

6 files changed

+267
-159
lines changed

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/BatchInsertUpdateTest.php

Lines changed: 0 additions & 158 deletions
This file was deleted.

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+
}

0 commit comments

Comments
 (0)