Skip to content

Commit

Permalink
use require instead of namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacchaeus Bolaji committed Nov 4, 2019
1 parent 20328c1 commit 7ecc59c
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 159 deletions.
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();
}
}
158 changes: 0 additions & 158 deletions tests/BatchInsertUpdateTest.php

This file was deleted.

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();
}
}
Loading

0 comments on commit 7ecc59c

Please sign in to comment.