Merge pull request #418 from berkaygure/feature/add-tests
Added Customers and Items tests.
This commit is contained in:
commit
1538507e24
24
.env.testing
Normal file
24
.env.testing
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
APP_NAME=Akaunting
|
||||||
|
APP_ENV=testing
|
||||||
|
APP_LOCALE=en-GB
|
||||||
|
APP_INSTALLED=false
|
||||||
|
APP_KEY=base64:xBC+BxlC7sXhYAtpTZv8TYAHqoPgsJaXL0S5Id6BbBc=
|
||||||
|
APP_DEBUG=true
|
||||||
|
APP_LOG_LEVEL=debug
|
||||||
|
APP_URL=http://akaunting.test
|
||||||
|
|
||||||
|
DB_CONNECTION=sqlite
|
||||||
|
DB_DATABASE=:memory:
|
||||||
|
DB_PREFIX=
|
||||||
|
|
||||||
|
BROADCAST_DRIVER=log
|
||||||
|
CACHE_DRIVER=file
|
||||||
|
SESSION_DRIVER=file
|
||||||
|
QUEUE_DRIVER=database
|
||||||
|
|
||||||
|
MAIL_DRIVER=mail
|
||||||
|
MAIL_HOST=localhost
|
||||||
|
MAIL_PORT=2525
|
||||||
|
MAIL_USERNAME=null
|
||||||
|
MAIL_PASSWORD=null
|
||||||
|
MAIL_ENCRYPTION=null
|
@ -38,7 +38,8 @@
|
|||||||
"tucker-eric/eloquentfilter": "1.1.*"
|
"tucker-eric/eloquentfilter": "1.1.*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"fzaninotto/faker": "1.6.*"
|
"fzaninotto/faker": "1.6.*",
|
||||||
|
"phpunit/phpunit": "^7.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"classmap": [
|
"classmap": [
|
||||||
|
1490
composer.lock
generated
1490
composer.lock
generated
File diff suppressed because it is too large
Load Diff
28
database/factories/ItemFacorty.php
Normal file
28
database/factories/ItemFacorty.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Faker\Generator;
|
||||||
|
use App\Models\Auth\User;
|
||||||
|
use App\Models\Common\Item;
|
||||||
|
use App\Models\Common\Company;
|
||||||
|
use Illuminate\Database\Eloquent\Factory;
|
||||||
|
|
||||||
|
/** @var Factory $factory */
|
||||||
|
$factory->define(Item::class, function (Generator $faker) {
|
||||||
|
/** @var User $user */
|
||||||
|
$user = User::first();
|
||||||
|
/** @var Company $company */
|
||||||
|
$company = $user->companies()->first();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => $faker->title,
|
||||||
|
'sku' => $faker->languageCode,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
'description' => $faker->text(100),
|
||||||
|
'purchase_price' => $faker->randomFloat(2,10,20),
|
||||||
|
'sale_price' => $faker->randomFloat(2,10,20),
|
||||||
|
'quantity' => $faker->randomNumber(2),
|
||||||
|
'category_id' => $company->categories()->first()->id,
|
||||||
|
'tax_id' => $company->taxes()->first()->id,
|
||||||
|
'enabled' => $this->faker->boolean ? 1 : 0
|
||||||
|
];
|
||||||
|
});
|
@ -12,7 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||||
$factory->define(App\User::class, function (Faker\Generator $faker) {
|
$factory->define(\App\Models\Auth\User::class, function (Faker\Generator $faker) {
|
||||||
static $password;
|
static $password;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use App\Models\Banking\Account;
|
use App\Models\Banking\Account;
|
||||||
use Setting;
|
use Setting;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use App\Models\Expense\BillStatus;
|
use App\Models\Expense\BillStatus;
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use App\Models\Setting\Category;
|
use App\Models\Setting\Category;
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use App\Models\Setting\Currency;
|
use App\Models\Setting\Currency;
|
||||||
|
|
||||||
|
@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use App\Models\Income\InvoiceStatus;
|
use App\Models\Income\InvoiceStatus;
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use Artisan;
|
use Artisan;
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Setting;
|
use Setting;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Database\Seeds;
|
namespace Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Common\Company;
|
||||||
use App\Models\Model;
|
use App\Models\Model;
|
||||||
use App\Models\Setting\Tax;
|
use App\Models\Setting\Tax;
|
||||||
|
|
||||||
|
32
phpunit.xml
Normal file
32
phpunit.xml
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit backupGlobals="false"
|
||||||
|
backupStaticAttributes="false"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
colors="true"
|
||||||
|
convertErrorsToExceptions="true"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Feature">
|
||||||
|
<directory suffix="Test.php">./tests/Feature</directory>
|
||||||
|
</testsuite>
|
||||||
|
|
||||||
|
<testsuite name="Unit">
|
||||||
|
<directory suffix="Test.php">./tests/Unit</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<filter>
|
||||||
|
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||||
|
<directory suffix=".php">./app</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
<php>
|
||||||
|
<env name="APP_ENV" value="testing"/>
|
||||||
|
<env name="CACHE_DRIVER" value="array"/>
|
||||||
|
<env name="SESSION_DRIVER" value="array"/>
|
||||||
|
<env name="QUEUE_DRIVER" value="sync"/>
|
||||||
|
<env name="MAIL_DRIVER" value="array"/>
|
||||||
|
</php>
|
||||||
|
</phpunit>
|
77
tests/Feature/Common/ItemsTest.php
Normal file
77
tests/Feature/Common/ItemsTest.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Common;
|
||||||
|
|
||||||
|
use App\Models\Common\Item;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
use Tests\Feature\FeatureTestCase;
|
||||||
|
|
||||||
|
class ItemsTest extends FeatureTestCase
|
||||||
|
{
|
||||||
|
public function testItShouldBeShowTheItemsPage()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->get(route('items.index'))
|
||||||
|
->assertStatus(200)
|
||||||
|
->assertSee('Items');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldBeShowCreateItemPage()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->get(route('items.create'))
|
||||||
|
->assertStatus(200)
|
||||||
|
->assertSee('New Item');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldStoreAnItem()
|
||||||
|
{
|
||||||
|
$picture = UploadedFile::fake()->create('image.jpg');
|
||||||
|
|
||||||
|
$item = [
|
||||||
|
'name' => $this->faker->title,
|
||||||
|
'sku' => $this->faker->languageCode,
|
||||||
|
'picture' => $picture,
|
||||||
|
'description' => $this->faker->text(100),
|
||||||
|
'purchase_price' => $this->faker->randomFloat(2,10,20),
|
||||||
|
'sale_price' => $this->faker->randomFloat(2,10,20),
|
||||||
|
'quantity' => $this->faker->randomNumber(2),
|
||||||
|
'category_id' => $this->company->categories()->first()->id,
|
||||||
|
'tax_id' => $this->company->taxes()->first()->id,
|
||||||
|
'enabled' => $this->faker->boolean ? 1 : 0
|
||||||
|
];
|
||||||
|
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->post(route('items.store'), $item)
|
||||||
|
->assertStatus(302)
|
||||||
|
->assertRedirect(route('items.index'));
|
||||||
|
$this->assertFlashLevel('success');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldEditItem()
|
||||||
|
{
|
||||||
|
$item = factory(Item::class)->create();
|
||||||
|
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->get(route('items.edit', ['item' => $item]))
|
||||||
|
->assertStatus(200)
|
||||||
|
->assertSee($item->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldDeleteItem()
|
||||||
|
{
|
||||||
|
$item = factory(Item::class)->create();
|
||||||
|
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->delete(route('items.destroy', ['item' => $item]))
|
||||||
|
->assertStatus(302)
|
||||||
|
->assertRedirect(route('items.index'));
|
||||||
|
|
||||||
|
$this->assertFlashLevel('success');
|
||||||
|
}
|
||||||
|
}
|
@ -2,10 +2,8 @@
|
|||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\Auth\User;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
||||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
||||||
|
|
||||||
class ExampleTest extends TestCase
|
class ExampleTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -16,7 +14,9 @@ class ExampleTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testBasicTest()
|
public function testBasicTest()
|
||||||
{
|
{
|
||||||
$response = $this->get('/');
|
$response = $this
|
||||||
|
->actingAs(User::first())
|
||||||
|
->get('/');
|
||||||
|
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
}
|
}
|
||||||
|
64
tests/Feature/FeatureTestCase.php
Normal file
64
tests/Feature/FeatureTestCase.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: bgure
|
||||||
|
* Date: 13.07.2018
|
||||||
|
* Time: 19:44
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Models\Auth\User;
|
||||||
|
use App\Models\Common\Company;
|
||||||
|
use Faker\Factory;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
abstract class FeatureTestCase extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Faker\Generator
|
||||||
|
*/
|
||||||
|
protected $faker;
|
||||||
|
|
||||||
|
/** @var User */
|
||||||
|
protected $user;
|
||||||
|
|
||||||
|
/** @var Company */
|
||||||
|
protected $company;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->faker = Factory::create();
|
||||||
|
$this->user = User::first();
|
||||||
|
$this->company = $this->user->first()->companies()->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty for default user.
|
||||||
|
*
|
||||||
|
* @param User|null $user
|
||||||
|
* @param Company|null $company
|
||||||
|
* @return FeatureTestCase
|
||||||
|
*/
|
||||||
|
public function loginAs(User $user = null, Company $company = null)
|
||||||
|
{
|
||||||
|
if(!$user) $user = $this->user;
|
||||||
|
if(!$company) $company = $user->companies()->first();
|
||||||
|
$this->startSession();
|
||||||
|
return $this->actingAs($user)
|
||||||
|
->withSession(['company_id' => $company->id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertFlashLevel($excepted)
|
||||||
|
{
|
||||||
|
$flash["level"] = null;
|
||||||
|
if($flashMessage = session('flash_notification'))
|
||||||
|
{
|
||||||
|
$flash = $flashMessage->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals($excepted, $flash['level']);
|
||||||
|
}
|
||||||
|
}
|
127
tests/Feature/Incomes/CustomersTest.php
Normal file
127
tests/Feature/Incomes/CustomersTest.php
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Incomes;
|
||||||
|
|
||||||
|
use App\Models\Auth\User;
|
||||||
|
use App\Models\Income\Customer;
|
||||||
|
use Tests\Feature\FeatureTestCase;
|
||||||
|
|
||||||
|
class CustomersTest extends FeatureTestCase
|
||||||
|
{
|
||||||
|
public function testItShouldCreateOnlyCustomerWithoutUser()
|
||||||
|
{
|
||||||
|
$customer = $this->getCustomerData();
|
||||||
|
$this->loginAs()
|
||||||
|
->post(route("customers.store"), $customer)
|
||||||
|
->assertStatus(302)
|
||||||
|
->assertRedirect(route("customers.index"));
|
||||||
|
$this->assertFlashLevel("success");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldCreateCustomerWithUser()
|
||||||
|
{
|
||||||
|
$customerWithUser = $this->getCustomerDataWithUser();
|
||||||
|
|
||||||
|
$this->loginAs()
|
||||||
|
->post(route("customers.store"), $customerWithUser)
|
||||||
|
->assertStatus(302)
|
||||||
|
->assertRedirect(route("customers.index"));
|
||||||
|
$this->assertFlashLevel("success");
|
||||||
|
|
||||||
|
$user = User::where("email", $customerWithUser["email"])->first();
|
||||||
|
$this->assertNotNull($user);
|
||||||
|
$this->assertEquals($customerWithUser["email"], $user->email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldNotCreateCustomerWithExistsUser()
|
||||||
|
{
|
||||||
|
$customerWithUser = $this->getCustomerDataWithUser();
|
||||||
|
User::create($customerWithUser);
|
||||||
|
|
||||||
|
$this->loginAs()
|
||||||
|
->post(route('customers.store'), $customerWithUser)
|
||||||
|
->assertSessionHasErrors(['email']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldBeSeeTheCustomersPage()
|
||||||
|
{
|
||||||
|
$customer = Customer::create($this->getCustomerData());
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->get(route('customers.index'))
|
||||||
|
->assertStatus(200)
|
||||||
|
->assertSee($customer->email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldBeSeeTheEditCustomersPage()
|
||||||
|
{
|
||||||
|
$customer = Customer::create($this->getCustomerData());
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->get(route('customers.edit', ['customer' => $customer->id]))
|
||||||
|
->assertStatus(200)
|
||||||
|
->assertSee($customer->email)
|
||||||
|
->assertSee($customer->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldUpdateTheCustomer()
|
||||||
|
{
|
||||||
|
$customerData = $this->getCustomerData();
|
||||||
|
$customer = Customer::create($customerData);
|
||||||
|
$customerData["name"] = $this->faker->name;
|
||||||
|
|
||||||
|
$this
|
||||||
|
->loginAs()
|
||||||
|
->patch(route('customers.update', $customer->id), $customerData)
|
||||||
|
->assertStatus(302)
|
||||||
|
->assertRedirect(route('customers.index'));
|
||||||
|
$this->assertFlashLevel('success');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldDeleteTheCustomer()
|
||||||
|
{
|
||||||
|
$customer = Customer::create($this->getCustomerData());
|
||||||
|
|
||||||
|
$this->loginAs()
|
||||||
|
->delete(route('customers.destroy', $customer->id))
|
||||||
|
->assertStatus(302)
|
||||||
|
->assertRedirect(route('customers.index'));
|
||||||
|
|
||||||
|
$this->assertFlashLevel('success');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldNotDeleteIfItHaveRelations()
|
||||||
|
{
|
||||||
|
$this->assertTrue(true);
|
||||||
|
//TODO : This will write after done invoice and revenues tests.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
private function getCustomerData()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'name' => $this->faker->name,
|
||||||
|
'email' => $this->faker->email,
|
||||||
|
'tax_number' => $this->faker->buildingNumber,
|
||||||
|
'phone' => $this->faker->phoneNumber,
|
||||||
|
'address' => $this->faker->streetAddress,
|
||||||
|
'website' => 'www.akaunting.com',
|
||||||
|
'currency_code' => $this->company->currencies()->first()->code,
|
||||||
|
'enabled' => $this->faker->boolean ? 1 : 0
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCustomerDataWithUser()
|
||||||
|
{
|
||||||
|
$password = $this->faker->password;
|
||||||
|
|
||||||
|
return $this->getCustomerData() + [
|
||||||
|
'create_user' => 1,
|
||||||
|
'locale' => 'en-GB',
|
||||||
|
'password' => $password,
|
||||||
|
'password_confirmation' => $password
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,19 @@
|
|||||||
|
|
||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||||
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
|
||||||
abstract class TestCase extends BaseTestCase
|
abstract class TestCase extends BaseTestCase
|
||||||
{
|
{
|
||||||
use CreatesApplication;
|
use CreatesApplication, DatabaseMigrations;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Artisan::call('db:seed', ['--class' => '\Database\Seeds\TestCompany', '--force' => true]);
|
||||||
|
Artisan::call('company:seed',['company' => 1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user