LoginSignup
1
1

More than 5 years have passed since last update.

PHPでリフレクションはこうでしょうが!

Posted at

テストを書くときにprotected,privateなプロパティを書き換えたいときがあります。
でも、なかなかそらで書けないのです。僕は。

だから、自分への戒めとして書いておきます。
(そもそもアクセスできないテストってどうなの、、、ってのはありますが、、、)

リフレクションはこう!こう書く!!覚えろ!自分!!

        $reflection = new \ReflectionClass($Mailer);
        $property = $reflection->getProperty('oauth');
        $property->setAccessible(true);
        $property->setValue($Mailer, true);
        $this->assertTrue($Mailer->getOAuth());
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1