お知らせ

現在サイトのリニューアル作業中のため、全体的にページの表示が乱れています。
投稿日:
言語::PHP

PHPでClassをrequireせずに使えるやつ
Laravelのrequest()とかがどうやって呼ばれてるのかを調べていくうちに辿り着いたのでメモがてら

Classは簡単に読み込めるけど、Functionは一筋縄ではいかなさそう
ついでにClassに好き勝手プロパティ増やせることも発見した

autoloaderを実装

spl_autoload_registerを使う。Laravelはcomposerが上手いことやってくれてるっぽかった

<?
function regist() {
    spl_autoload_register(function() {
        require './Hoge.php';
        require './Piyo.php';
    });
}

使いたいクラス

適当に用意

<?
class Hoge {
    public $hoge;
}
<?
class Piyo {
    public $piyo;
}

autoloaderを呼んで使う

ついでに好き勝手にプロパティも生やす

<?
require './bootstrap.php';
// ここでクラスをautoloadする
regist();

// どこからも呼んでないけど
$h = new Hoge;
// 生やせる
$h->fuga = "aaa";
$h->hogepiyo = "bbb";

// 使える
$p = new Piyo;
$p->fuga = "aaa";
$p->hogepiyo = "bbb";

var_dump($h, $p);

/*
結果

class Hoge#2 (3) {
  public $hoge =>
  NULL
  public $fuga =>
  string(3) "aaa"
  public $hogepiyo =>
  string(3) "bbb"
}

class Piyo#3 (3) {
  public $piyo =>
  NULL
  public $fuga =>
  string(3) "aaa"
  public $hogepiyo =>
  string(3) "bbb"
}

*/
投稿日:
言語::PHP

手順

  1. composer initでプロジェクトを作成
  2. composer require [パッケージ名]でパッケージを入れる
  3. 名前空間を作る場合、 composer.jspnに以下のセクションを作成
    1. リファレンス: getcomposer.org
"autoload": {
    "psr-4": {
        "App\\": "src/App" // 名前空間と対応するパス
    },
    "classmap": [
        "src/App" // クラスを読み込むディレクトリのルート
    ]
}
  1. autoloadclass_aliasなどを設定するbootstrap.phpを適当に作成
    1. このbootstrap.phpはエントリポイントからrequire_onceなどで読み込無事で有効化する
  2. phpunitを組み込む場合
    1. composer require --dev phpunit/phpunitでインストール
    2. composer.jsonに以下のセクションを作成
    "scripts": {
        "test": [
            "phpunit --bootstrap bootstrap.php test"
        ]
    }
投稿日:
言語::JavaScript::CommonJSNode.js::Jest開発::テスト

確認環境

Env Ver
Node.js 12.18.3
Jest 26.4.2

やりたいこと

以下の実装のときに、parentFunc()を呼んだ時にchildFunc()が呼ばれることをテストしたい

function parentFunc() {
  console.log('called parentFunc');
  childFunc('XXXX');
}

function childFunc(param) {
  console.log(`called childFunc ${param}`);
}

各ケース紹介

Case1 そもそも構文がおかしい

テストするためには関数をexportする必要あるが、愚直過ぎて構文的に実行不能になるケース

exports.parentFunc = () => {
  console.log('called parentFunc');
  // childFuncは別モジュールなので呼べない
  childFunc('XXXX');
};

exports.childFunc = (param) => {
  console.log(`called childFunc ${param}`);
};

Case2 スコープ違いでテストが失敗する

この実装を実行すると期待通り動作するので、一見すると大丈夫そうに見える

function parentFunc() {
  console.log('called parentFunc');
  childFunc('XXXX');
}

function childFunc(param) {
  console.log(`called childFunc ${param}`);
}

module.exports = { parentFunc, childFunc };

しかしこのテストを流すと失敗する
これはparentFunc()が呼び出すchildFunc()が下記case2の中にないため
parentFunc()のスコープ内にchildFunc()がいないことが原因

const case2 = require('./case2');
// こうするとjest.spyOn()の第一引数を満たせないので落ちる
// const { parentFunc, childFunc } = require('./case2');

describe('inside call test', function () {
  it('parentFunc', function () {
    const spy = jest.spyOn(case2, 'parentFunc');
    case2.parentFunc();
    expect(spy).toHaveBeenCalled();
  });
  it('childFunc', function () {
    const spy = jest.spyOn(case2, 'childFunc');
    case2.parentFunc();
    // childFuncはcase2に属していないため呼ばれない
    expect(spy).toHaveBeenCalled();
  });
});

Case3 テストが成功するケース

const parentFunc = () => {
  console.log('called parentFunc');
  // parentFunc()の中にchildオブジェクトを注入することで、
  // jestがchildFunc()を認識できるようにする
  child.childFunc('XXXX');
};

const child = {
  childFunc: (param) => {
    console.log(`called childFunc ${param}`);
  },
};

// childFuncでなく、childオブジェクトをexportするのが味噌
module.exports = { parentFunc, child };
const case3 = require('./case3');

describe('inside call test', function () {
  it('parentFunc', function () {
    const spy = jest.spyOn(case3, 'parentFunc');
    case3.parentFunc();
    expect(spy).toHaveBeenCalled();
  });
  it('childFunc', function () {
    // 注入している側のオブジェクトを参照する
    const spy = jest.spyOn(case3.child, 'childFunc');
    case3.parentFunc();
    // child.childFuncはcase3に属しているため呼ばれる
    expect(spy).toHaveBeenCalled();
  });
});

GitHub Pagesにドメインを紐付ける

# ルートドメイン, cnameだとメールが届かなくなる
alias @ example-user.github.io.
# サブドメイン
cname sub example-user.github.io.

メールサーバーは分けたい

メールサーバーがさくらインターネットの場合はこんな感じで行ける

mx xxx.xxx.xxx.xxx. 10
txt xxx.xxx.xxx.xxx. v=spf1 a:wwwXXXX.sakura.ne.jp mx ~all
a mail xxx.xxx.xxx.xxx

Let’s EncryptのDNS-01チャレンジの設定方法

  • サブドメインのケースで書いてる
  • ワイルドカード証明書の設定も確かできたはずだが、今ん所使える環境がないので書いてない
  • @lycolia/value-domain-dns-cert-register で自動更新できる
a sub xxx.xxx.xxx.xxx
txt _acme-challenge.sub XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX