آخر تابستونت رو متفاوت کن 🌟 ۳۰٪ تخفیف + هدیه ۳ میلیونی 🎁😉
۰ ثانیه
۰ دقیقه
۰ ساعت
۳ Ashkan ahs
خطای 500 در تست
جامعه برنامه نویسی وب ایجاد شده در ۰۳ آبان ۱۴۰۲

سلام وقت بخیر

من تستام وقتی construct رو می‌نویسم پاس نمی‌شن بقیه پاسخ‌هارو هم چک کردم ولی مشکلم بر طرف نشد 

ممنون میشم راهنماییم کنید 

https://github.com/a-ahs/7azmoon

user controller

<?php
    namespace App\Http\Controllers\API\V1;
    use App\Http\Controllers\Controller;
    use App\Repositories\Contracts\userRepositoryInterface;
    use Illuminate\Http\Request;
    class usersController extends Controller
    {
        public function __construct(private userRepositoryInterface $userRepository)
        {
        }
        public function store(Request $request)
        {
            $this->validate($request, [
                'full_name' => 'required|string',
                'email' => 'required|string',
                'mobile' => 'required|string',
                'password' => 'required'
            ]);
            $this->userRepository->create($request->toArray());
            return response()->json([
                'success' => true,
                'message' => 'کاربر با موفقیت ایجاد شد',
                'data' => [
                    'full_name' => $request->full_name,
                    'email' => $request->email,
                    'mobile' => $request->mobile,
                    'password' => $request->password    
                ]
            ])->setStatusCode(201);
        }
    }
?>

 

test 

<?php
    namespace API\V1\Users;
    use Tests\TestCase;
    class UserTest extends TestCase
    {
        public function testCreateNewUser()
        {
            $response = $this->call('POST', 'api/v1/users', [
                'full_name' => 'ashkan',
                'email' => 'ashkan@gmail.com',
                'mobile' => '09111111111',
                'password' => 'ashkan123'
            ]);
            $this->assertEquals(201, $response->status());
            $this->seeJsonStructure([
                'success',
                'message',
                'data' => [
                    'full_name',
                    'email',
                    'mobile',
                    'password'
                ]
            ]);
        }
        public function testSendWrongData()
        {
            $response = $this->call('POST', 'api/v1/users', []);
            $this->assertEquals(422, $response->status());
        }
    }
?>

json base repository

<?php
    namespace   App\Repositories\Json;
    use App\Repositories\Contracts\repositoryInterface;
    class jsonBaseRepository implements repositoryInterface
    {
        protected $model;
        public function create(array $data)
        {
            return file_put_contents('users.json', json_encode($data));
        }
        public function update(int $id, array $data)
        {
        }
        public function delete(array $where)
        {
        }
        public function all(array $where)
        {
        }
        public function find(int $id)
        {
        }
    }
?>

 

app services provide

<?php
namespace App\Providers;
use App\Repositories\Contracts\userRepositoryInterface;
use App\Repositories\Eloquent\eloquentUserRepository;
use App\Repositories\Json\jsonUserRepository;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
    public function boot()
    {
        $this->app->bind(userRepositoryInterface::class, jsonUserRepository::class);
    }
}

 

repository interface

<?php
    namespace App\Repositories\Contracts;
    interface repositoryInterface
    {
        public function create(array $data);
        public function update(int $id, array $data);
        public function delete(array $where);
        public function all(array $where);
        public function find(int $id);
    }
?>

 

user repository interface

<?php
    namespace App\Repositories\Contracts;
    interface userRepositoryInterface extends repositoryInterface
    {
    }
?>

 

Ashkan ahs ۰۴ آبان ۱۴۰۲، ۰۹:۰۱

خطای خاصی رو دریافت میکنید؟ اگه دریافت میکنید لطفا اونو هم قرار بدید
تست هاتون رو به این شکل هم میتونید بنویسید


 

 

public function testCreateNewUser()
{
    $response = $this->post('api/v1/users', [
        'full_name' => 'ashkan',
        'email' => 'ashkan@gmail.com',
        'mobile' => '09111111111',
        'password' => 'ashkan123'
    ]);
    $response->assertStatus(201);
    $response->assertJsonStructure([
        'success',
        'message',
        'data' => [
            'full_name',
            'email',
            'mobile',
            'password'
        ]
    ]);
}
public function testSendWrongData()
{
    $response = $this->post('api/v1/users', []);
    $response->assertStatus(422);
    $response->assertJsonValidationErrors(['full_name', 'email', 'mobile', 'password']);
}
امیر صالحی ۰۵ آبان ۱۴۰۲، ۱۸:۰۲
Capture-2IaA.png
Ashkan ahs ۰۶ آبان ۱۴۰۲، ۱۳:۵۱