Git – 重新設定遠端 Repository

有時候我們會替換遠端倉儲位置,例如要移轉的時候,那就須要重新指定

# 檢查原本的位置
git remote -v

# 替換到新的位置
git remote set-url origin https://github.com/{user}/{repository-name}.git

# 檢查是否真的替換到新的位置
git remote -v

# 測試從遠端拉取,沒有錯誤就代表正常了
git pull

Git – GitHub 出現 「Please make sure you have the correct access rights and the repository exists.」

接手專案維護的時候,準備要 git pull 卻出現錯誤

案例一 2023/08/08

如果你看到

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

那代表你沒有存取 repository 的權限,可以觀察

git remote -v

# 如果出現類似以下格式,代表過去的連線使用 ssh,那麼將繼續使用
origin  git@github.com:fdjkgh580/laradock-with-mysql.git (fetch)
origin  git@github.com:fdjkgh580/laradock-with-mysql.git (push)

# 如果出現類似以下格式,代表過去的連線透過 http 的方式連線,應該趁這次換掉。
origin	https://github.com/{username}/{repo}.git (fetch)
origin	https://github.com/{username}/{repo}.git (push)

以現在的 Github 來說已經強烈建議改為 ssh 連線方式,因此不再建議使用輸入 Github 的個人帳號密碼來使用。請參考我這篇取解決這個問題:

Github – 使用 git push 使用 ssh key 免帳號密碼

案例二 不建議的方式

ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

這是因為使用 ssh 連線到 GitHub 但可能出現 key 遺失或遭受更改的關係,如果不是要自動化處理,我們可以替換成詢問 username 與 password 的模式來解決這個困擾。

# 先確定目前遠端位置
git remote -v

# 如果出現這樣,代表前人用的是 SSH 連線方式
origin	git@github.com:{username}/{repo}.git (fetch)
origin	git@github.com:{username}/{repo}.git (push)

# 打這行會不給修改
git remote add origin https://github.com/{username}/{repo}.git

# 會說已經存在
fatal: remote origin already exists.

# 所以要打這行,修改為走 https 通道的詢問模式
git remote set-url origin https://github.com/{username}/{repo}.git

# 接著確認一下
git remote -v

# 代表成功替換
origin	https://github.com/{username}/{repo}.git (fetch)
origin	https://github.com/{username}/{repo}.git (push)

# 接著 pull 
git pull

# 就會詢問我們 GitHub 帳號密碼了
Username for 'https://github.com':

參考來源

SQL – 製作分頁會用到的取得所有列表總數

通常我們要取得第一頁前 10 筆

select *
from articles
limit 0, 10;

但製作分頁,會需要得知總數量好讓 PHP 中顯示分頁總數按鈕。如果我們又下第二次 count() 去統計那就太麻煩了。所以我們可以搭配使用 SQL_CALC_FOUND_ROWS 忽略 offset / limit 統計總數量,例如第一次我們下

select SQL_CALC_FOUND_ROWS *
from articles
limit 0, 10;

要注意 SQL_CALC_FOUND_ROWS 後方不佳半形逗號。接下來下指令

SELECT FOUND_ROWS();

就會取得第一次條件並忽略 offset / limit 的總數量了。

參考網友

phpUnit – 如何測試類別中的保護或私有方法

通常在 phpUnit 中, 為了避免過度設計,要測試的方法都是 Class 的公開方法。但有時候我們要對受保護的方法或私有方法、甚至是屬性作測試,那該如何測試寫?

我們可以藉由 「ReflectionClass 反射類別 / ReflectionMethod 反射方法」 來達到,以下是 phpUnit 運行在路徑 tests/ 底下的範例

<?php

namespace Tests;

use PHPUnit\Framework\TestCase;

// 受測範例
class User
{
    protected static function getPhone($name)
    {
        return "{$name}: 09123456789";
    }

    private function search($id, $name)
    {
        return "{$id} - {$name}";
    }
}

// 測試 User 類別
class MyTest extends TestCase
{
    // 測試靜態保護的方法
    public function testProtectedStaticMethod()
    {
        // 直接反射方法
        $reflectionMethod = new \ReflectionMethod(User::class, 'getPhone');
        $reflectionMethod->setAccessible(true);

        // 代入該方法的參數
        $result = $reflectionMethod->invoke(new User(), "Cary");
        $this->assertEquals("Cary: 09123456789", $result);
    }

    // 測試私有方法
    public function testPrivateMethod()
    {
        // 直接反射方法
        $reflectionMethod = new \ReflectionMethod(User::class, 'search');
        $reflectionMethod->setAccessible(true);

        // 代入該方法的參數
        $result = $reflectionMethod->invoke(new User(), "500800", "Cary");
        $this->assertEquals("500800 - Cary", $result);
    }

    // 混和測試
    public function testMix()
    {
        // 只反射類別
        $reflectionClass = new \ReflectionClass(User::class);

        // 從反射的類別下去取得靜態方法
        $method = $reflectionClass->getMethod('getPhone');
        $method->setAccessible(true);

        // 為該方法帶入參數
        $invoke = $method->invoke(new User(), 'Cary');

        // 從反射的類別下去取得方法
        $method = $reflectionClass->getMethod('search');
        $method->setAccessible(true);

        // 為該方法帶入參數
        $invoke = $method->invoke(new User(), '500800', 'Cary');
    }

}

至於何時用 ReflectionClass 或 ReflectionMethod,如果要同時測試 Class 底下多個 Methods,那麼使用 ReflectionClass 會方便一點;如果只是單純測試某個 Methods 那麼就使用 ReflectionMethod 。

參考來源

JavaScript – 產生亂數的方法

方法一

function makeid(length) {
   var result           = '';
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   return result;
}

console.log(makeid(5));

方法二

let r = Math.random().toString(36).substring(7);
console.log("random", r);

參考來源