溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB與PHP的集成測試案例

發(fā)布時間:2024-09-12 15:16:55 來源:億速云 閱讀:78 作者:小樊 欄目:編程語言

在這個案例中,我們將使用PHP和MongoDB進行集成測試。我們將創(chuàng)建一個簡單的用戶注冊和登錄系統(tǒng),包括用戶數(shù)據(jù)的增刪改查操作。

首先,確保已經(jīng)安裝了以下軟件:

  1. PHP(推薦版本7.4或更高)
  2. MongoDB(推薦版本4.4或更高)
  3. Composer(PHP依賴管理工具)

接下來,我們需要安裝MongoDB的PHP擴展。在命令行中運行以下命令:

composer require mongodb/mongodb

現(xiàn)在我們可以開始編寫代碼了。首先,創(chuàng)建一個名為UserRepository.php的文件,用于處理用戶數(shù)據(jù)的增刪改查操作:

<?php

require 'vendor/autoload.php';

use MongoDB\Client as Mongo;

class UserRepository
{
    private $collection;

    public function __construct()
    {
        $mongo = new Mongo("mongodb://localhost:27017");
        $db = $mongo->selectDatabase("user_database");
        $this->collection = $db->selectCollection("users");
    }

    public function createUser($username, $password)
    {
        $user = [
            "username" => $username,
            "password" => password_hash($password, PASSWORD_BCRYPT),
        ];

        $result = $this->collection->insertOne($user);

        return $result->getInsertedId();
    }

    public function getUserByUsername($username)
    {
        return $this->collection->findOne(["username" => $username]);
    }

    public function updateUser($userId, $data)
    {
        $result = $this->collection->updateOne(
            ["_id" => new \MongoDB\BSON\ObjectId($userId)],
            ['$set' => $data]
        );

        return $result->getModifiedCount();
    }

    public function deleteUser($userId)
    {
        $result = $this->collection->deleteOne(["_id" => new \MongoDB\BSON\ObjectId($userId)]);

        return $result->getDeletedCount();
    }
}

接下來,創(chuàng)建一個名為UserController.php的文件,用于處理用戶注冊和登錄的邏輯:

<?php

require 'UserRepository.php';

class UserController
{
    private $userRepository;

    public function __construct()
    {
        $this->userRepository = new UserRepository();
    }

    public function register($username, $password)
    {
        if ($this->userRepository->getUserByUsername($username)) {
            throw new Exception("Username already exists.");
        }

        $userId = $this->userRepository->createUser($username, $password);

        return $userId;
    }

    public function login($username, $password)
    {
        $user = $this->userRepository->getUserByUsername($username);

        if (!$user || !password_verify($password, $user["password"])) {
            throw new Exception("Invalid username or password.");
        }

        return $user;
    }
}

最后,創(chuàng)建一個名為index.php的文件,用于測試注冊和登錄功能:

<?php

require 'UserController.php';

$userController = new UserController();

// 注冊
$userId = $userController->register("testuser", "testpassword");
echo "User registered with ID: " . $userId . "\n";

// 登錄
$user = $userController->login("testuser", "testpassword");
echo "User logged in with ID: " . $user["_id"] . "\n";

現(xiàn)在,運行index.php文件以測試注冊和登錄功能:

php index.php

如果一切正常,你應該會看到類似以下的輸出:

User registered with ID: 60a8d4a15c1e000011004491
User logged in with ID: 60a8d4a15c1e000011004491

這個簡單的案例展示了如何在PHP和MongoDB之間進行集成測試。你可以根據(jù)自己的需求對其進行擴展和優(yōu)化。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

php
AI