Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abdumu committed Dec 5, 2017
1 parent fb367c3 commit 9abcab4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public function compileInsert(Builder $query, array $values)
return "insert into $table ($names) select ".implode(' union all select ', $columns);
}


/**
* Compile a delete statement into SQL.
*
Expand All @@ -191,9 +190,7 @@ public function compileDelete(Builder $query)
// we use it in select sub-query and in delete where-in statement.
$selectSql = parent::compileSelect($query->select("{$query->from}.rowid"));

return trim(
"delete from {$this->wrapTable($query->from)} where {$this->wrap('rowid')} in ({$selectSql})"
);
return "delete from {$this->wrapTable($query->from)} where {$this->wrap('rowid')} in ({$selectSql})";
}

$wheres = is_array($query->wheres) ? $this->compileWheres($query) : '';
Expand Down
70 changes: 70 additions & 0 deletions tests/Integration/Database/EloquentDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;

/**
* @group integration
*/
class EloquentDeleteTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

public function setUp()
{
parent::setUp();

Schema::create('posts', function ($table) {
$table->increments('id');
$table->string('title')->nullable();
$table->timestamps();
});

Schema::create('comments', function ($table) {
$table->increments('id');
$table->string('body')->nullable();
$table->integer('post_id');
$table->timestamps();
});
}

public function testOnlyDeleteWhatGiven()
{
for($i = 1; $i <= 10; $i++) {
Comment::create([
'post_id' => Post::create()->id
]);
}

Post::latest('id')->limit(1)->delete();
$this->assertEquals(9, Post::all()->count());

Post::join('comments', 'comments.post_id', '=', 'posts.id')->where('posts.id', '>', 1)->orderBy('posts.id')->limit(1)->delete();
$this->assertEquals(8, Post::all()->count());
}
}

class Post extends Model
{
public $table = 'posts';
}

class Comment extends Model
{
public $table = 'comments';
protected $fillable = ['post_id'];
}

0 comments on commit 9abcab4

Please sign in to comment.