Download the PHP package make/accessible without Composer
On this page you can find all versions of the php package make/accessible. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download make/accessible
More information about make/accessible
Files in make/accessible
Package accessible
Short Description A very lightweight PHP package that let you easely access and test singleton classes and inaccessible instance members like private or protected methods and properties.
License MIT
Homepage https://github.com/e200/MakeAccessible
Informations about the package accessible
Make/Accessible
Lightweight PHP package that let you test singleton classes and inaccessible (private or protected) instance members.
Table of contents
- Installation
- Features
- Usage
- Best practices
- Support
- Contribute
- Credits
- License
Installation
composer require make/accessible
Features
- Call inaccessible methods
- Set values into inaccessible properties
- Get values from inaccessible properties
- Instantiate singleton classes
Usage
Suppose you have the class bellow:
This class is correct, it does'nt exposes its inner elements (encapsulation) and was designed to do what is supposed to do.
But, how are you going to test this class? Test if the addPersonToGreet()
method is really adding $person
into $peopleToGreet
and greet()
method is really distinguishing mens from womens?
You can just make all your class members public, but that way you're breaking encapsulation, thats not good.
So, what can be done here?
Solutions
One way to test addPersonToGreet()
would be by directly accessing $peopleToGreet
and check if $person
is really there:
One way to test greet()
would be by directly accessing it:
But of course these methods will fail, its will result in:
Error: Cannot access protected property PeopleGreeter::$peopleToGreet.
Error: Cannot access protected method PeopleGreeter::greet().
because we can't access protected and private members outside of they parent scope.
Another way that works only with protected members is by extending their parent to another class that exposes its methods:
Pros:
- You can test protected members.
Cons:
- Don't works with private members.
- Makes really hard write simples tests.
- You need to write a fake class every time you want to test a real class.
- You need to write a method for each inaccessilble member you want to test.
But of course there's another solution: Using PHP Reflection:
Pros:
- Works for both protected and private members.
Cons:
- You need to write a lot of extra code to make simple tests.
- You need to write code that also need to be tested.
- Its hard to reuse this code in other test either in other projects.
We're testing only 2 methods, now think about 30 or 50??? No, no... Forget about it!
Using Make/Accessible
Did you saw that??? Just one line of code and we made our tests!!!
We gain access to our PeopleGreeter::class
inaccessible members in a common and friendly way! ;)
Pros:
- User friendly.
- Just one line of code.
- Makes tests really easy.
- Works for both private and protected members.
- Don't require write fake classes or methods for each test.
- Encourages the usage of encapsulation in projects providing a more isolated and flexible environment.
Cons:
- :sweat_smile:
As you can see, with Make/Accessible you can now make your projects encapsulated withour fear!
And you can also start testing singleton classes using Make/Accessible. Feature documentation soon.
Best practices
We highly recommend the use of this package for tests purposes only.
Avoid use this package to gain access to encapsulated classes, since it's like break the door of someone's house that does'nt want you to get inside.
If you're testing the same class the same way, we recommend create a function getAccessibleInstance()
at the bottom of your test class and there you make the instantiation, mocks, everything you need to instantiate the class you need to test:
For more tips about best practices, please, read our best practices documentation.
Support
Need a new feature? Found a bug? Please open a new issue or send me an email and we'll fix it as soon as possible.
Contribute
Feel free to contribute forking, making changes and pull requests.
Credits
- Eleandro Duzentos and contributors.
License
The MakeAccessible is licensed under the MIT license. See the license file for more information.