Git Product home page Git Product logo

dependency-injection-example's Introduction

Dependency Injection Example

Dependency injection doesn't have to be complicated. Let's work it out together!

What is it?

Dependency injection is a technique that makes it easier to manage the dependencies of your classes, and allow for better testing using the mocking technique. There are plenty of examples out there on the internet, but I found that many of them tend to overcomplicate the technique. This package contains a simple down-to-earth explanation of the concept, step-by-step and through the medium of code comments.

Who wrote it?

Hi! I'm Dayle Rees.

I'm an open source enthusiast, and a Laravel framework core developer. I'm also very fond of teaching. My books Code Bright and Code Happy have helped thousands of PHP web developers adopt the Laravel PHP framework.

My writing style is simple and plain spoken, just like this description!

How do I learn?

It's easy, see those page-x directories? Each of them represent our complete progress at a different point of time. Starting at part-1 we have an application in dire need of some dependency injection. Throughout the parts we discover difficulties, and develop resolutions.

To learn, you need only read the comments held within. First take a look at the architecture of the application within the part-x/src directory, and all comments held within the classes. It's not a big application, less than five files. Next look at the associated test files within the part-x/tests directory to learn more about the limitations of each stage of the applications development.

Don't forget to run phpunit from the part-x directory to examine the test results.

By the time you get to part 9, you will be a dependency injection master.

How does it work?

Each part of the repository contains a complete PHP application in various stages of its lifecycle. They each function independently. Class loading is provided by the composer project so you will find a composer.json stored within.

However, you will not need to have a copy of composer to run the tests. I've versioned the vendor directories along with each stage so that you can run the tests right away! Lucky you!

Our application files are psr-0 autoloaded (file structure and path matching namespace) from the part-x/src directories. All tests are located within the part-x/tests directory, and each stage contains a phpunit.xml bootstrap so that you can run the tests with only the phpunit command.

Any other advice?

Read through each stage carefully, noting the differences between previous stages. Normally these are outlined in the comments. Get to know the architecture, and visualize the problems.

I'd recommend cloning the repository down locally so you can view the source in a syntax-highlighting editor, and run the tests directly.

How can I thank you?

Why not buy my latest Laravel book? Laravel is a beautiful framework that makes excellent use of the dependency injection and inversion of control principles. You can get a copy here on Leanpub.

Don't want to part with cash? Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!

Don't forget to follow me on twitter!

Thanks!

dependency-injection-example's People

Contributors

anouarabdsslm avatar danbartram avatar danwall avatar daylerees avatar loic-sharma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dependency-injection-example's Issues

Expected vs Actual

Thanks to the great tutorials!
I noticed a mistake in your code

https://github.com/daylerees/dependency-injection-example/blob/master/part-9/tests/SocialFeedTest.php#L41

        $this->assertEquals($v[0], 'foo');
        $this->assertEquals($v[1], 'bar');
        $this->assertEquals($v[2], 'baz');
        $this->assertEquals($v[3], 'boo');
        $this->assertEquals($v[4], 'bop');

In PHPUnit (as well as all xUnit frameworks) the first argument in assertion is expected, and second is actual. So you should flip this code to look like:

        $this->assertEquals('foo', $v[0]);

part 8 - test for twitterfeed

Hi,
I'm newbie to TDD so my appologize for that question, but shouldn't we write test for Twitter like for Facebook?
public function testSocialFeedCanBeInstantiated() { $t = M::mock('Example\FeedReaders\FacebookFeedReader'); $s = new SocialFeed($t); }

public function testSocialFeedCanBeInstantiated2() { $t = M::mock('Example\FeedReaders\TwitterFeedReader'); $s = new SocialFeed($t); }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.