From d6877700a5d6906b93da7585901b091701882472 Mon Sep 17 00:00:00 2001 From: Kader1680 Date: Sun, 1 Dec 2024 12:04:22 +0100 Subject: [PATCH 1/4] make new test for register user --- .env.example | 19 ++++++----- composer.json | 2 +- composer.lock | 2 +- config/database.php | 4 +-- tests/CreatesApplication.php | 7 ++--- tests/Feature/ExampleTest.php | 10 +++--- tests/Feature/ProductTest.php | 35 +++++++++++++++++++++ tests/Feature/UserTest.php | 32 +++++++++++++++++++ tests/TestCase.php | 2 +- tests/Unit/{ExampleTest.php => Product.php} | 8 ++--- tests/Unit/UserTest.php | 0 yarn.lock | 11 ++----- 12 files changed, 97 insertions(+), 35 deletions(-) create mode 100644 tests/Feature/ProductTest.php create mode 100644 tests/Feature/UserTest.php rename tests/Unit/{ExampleTest.php => Product.php} (50%) create mode 100644 tests/Unit/UserTest.php diff --git a/.env.example b/.env.example index c83b59bc..a1caa621 100644 --- a/.env.example +++ b/.env.example @@ -19,13 +19,18 @@ LOG_STACK=single LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug -DB_CONNECTION=sqlite -# DB_HOST=127.0.0.1 -# DB_PORT=3306 -# DB_DATABASE=laravel -# DB_USERNAME=root -# DB_PASSWORD= - +# DB_CONNECTION=sqlite +# # DB_HOST=127.0.0.1 +# # DB_PORT=3306 +# # DB_DATABASE=laravel +# # DB_USERNAME=root +# # DB_PASSWORD= +DB_CONNECTION=mysql +DB_HOST=127.0.0.1 +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=root +DB_PASSWORD= SESSION_DRIVER=database SESSION_LIFETIME=120 SESSION_ENCRYPT=false diff --git a/composer.json b/composer.json index 20b27ce7..bf45611c 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "laravel/sail": "^1.26", "mockery/mockery": "^1.6", "nunomaduro/collision": "^8.0", - "phpunit/phpunit": "^11.0.1" + "phpunit/phpunit": "^11.3" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 0b3b3798..c26485d8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "69b12737152492cc1654bfbb86820815", + "content-hash": "80f95c770e44c0aafad3e8229e80d4ca", "packages": [ { "name": "brick/math", diff --git a/config/database.php b/config/database.php index 5084c802..f3b6ee41 100644 --- a/config/database.php +++ b/config/database.php @@ -48,8 +48,8 @@ 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), - 'database' => env('DB_DATABASE', 'forge'), - 'username' => env('DB_USERNAME', 'forge'), + 'database' => env('DB_DATABASE', 'laravel'), + 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php index 547152f6..d0c808b5 100644 --- a/tests/CreatesApplication.php +++ b/tests/CreatesApplication.php @@ -3,15 +3,14 @@ namespace Tests; use Illuminate\Contracts\Console\Kernel; +use Illuminate\Foundation\Application; trait CreatesApplication { /** * Creates the application. - * - * @return \Illuminate\Foundation\Application */ - public function createApplication() + public function createApplication(): Application { $app = require __DIR__.'/../bootstrap/app.php'; @@ -19,4 +18,4 @@ public function createApplication() return $app; } -} +} \ No newline at end of file diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index cdb51119..91163c30 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -2,20 +2,18 @@ namespace Tests\Feature; -use Illuminate\Foundation\Testing\RefreshDatabase; +// use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class ExampleTest extends TestCase { /** * A basic test example. - * - * @return void */ - public function testBasicTest() + public function test_the_application_returns_a_successful_response(): void { $response = $this->get('/'); - $response->assertStatus(200); + $response->assertOk(); } -} +} \ No newline at end of file diff --git a/tests/Feature/ProductTest.php b/tests/Feature/ProductTest.php new file mode 100644 index 00000000..f7361315 --- /dev/null +++ b/tests/Feature/ProductTest.php @@ -0,0 +1,35 @@ + 'user', + 'description' => 'user', + 'image' => '/public/images/img-placeholder.jpg', + 'barcode' => 'user123', + 'price' => 2, + 'quantity' => 2, + 'status' => true, + + + ]; + + // Act: Make a POST request to the registration endpoint + $response = $this->postJson('/admin/products/create', $data); + $response->assertStatus(201); + + } +} diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php new file mode 100644 index 00000000..9ed6fbc2 --- /dev/null +++ b/tests/Feature/UserTest.php @@ -0,0 +1,32 @@ + 'John Doe', + 'last_name' => 'John Doe', + 'email' => 'user@gmail.com', + 'password' => 'password123', + 'password_confirmation' => 'password123', + + ]; + + $response = $this->postJson('/register', $data); + + $response->assertStatus(201); + + $this->assertDatabaseHas('users', [ + 'email' => 'user@gmail.com', + ]); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a6..015e9f33 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,4 +7,4 @@ abstract class TestCase extends BaseTestCase { use CreatesApplication; -} +} \ No newline at end of file diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/Product.php similarity index 50% rename from tests/Unit/ExampleTest.php rename to tests/Unit/Product.php index 358cfc88..b3864e92 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/Product.php @@ -4,14 +4,12 @@ use PHPUnit\Framework\TestCase; -class ExampleTest extends TestCase +class Product extends TestCase { /** - * A basic test example. - * - * @return void + * A basic unit test example. */ - public function testBasicTest() + public function test_example(): void { $this->assertTrue(true); } diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php new file mode 100644 index 00000000..e69de29b diff --git a/yarn.lock b/yarn.lock index 145b80e2..731ea4a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1502,10 +1502,10 @@ es6-symbol@3.1.1: d "1" es5-ext "~0.10.14" -esbuild-darwin-arm64@0.15.10: +esbuild-windows-64@0.15.10: version "0.15.10" - resolved "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.15.10.tgz" - integrity sha512-M1t5+Kj4IgSbYmunf2BB6EKLkWUq+XlqaFRiGOk8bmBapu9bCDrxjf4kUnWn59Dka3I27EiuHBKd1rSO4osLFQ== + resolved "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-0.15.10.tgz" + integrity sha512-2H0gdsyHi5x+8lbng3hLbxDWR7mKHWh5BXZGKVG830KUmXOOWFE2YKJ4tHRkejRduOGDrBvHBriYsGtmTv3ntA== esbuild@^0.15.9: version "0.15.10" @@ -1736,11 +1736,6 @@ fs-extra@^10.0.0: jsonfile "^6.0.1" universalify "^2.0.0" -fsevents@~2.3.1, fsevents@~2.3.2: - version "2.3.2" - resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz" - integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA== - fullcalendar@^5.10.1: version "5.11.3" resolved "https://registry.npmjs.org/fullcalendar/-/fullcalendar-5.11.3.tgz" From 7866c75249b39677990e6a4866f3a5247d536dfd Mon Sep 17 00:00:00 2001 From: Kader1680 Date: Mon, 2 Dec 2024 20:07:12 +0100 Subject: [PATCH 2/4] test login operation --- app/Models/User.php | 6 +++++ tests/Feature/UserTest.php | 54 ++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index b49f2c05..fdf4a53b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Hash; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable @@ -52,4 +53,9 @@ public function getAvatar() { return 'https://www.gravatar.com/avatar/' . md5($this->email); } + + public function login($password) + { + return Hash::check($password, $this->password); + } } diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 9ed6fbc2..66e33ef0 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -2,15 +2,17 @@ namespace Tests\Feature; +use App\Models\User; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Hash; class UserTest extends TestCase { use RefreshDatabase; // This will ensure that the database is migrated before each test /** @test */ - public function a_user_can_register() + public function test_user_can_register() { $data = [ 'first_name' => 'John Doe', @@ -18,15 +20,57 @@ public function a_user_can_register() 'email' => 'user@gmail.com', 'password' => 'password123', 'password_confirmation' => 'password123', - ]; - $response = $this->postJson('/register', $data); - + $response->assertStatus(201); - + $this->assertDatabaseHas('users', [ 'email' => 'user@gmail.com', ]); + + } + + public function test_user_can_login_with_valid_credentials() + { + // Create a user instance + $user = User::create([ + 'first_name' => 'aze', + 'last_name' => 'aze', + 'email' => 'aze@email.com', + 'password' => Hash::make('123'), + ]); + + // Try logging in with an empty password + $this->assertTrue($user->login('123')); // Should return false + } + + + public function test_login_unsuccessful_wrong_password() + { + $user = User::create([ + 'first_name' => 'aze', + 'last_name' => 'aze', + 'email' => 'aze@email.com', + 'password' => Hash::make('123'), + ]); + + // Try logging in with an incorrect password + $this->assertFalse($user->login('wrongpassword')); // Should return false + } + + public function test_login_unsuccessful_empty_password() + { + $user = User::create([ + 'first_name' => 'aze', + 'last_name' => 'aze', + 'email' => 'aze@email.com', + 'password' => Hash::make('123'), + ]); + + // Try logging in with an incorrect password + $this->assertFalse($user->login('')); // Should return false } + + } From 8deb5e47cf4d20e56f5c0d8b998e473bdf643983 Mon Sep 17 00:00:00 2001 From: Kader1680 Date: Tue, 3 Dec 2024 02:46:41 +0100 Subject: [PATCH 3/4] validate request add customer --- app/Http/Controllers/CustomerController.php | 2 +- app/Http/Requests/CustomerStoreRequest.php | 6 +- app/Models/User.php | 3 + tests/Feature/CustomerTest.php | 61 +++++++++++++++++++++ tests/Feature/UserTest.php | 14 ++++- 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/CustomerTest.php diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index b3872f32..9ad2d325 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -94,7 +94,7 @@ public function edit(Customer $customer) * @param \App\Models\Customer $customer * @return \Illuminate\Http\Response */ - public function update(Request $request, Customer $customer) + public function update(CustomerStoreRequest $request, Customer $customer) { $customer->first_name = $request->first_name; $customer->last_name = $request->last_name; diff --git a/app/Http/Requests/CustomerStoreRequest.php b/app/Http/Requests/CustomerStoreRequest.php index d695ad6c..54fc3ef0 100644 --- a/app/Http/Requests/CustomerStoreRequest.php +++ b/app/Http/Requests/CustomerStoreRequest.php @@ -26,10 +26,10 @@ public function rules() return [ 'first_name' => 'required|string|max:20', 'last_name' => 'required|string|max:20', - 'email' => 'nullable|email', - 'phone' => 'nullable|string|max:20', + 'email' => 'required|email', + 'phone' => 'nullable|integer', 'address' => 'nullable|string', - 'avatar' => 'nullable|image', + 'avatar' => 'nullable|string', ]; } } diff --git a/app/Models/User.php b/app/Models/User.php index fdf4a53b..278b68e7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -58,4 +58,7 @@ public function login($password) { return Hash::check($password, $this->password); } + + + } diff --git a/tests/Feature/CustomerTest.php b/tests/Feature/CustomerTest.php new file mode 100644 index 00000000..81ec3942 --- /dev/null +++ b/tests/Feature/CustomerTest.php @@ -0,0 +1,61 @@ + 'aze', + // 'last_name' => 'aze', + // 'email' => 'aze@gmail.com', + // 'phone' => 456789, + // 'address' => 'myhope', + // 'avatar' => '/public/images/logo.png', + + // ]); + + // $admin = User::create([ + // 'first_name' => 'z', + // 'last_name' => 'z', + // 'email' => 'admin@gmail.com', + // 'password' => bcrypt('admin123'), // or use an appropriate admin password + // ]); + + // // Acting as the admin user + // $this->actingAs($admin); + + + $response = $this->postJson('/admin/customers', [ + 'first_name' => 'aze', + 'last_name' => 'aze', + 'email' => 'aze@gmail.com', + 'phone' => 456789, + 'address' => 'myhope', + 'avatar' => 'logo.png', + + ]); + + $response->assertStatus(201); + + $response->assertDatabaseHas('customers', [ + 'email' => 'aze@gmail.com' + ]); + + + } +} diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 66e33ef0..6f413655 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -69,8 +69,20 @@ public function test_login_unsuccessful_empty_password() ]); // Try logging in with an incorrect password - $this->assertFalse($user->login('')); // Should return false + $this->assertFalse($user->login(' ')); // Should return false } + // public function test_email_must_be_unique(){ + // $user = User::create([ + // 'first_name' => 'aze', + // 'last_name' => 'aze', + // 'email' => 'aze@email.com', + // 'password' => Hash::make('123'), + // ]); + + // $this->assertFalse($user->login('')); // Should return false + + // } + } From ec2471ee917d787479d2747ee3d8c0d52e55857a Mon Sep 17 00:00:00 2001 From: Kader1680 Date: Sat, 4 Jan 2025 17:29:40 +0100 Subject: [PATCH 4/4] add change --- app/Http/Controllers/HomeController.php | 7 ++++ app/Models/Customer.php | 3 ++ database/factories/CustomerFactory.php | 46 +++++++++++++++++++------ database/factories/UserFactory.php | 3 +- routes/web.php | 4 +++ tests/Feature/CustomerTest.php | 31 +++++------------ tests/Feature/UserTest.php | 10 +++--- 7 files changed, 64 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 83131572..b4c6c541 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -28,7 +28,10 @@ public function index() $orders = Order::with(['items', 'payments'])->get(); $customers_count = Customer::count(); + return view('home', [ + + 'orders_count' => $orders->count(), 'income' => $orders->map(function($i) { if($i->receivedAmount() > $i->total()) { @@ -43,6 +46,10 @@ public function index() return $i->receivedAmount(); })->sum(), 'customers_count' => $customers_count + + + + ]); } } diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 2af8677d..7d60a6ab 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -4,9 +4,12 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Storage; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Customer extends Model { + + use HasFactory; protected $fillable = [ 'first_name', 'last_name', diff --git a/database/factories/CustomerFactory.php b/database/factories/CustomerFactory.php index 0d4296e8..070a6990 100644 --- a/database/factories/CustomerFactory.php +++ b/database/factories/CustomerFactory.php @@ -2,15 +2,39 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ +// use App\Models\Customer; +// use Faker\Generator as Faker; + +// $factory->define(Customer::class, function (Faker $faker) { +// return [ +// 'first_name' => $faker->firstName, +// 'last_name' => $faker->lastName, +// 'email' => $faker->unique()->safeEmail, +// 'phone' => $faker->phoneNumber, +// 'address' => $faker->address, +// ]; +// }); + + +namespace Database\Factories; + use App\Models\Customer; -use Faker\Generator as Faker; - -$factory->define(Customer::class, function (Faker $faker) { - return [ - 'first_name' => $faker->firstName, - 'last_name' => $faker->lastName, - 'email' => $faker->unique()->safeEmail, - 'phone' => $faker->phoneNumber, - 'address' => $faker->address, - ]; -}); +use Illuminate\Database\Eloquent\Factories\Factory; + +class CustomerFactory extends Factory +{ + // Define the associated model + protected $model = Customer::class; + + // Define the default state for your model + public function definition() + { + return [ + 'first_name' => $this->faker->firstName(), + 'last_name' => $this->faker->lastName(), + 'email' => $this->faker->unique()->safeEmail(), + 'phone' => $this->faker->phoneNumber(), + 'address' => $this->faker->address(), + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 741edead..5ea04340 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,7 +2,8 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ -use App\User; +use App\Models\User; +// use App\User; use Faker\Generator as Faker; use Illuminate\Support\Str; diff --git a/routes/web.php b/routes/web.php index 35884ea0..eac06b0d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -16,11 +16,15 @@ Auth::routes(); Route::prefix('admin')->middleware('auth')->group(function () { + Route::get('/', [HomeController::class, 'index'])->name('home'); + + Route::get('/settings', [SettingController::class, 'index'])->name('settings.index'); Route::post('/settings', [SettingController::class, 'store'])->name('settings.store'); Route::resource('products', ProductController::class); Route::resource('customers', CustomerController::class); + Route::resource('orders', OrderController::class); Route::get('/cart', [CartController::class, 'index'])->name('cart.index'); diff --git a/tests/Feature/CustomerTest.php b/tests/Feature/CustomerTest.php index 81ec3942..6138df6e 100644 --- a/tests/Feature/CustomerTest.php +++ b/tests/Feature/CustomerTest.php @@ -4,10 +4,12 @@ use App\Models\Customer; use App\Models\User; + use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; + class CustomerTest extends TestCase { /** @@ -18,40 +20,23 @@ class CustomerTest extends TestCase public function test_add_new_customer_succesfully(): void { - // $newCustomer = Customer::create('/admin/customers', [ - - // 'first_name' => 'aze', - // 'last_name' => 'aze', - // 'email' => 'aze@gmail.com', - // 'phone' => 456789, - // 'address' => 'myhope', - // 'avatar' => '/public/images/logo.png', - - // ]); - // $admin = User::create([ - // 'first_name' => 'z', - // 'last_name' => 'z', - // 'email' => 'admin@gmail.com', - // 'password' => bcrypt('admin123'), // or use an appropriate admin password - // ]); + // Create a user (you may want to create an admin user with specific permissions) + $user = User::factory()->create(); + - // // Acting as the admin user - // $this->actingAs($admin); - - - $response = $this->postJson('/admin/customers', [ + // Act as the created user and send the POST request + $response = $this->actingAs($user)->postJson('/admin/customers', [ 'first_name' => 'aze', 'last_name' => 'aze', 'email' => 'aze@gmail.com', 'phone' => 456789, 'address' => 'myhope', 'avatar' => 'logo.png', - ]); + // Assert the correct status code and check the database $response->assertStatus(201); - $response->assertDatabaseHas('customers', [ 'email' => 'aze@gmail.com' ]); diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 6f413655..3dbf6567 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -35,14 +35,14 @@ public function test_user_can_login_with_valid_credentials() { // Create a user instance $user = User::create([ - 'first_name' => 'aze', - 'last_name' => 'aze', - 'email' => 'aze@email.com', - 'password' => Hash::make('123'), + 'first_name' => 'admin', + 'last_name' => 'admin', + 'email' => 'admin@email.com', + 'password' => Hash::make('admin123'), ]); // Try logging in with an empty password - $this->assertTrue($user->login('123')); // Should return false + $this->assertTrue($user->login('admin123')); // Should return false }