Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
The new factories, causes the unit tests to fail. The tests are
expecting a specific seeded data set, with the new method we don’t have
that set anymore.

- updated test to use data that is available instead of hardcooded
values
  • Loading branch information
joverthegrey committed Sep 8, 2016
1 parent 412c68b commit ef2aa06
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,3 +7,5 @@ AbuseIO.iml
_ide_helper.php
composer.lock
htpasswd

.DS_Store
9 changes: 1 addition & 8 deletions database/factories/ModelFactory.php
Expand Up @@ -33,15 +33,8 @@
});

$factory->define(AbuseIO\Models\Contact::class, function (Faker\Generator $faker) {
global $contact_reference_counter;
if (!$contact_reference_counter) {
$contact_reference_counter = 1;
} else {
$contact_reference_counter++;
}

return [
'reference' => sprintf('reference_%d', $contact_reference_counter),
'reference' => sprintf('reference_%s',uniqid()),
'name' => $faker->name,
'email' => $faker->email,
'api_host' => $faker->url, //'api_host',
Expand Down
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Expand Up @@ -20,6 +20,7 @@ public function run()
factory(AbuseIO\Models\Domain::class, 10)->create();
factory(AbuseIO\Models\Account::class, 4)->create();
factory(AbuseIO\Models\User::class, 4)->create();
factory(AbuseIO\Models\Ticket::class, 10)->create();

factory(AbuseIO\Models\Role::class)->create(
[
Expand Down
2 changes: 2 additions & 0 deletions tests/Api/ShowTestHelper.php
Expand Up @@ -17,8 +17,10 @@ trait ShowTestHelper
public function testStatusCodeValidRequest()
{
$this->initWithValidResponse();

$this->assertEquals(200, $this->statusCode);


$obj = json_decode($this->content);
$this->assertTrue($obj->message->success);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Console/Commands/Account/ShowCommandTest.php
Expand Up @@ -2,6 +2,7 @@

namespace tests\Console\Commands\Account;

use AbuseIO\Models\Account;
use Illuminate\Support\Facades\Artisan;
use tests\TestCase;

Expand All @@ -27,14 +28,16 @@ public function testWithValidIdFilter()

public function testWithValidNameFilter()
{
$account = Account::all()->random();

$exitCode = Artisan::call(
'account:show',
[
'account' => 'Customer Internet',
'account' => $account->name,
]
);
$this->assertEquals($exitCode, 0);
$this->assertContains('Customer Internet', Artisan::output());
$this->assertContains($account->name, Artisan::output());
}

public function testWithInvalidFilter()
Expand Down
20 changes: 12 additions & 8 deletions tests/Console/Commands/Contact/EditCommandTest.php
Expand Up @@ -34,41 +34,45 @@ public function testWithInvalidId()

public function testName()
{
$this->assertEquals('John Doe', Contact::find(1)->name);
$contact = Contact::all()->random();
$oldname = $contact->name;

$exitCode = Artisan::call(
'contact:edit',
[
'id' => '1',
'id' => $contact->id,
'--name' => 'New name',
]
);
$this->assertEquals($exitCode, 0);
$this->assertContains('The contact has been updated', Artisan::output());

$contact = Contact::find(1);
// update contact
$contact = Contact::find($contact->id);
$this->assertEquals('New name', $contact->name);
$contact->name = 'John Doe';
$contact->name = $oldname;
$contact->save();
}

public function testCompanyName()
{
$this->assertEquals('JOHND', Contact::find(1)->reference);
$contact = Contact::all()->random();
$oldref = $contact->reference;

$exitCode = Artisan::call(
'contact:edit',
[
'id' => '1',
'id' => $contact->id,
'--reference' => 'New reference',
]
);
$this->assertEquals($exitCode, 0);
$this->assertContains('The contact has been updated', Artisan::output());

$contact = Contact::find(1);
// update contact
$contact = Contact::find($contact->id);
$this->assertEquals('New reference', $contact->reference);
$contact->reference = 'JOHND';
$contact->reference = $oldref;
$contact->save();
}
}
7 changes: 5 additions & 2 deletions tests/Console/Commands/Contact/ShowCommandTest.php
Expand Up @@ -2,6 +2,7 @@

namespace tests\Console\Commands\Contact;

use AbuseIO\Models\Contact;
use Illuminate\Support\Facades\Artisan;
use tests\TestCase;

Expand All @@ -27,14 +28,16 @@ public function testWithValidIdFilter()

public function testWithValidNameFilter()
{
$contact = Contact::all()->random();

$exitCode = Artisan::call(
'contact:show',
[
'contact' => 'John Doe',
'contact' => $contact->name,
]
);
$this->assertEquals($exitCode, 0);
$this->assertContains('j.doe@customers.isp.local', Artisan::output());
$this->assertContains($contact->email, Artisan::output());
}

public function testWithInvalidFilter()
Expand Down
6 changes: 5 additions & 1 deletion tests/Console/Commands/Domain/ListCommandTest.php
Expand Up @@ -2,6 +2,7 @@

namespace tests\Console\Commands\Domain;

use AbuseIO\Models\Domain;
use Illuminate\Support\Facades\Artisan;
use tests\TestCase;

Expand All @@ -25,10 +26,13 @@ public function testHeaders()

public function testAll()
{
$domain = Domain::all()->random();
$contact = $domain->contact;

$exitCode = Artisan::call('domain:list', []);

$this->assertEquals($exitCode, 0);
$this->assertContains('Customer 1', Artisan::output());
$this->assertContains($contact->name, Artisan::output());
}

public function testFilter()
Expand Down
19 changes: 15 additions & 4 deletions tests/Console/Commands/Netblock/ListCommandTest.php
Expand Up @@ -2,6 +2,8 @@

namespace tests\Console\Commands\Netblock;

use AbuseIO\Models\Contact;
use AbuseIO\Models\Netblock;
use Illuminate\Support\Facades\Artisan;
use tests\TestCase;

Expand All @@ -12,23 +14,32 @@ class ListCommandTest extends TestCase
{
public function testNetBlockListCommand()
{
$netblock = Netblock::all()->random();
$contact = $netblock->contact;

$exitCode = Artisan::call('netblock:list', []);

$this->assertEquals($exitCode, 0);
$this->assertContains('John Doe', Artisan::output());
$this->assertContains($contact->name, Artisan::output());
}

public function testNetBlockListCommandWithValidFilter()
{
$netblock = Netblock::all()->random();
$ip = $netblock->first_ip;
$netblock_contact = $netblock->contact;
$other_contact = Contact::where('id', '!=', $netblock_contact->id)
->get()->random();

$exitCode = Artisan::call(
'netblock:list',
[
'--filter' => '192.168.1.0',
'--filter' => $ip,
]
);

$this->assertEquals($exitCode, 0);
$this->assertContains('ISP Business Internet', Artisan::output());
$this->assertNotContains('Customer 1', Artisan::output());
$this->assertContains($netblock_contact->name, Artisan::output());
$this->assertNotContains($other_contact->name, Artisan::output());
}
}
19 changes: 13 additions & 6 deletions tests/Console/Commands/User/ListCommandTest.php
Expand Up @@ -2,6 +2,7 @@

namespace tests\Console\Commands\User;

use AbuseIO\Models\User;
use Illuminate\Support\Facades\Artisan;
use tests\TestCase;

Expand All @@ -10,8 +11,11 @@
*/
class ListCommandTest extends TestCase
{
public function testNetBlockListCommand()
public function testUserListCommand()
{

$user = User::all()->random();

$exitCode = Artisan::call(
'user:list',
[
Expand All @@ -20,22 +24,25 @@ public function testNetBlockListCommand()
);

$this->assertEquals($exitCode, 0);
$this->assertContains('admin@isp.local', Artisan::output());
$this->assertContains($user->email, Artisan::output());
}

public function testNetBlockListCommandWithValidFilter()
public function testUserListCommandWithValidFilter()
{
$user = User::all()->random();
$other_user = User::where('id','!=', $user->id)->get()->random();

$exitCode = Artisan::call(
'user:list',
[
'--filter' => 'user',
'--filter' => $user->email,
]
);

$this->assertEquals($exitCode, 0);

$output = Artisan::output();
$this->assertContains('user@isp.local', $output);
$this->assertNotContains('admin@account2.local', $output);
$this->assertContains($user->email, $output);
$this->assertNotContains($other_user->email, $output);
}
}

1 comment on commit ef2aa06

@marknl
Copy link
Member

@marknl marknl commented on ef2aa06 Sep 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would've done this a bit differently. (I'm currently working on using "setUp()" for each unit test to setup dummy data to test with. This way the tests don't have to use supposibly seeded data. The only exception is that ther MUST be a user with id=1 (admin). But that's always seeded.

Please sign in to comment.