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
Ad

More Related Content

What's hot (20)

Unit Testing
Unit TestingUnit Testing
Unit Testing
François Camus
 
TDD
TDDTDD
TDD
Dotan Patrich
 
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
 
Unit testing with Jasmine
Unit testing with JasmineUnit testing with Jasmine
Unit testing with Jasmine
simonKenyonShepard
 
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 Review
Ravi 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
Code Review
R M Shahidul Islam Shahed
 
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
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
Kirill Miroshnichenko
 
Code Review
Code ReviewCode Review
Code Review
Lukas Rypl
 
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
 
Code Review
Code ReviewCode Review
Code Review
Ravi 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
 
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
 

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
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 Da Nang
 
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
Fernando Cejas
 
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
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
Lior Tal
 
Introduction to Continuous integration
Introduction to Continuous integrationIntroduction to Continuous integration
Introduction to Continuous integration
liortal53
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Rajesh Kumar
 
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
 
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
 
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 Da Nang
 
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
 
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
 
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
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
Lior Tal
 
Introduction to Continuous integration
Introduction to Continuous integrationIntroduction to Continuous integration
Introduction to Continuous integration
liortal53
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Rajesh Kumar
 
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
 
Ad

Recently uploaded (20)

HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Ad

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

  • #4: Walk through both the code and the test on this
  • #5: Explain what the code is doing here. Then talk about how it’s hard to test.
  • #6: 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.
  • #7: 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
  • #10: Step through test line by line, then walk through execution
  • #13: 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.