SlideShare a Scribd company logo
AspectMock
Making it easier to write unit tests
So, yeah, tests are important
“Test code is just as important as production code.”
“… tests enable change”“Code, without tests, is not clean. No matter how elegant it is, no
matter how readable and accessible, if it hath not tests, it be unclean.”
“All software you write will be tested – if not by you and
your team, then by the eventual users.”
“…legacy code is simply code without tests.”
“Code without tests is bad code. … With tests we can change the
behavior of our code quickly and verifiably. Without them, we
really don’t know if our code is getting better or worse.”
From Clean Code by Robert Martin, The Pragmatic Programmer by Andrew Hunt and David Thomas, and Working Effectively With Legacy Code by Michael Feathers
If you have trivial methods, tests are easy
Code Test
But who has trivial methods?
“They” say use Dependency Injection and mocks
Changing the
method’s
signature requires
refactoring all
method calls.
DI does nothing
for static
method calls
https://phpunit.de/manual/current/en/test-doubles.html
Or you could write integration tests …
Code Test
Even with integration tests, this
path cannot be tested because it
is a time-sensitive conditional
That’s a lot of work
Is there a third option?
A. Refactor all of your code to use Dependency Injections so you can mock dependencies.
B. Write intricate integration tests with heavy setup and teardown
C. ??
C. Use AspectMock
• Allows you to mock any method call (static, private, built-in PHP)
• Does not require refactoring existing code
• Installs via Composer (and configure an autoloader for tests)
• Can return a different mock value for each individual test
• Easy to use
• Magical
AspectMock mocks your code as it is
Code Test
Here’s the basic setup
• Import AspectMockTest
• In tearDown() method, run clean() to
reset the mocks between tests
• Define the class to be mocked, and the
desired results for each of its methods
• Write your test
Code
Want a different
value for each
test? Okay
Test
RealSample
TestCodeRealSample
So, how does it work?
• Relies on Aspect Oriented Programming
• Some concerns cut across classes (security, logging, etc.)
• Identify join points (method invocation, method return)
• Write code that provides advice at designated join points
• AOP code can target methods without cluttering those methods
• Uses PHP implementation of AOP called Go! AOP Framework
(http://go.aopphp.com)
Setup
1. Install via Composer
Be sure to include any path
you may mock, and exclude
all test-related paths
2. Create custom autoloader
3. Point phpunit to
special autoloader
Ready to try it for yourself?
Website: https://github.com/Codeception/AspectMock

More Related Content

What's hot

Unit Testing
Unit TestingUnit Testing
Unit Testing
François Camus
 
TDD
TDDTDD
Functional Tests Automation with Robot Framework
Functional Tests Automation with Robot FrameworkFunctional Tests Automation with Robot Framework
Functional Tests Automation with Robot Framework
laurent bristiel
 
Code review
Code reviewCode review
Code review
dqpi
 
Luis Correia - Instituto de Informática - OSL19
Luis Correia - Instituto de Informática - OSL19Luis Correia - Instituto de Informática - OSL19
Luis Correia - Instituto de Informática - OSL19
marketingsyone
 
Git branching policy and review comment's prefix
Git branching policy and review comment's prefixGit branching policy and review comment's prefix
Git branching policy and review comment's prefix
Kumaresh Chandra Baruri
 
Automatic Test 2019-07-25
Automatic Test 2019-07-25Automatic Test 2019-07-25
Automatic Test 2019-07-25
FedericoGuerinoni
 
Unit Testing Your Application
Unit Testing Your ApplicationUnit Testing Your Application
Unit Testing Your Application
Paladin Web Services
 
Code Review
Code ReviewCode Review
Code ReviewRavi Raj
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
 
Unit test
Unit testUnit test
Unit test
Tran Duc
 
Code Review
Code ReviewCode Review
Android tdd
Android tddAndroid tdd
Android tdd
Nhan Cao
 
Code Review Tool Evaluation
Code Review Tool EvaluationCode Review Tool Evaluation
Code Review Tool Evaluation
Kate Semizhon
 
PHPUnit with Magento
PHPUnit with MagentoPHPUnit with Magento
PHPUnit with Magento
Tu Hoang
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
Adam Culp
 
Unit tests benefits
Unit tests benefitsUnit tests benefits
Unit tests benefits
Kate Semizhon
 
Code Review
Code ReviewCode Review
Code Review
Lukas Rypl
 

What's hot (20)

Unit Testing
Unit TestingUnit Testing
Unit Testing
 
TDD
TDDTDD
TDD
 
Functional Tests Automation with Robot Framework
Functional Tests Automation with Robot FrameworkFunctional Tests Automation with Robot Framework
Functional Tests Automation with Robot Framework
 
Code review
Code reviewCode review
Code review
 
Unit testing with Jasmine
Unit testing with JasmineUnit testing with Jasmine
Unit testing with Jasmine
 
Luis Correia - Instituto de Informática - OSL19
Luis Correia - Instituto de Informática - OSL19Luis Correia - Instituto de Informática - OSL19
Luis Correia - Instituto de Informática - OSL19
 
Git branching policy and review comment's prefix
Git branching policy and review comment's prefixGit branching policy and review comment's prefix
Git branching policy and review comment's prefix
 
Automatic Test 2019-07-25
Automatic Test 2019-07-25Automatic Test 2019-07-25
Automatic Test 2019-07-25
 
Unit Testing Your Application
Unit Testing Your ApplicationUnit Testing Your Application
Unit Testing Your Application
 
Code Review
Code ReviewCode Review
Code Review
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testing
 
Unit test
Unit testUnit test
Unit test
 
Code Review
Code ReviewCode Review
Code Review
 
Android tdd
Android tddAndroid tdd
Android tdd
 
Code Review Tool Evaluation
Code Review Tool EvaluationCode Review Tool Evaluation
Code Review Tool Evaluation
 
PHPUnit with Magento
PHPUnit with MagentoPHPUnit with Magento
PHPUnit with Magento
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
Unit tests benefits
Unit tests benefitsUnit tests benefits
Unit tests benefits
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
 
Code Review
Code ReviewCode Review
Code Review
 

Similar to AspectMock

Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Aleksandar Bozinovski
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
Aleksandar Bozinovski
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
Mahesh Salaria
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
DevDay.org
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
Phat VU
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
EffectiveUI
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Effective
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
John Blanco
 
Test driven development
Test driven developmentTest driven development
Test driven development
Sharafat Ibn Mollah Mosharraf
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
penanochizzo
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
Fernando Cejas
 
Continuous integration
Continuous integrationContinuous integration
Continuous integrationLior Tal
 
Introduction to Continuous integration
Introduction to Continuous integrationIntroduction to Continuous integration
Introduction to Continuous integration
liortal53
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
Gianluca Padovani
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
Shaun Abram
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
Roman Okolovich
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
Caleb Jenkins
 

Similar to AspectMock (20)

Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
Introduction to Continuous integration
Introduction to Continuous integrationIntroduction to Continuous integration
Introduction to Continuous integration
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 

Recently uploaded (20)

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 

AspectMock

  • 1. AspectMock Making it easier to write unit tests
  • 2. So, yeah, tests are important “Test code is just as important as production code.” “… tests enable change”“Code, without tests, is not clean. No matter how elegant it is, no matter how readable and accessible, if it hath not tests, it be unclean.” “All software you write will be tested – if not by you and your team, then by the eventual users.” “…legacy code is simply code without tests.” “Code without tests is bad code. … With tests we can change the behavior of our code quickly and verifiably. Without them, we really don’t know if our code is getting better or worse.” From Clean Code by Robert Martin, The Pragmatic Programmer by Andrew Hunt and David Thomas, and Working Effectively With Legacy Code by Michael Feathers
  • 3. If you have trivial methods, tests are easy Code Test
  • 4. But who has trivial methods?
  • 5. “They” say use Dependency Injection and mocks Changing the method’s signature requires refactoring all method calls. DI does nothing for static method calls https://phpunit.de/manual/current/en/test-doubles.html
  • 6. Or you could write integration tests … Code Test Even with integration tests, this path cannot be tested because it is a time-sensitive conditional That’s a lot of work
  • 7. Is there a third option? A. Refactor all of your code to use Dependency Injections so you can mock dependencies. B. Write intricate integration tests with heavy setup and teardown C. ??
  • 8. C. Use AspectMock • Allows you to mock any method call (static, private, built-in PHP) • Does not require refactoring existing code • Installs via Composer (and configure an autoloader for tests) • Can return a different mock value for each individual test • Easy to use • Magical
  • 9. AspectMock mocks your code as it is Code Test
  • 10. Here’s the basic setup • Import AspectMockTest • In tearDown() method, run clean() to reset the mocks between tests • Define the class to be mocked, and the desired results for each of its methods • Write your test
  • 11. Code Want a different value for each test? Okay Test
  • 13. So, how does it work? • Relies on Aspect Oriented Programming • Some concerns cut across classes (security, logging, etc.) • Identify join points (method invocation, method return) • Write code that provides advice at designated join points • AOP code can target methods without cluttering those methods • Uses PHP implementation of AOP called Go! AOP Framework (http://go.aopphp.com)
  • 14. Setup 1. Install via Composer Be sure to include any path you may mock, and exclude all test-related paths 2. Create custom autoloader 3. Point phpunit to special autoloader
  • 15. Ready to try it for yourself? Website: https://github.com/Codeception/AspectMock

Editor's Notes

  1. Walk through both the code and the test on this
  2. Explain what the code is doing here. Then talk about how it’s hard to test.
  3. PHPUnit is designed to pass mocks through dependency injection. Injecting dependencies sometimes violates separate of concerns. The calling methods shouldn’t know what calculateTax needs to do its job.
  4. Set up what you need – a test User, location, and item. Use real TaxCode and Calendar classes, maybe with test data in those. It takes a lot of work to set up integration tests. If using dbase, tests are slower and dbase can get littered with test data from failed tests that don’t tear down correctly
  5. Step through test line by line, then walk through execution
  6. Combines integrations and mocks. Code is from a database mapper. The test is to determine if the dbase query runs correctly. We need real values in dbase due to FK constraints on ProjectId and SiteIds It uses values set up in the setUp function of the test, and removed in the teardown function In this sample, we are actually mocking the results of a private method within the method under test, because it relies on values we don’t want to set up. Remember, PHPUnit does not support mocking private methods, so we couldn’t do this in PHPUnit mocks.