SlideShare a Scribd company logo
1 of 19
PhpUnit over Symfony
2
www.innobyte.com
PhpUnit docs: http://phpunit.de/manual/current/en/
PhpUnit tutorial: https://jtreminio.com/2013/03/unit-testing-
tutorial-introduction-to-phpunit/
Summary
1. Install
2. Running tests
3. Symfony 2 Unit Testing structure
4. Simple tests (UnitTesting)
5. Controller tests (Functional Tests)
1. Client
2. Crawler
6. Code coverage
7. Assertions
8. Annotations
9. Example
Install
1. wget https://phar.phpunit.de/phpunit.phar
2. chmod +x phpunit.phar
3. mv phpunit.phar /usr/local/bin/phpunit
4. pear channel-discover pear.phpunit.de
5. pear remote-list -c phpunit
6. pear install phpunit/PHP_CodeCoverage
Running tests
1. phpunit.xml:
1. On Symfony 2, you must have app/phpunit.xml or app/phpunit.xml.dist. The second
one comes with the default Symfony 2 install and is more then enough.
2. You can find more about this on phpunit manual:
http://phpunit.de/manual/3.7/en/appendixes.configuration.html
2. Console:
1. Default: phpunit -c app
2. With code coverage: phpunit -c app –coverage-html /path/to/save
3. Running on an individual file: phpunit -c app src/Vendor/Bundle/Tests/MyTest.php
4. Run just some tests: phpunit -c app –filter=myMethodName
src/Vendor/Bundle/Tests/MyTest.php
5. More about what parameters accept phpunit you can find on:
http://phpunit.de/manual/3.7/en/textui.html
Symfony 2 Unit Testing structure
1. Each bundle contains or must contains the folder Tests in which you can
add all the unit test files and classes.
2. In the Tests folder, as a best practice, you must create the same folder
structure as in the bundle root folder. As an example, if you have folder
Entity in the root of the bundle, you must have Tests/Entity. Each folder
from the test must contains a file (class) for each class we need to test,
by adding the suffix Test, as an example, if you have the class Users
which is places in Entity/Users.php, you must create class UsersTest in
Tests/Entity/UsersTest.php.
3. Each method from a testing class must have the prefix test. For
example, if you have to test the method getUsername, create the
method testGetUsername. If you must have more than one test
method, which handle the testing of the same method, you can add as a
suffix and index or a scope, as an example testGetUsername1,
testGetUsername2, … or something like testGetUsernameException.
Simple tests (UnitTesting)
Controller tests (Functional Tests)
1. $client = static::createClient();
2. Util methods:
3. request: simulate a request on a
provided route
4. insulate: allow you to run the test in a
separated process
5. click: simulate the click on a link
6. submit: allow you to submit a form
7. back: navigate on the previous request
8. forward: navigate on a forward request,
if exists
9. reload: reload the current request
10. restart: clear all cookies and history
Controller tests (Functional Tests) - Client
11. getHistory: provide the history
of the requests
12. getCookieJar: provide all the
cookies of the test
13. getRequest: the HttpKernel
request instance
14. getInternalRequest: the
BrowserKit request instance
14. getResponse: the HttpKernel
response instance
15. getInternalResponse: the
BrowserKit response instance
16. getCrawler: get the Crawler
object of Symfony 2 unit testing
17. getContainer: get the container
of the Symfony 2
18. getKernel: get the Kernel of the
Symfony 2c
1. A Crawler instance is returned each time you make a request
with the Client. It allows you to traverse HTML documents,
select nodes, find links and forms.
2. Like jQuery, the Crawler has methods to traverse the DOM of
an HTML/XML document. For example, the following finds all
input[type=submit] elements, selects the last one on the
page, and then selects its immediate parent element.
3. More about the Crawler you can find:
http://symfony.com/doc/current/book/testing.html#the-
crawler
Controller tests (Functional Tests) -
Crawler
phpunit -c app –coverage-html /path/to/save
Code coverage (1)
Code coverage (2)
1. A test method can cover one or more methods, to provide the methods which
are tested by the test method, you can add the annotation @covers
{Class}::{Method}. Add one annotation for each method which is testes by your
test method.
2. If a test method does not cover any method, you can add the annotation
@coversNothing.
3. If you wish to have part of code which are ignored by the code coverage, you can
include those part between comments like:
➢ // @codeCoverageIgnoreStart
➢ // @codeCoverageIgnoreEnd
4. If you have a full method, or class, which must be ignored by the code coverage,
you can add the annotation @codeCoverageIgnore, in the doc block. This is
useful for constructors which just assign parameters values to class properties.
Code coverage (3)
1. assertTrue($condition): Reports an error if $condition is FALSE.
2. assertFalse($condition): Reports an error if $condition is TRUE.
3. assertNull(variable): Reports an error if $variable is not NULL.
4. assertEmpty(actual): Reports an error if $actual is not empty.
5. assertCount($expectedCount, $haystack): Reports an error if the
number of elements in $haystack is not $expectedCount.
6. More asserts you can find on:
http://phpunit.de/manual/current/en/appendixes.assertions.html
Assertions
1. @dataProvider {provider method name} offer you the posibility to have multiple
parameters on a test method. You can test a method on different scenarios using this
annotation. Data provider requires another public method which must return an array with
all the scenarios. Be carefull, if your test method requires more than one parameters, the
result of the data provider method must be an array of arrays.
2. @depends {method name} allow a test method to be executed only if the test on which is
depend was successfully.
3. @expectedException {exception class} expects to the test method to throw an exception.
4. @expectedExceptionCode {exception code} expects to the test method to throw an
exception with a specific code (can be combined with expectedException and
expectedExceptionMessage).
5. @expectedExceptionMessage {exception message} expects to the test method to throw
an exception with a specific message (can be combined with expectedException and
expectedExceptionCode).
6. More annotation you can find on:
http://phpunit.de/manual/current/en/appendixes.annotations.html
Annotations
A more complex method which include if statement and can
throw exceptions
Example – The method which is tested
Example – @dataProvider and @covers
Example – @expectedException* and
@depends
Example – Skipping because of
@depends
Thank you!

More Related Content

What's hot

Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnitinTwentyEight Minutes
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Edureka!
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesNarendra Pathai
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnitShouvik Chatterjee
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Frameworkwebhostingguy
 
.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010kgayda
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with MockitoRichard Paul
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And DrupalPeter Arato
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-ivRubaNagarajan
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMockYing Zhang
 

What's hot (20)

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
Rc2010 tdd
Rc2010 tddRc2010 tdd
Rc2010 tdd
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Unit testing
Unit testingUnit testing
Unit testing
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnit
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Framework
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
3 j unit
3 j unit3 j unit
3 j unit
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Junit
JunitJunit
Junit
 

Similar to Php unit

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2Tricode (part of Dept)
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to heroJeremy Cook
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnitMindfire Solutions
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightOpenDaylight
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineAnup Singh
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1Albert Rosa
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHPRadu Murzea
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitJames Fuller
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Qualityguest268ee8
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to TestZsolt Fabok
 
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactusHimanshu
 
Magento 2 integration tests
Magento 2 integration testsMagento 2 integration tests
Magento 2 integration testsDusan Lukic
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 

Similar to Php unit (20)

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
 
Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnitZend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Junit4.0
Junit4.0Junit4.0
Junit4.0
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test
 
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactus
 
Magento 2 integration tests
Magento 2 integration testsMagento 2 integration tests
Magento 2 integration tests
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
What is new in JUnit5
What is new in JUnit5What is new in JUnit5
What is new in JUnit5
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 

Recently uploaded

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

Php unit

  • 1. PhpUnit over Symfony 2 www.innobyte.com PhpUnit docs: http://phpunit.de/manual/current/en/ PhpUnit tutorial: https://jtreminio.com/2013/03/unit-testing- tutorial-introduction-to-phpunit/
  • 2. Summary 1. Install 2. Running tests 3. Symfony 2 Unit Testing structure 4. Simple tests (UnitTesting) 5. Controller tests (Functional Tests) 1. Client 2. Crawler 6. Code coverage 7. Assertions 8. Annotations 9. Example
  • 3. Install 1. wget https://phar.phpunit.de/phpunit.phar 2. chmod +x phpunit.phar 3. mv phpunit.phar /usr/local/bin/phpunit 4. pear channel-discover pear.phpunit.de 5. pear remote-list -c phpunit 6. pear install phpunit/PHP_CodeCoverage
  • 4. Running tests 1. phpunit.xml: 1. On Symfony 2, you must have app/phpunit.xml or app/phpunit.xml.dist. The second one comes with the default Symfony 2 install and is more then enough. 2. You can find more about this on phpunit manual: http://phpunit.de/manual/3.7/en/appendixes.configuration.html 2. Console: 1. Default: phpunit -c app 2. With code coverage: phpunit -c app –coverage-html /path/to/save 3. Running on an individual file: phpunit -c app src/Vendor/Bundle/Tests/MyTest.php 4. Run just some tests: phpunit -c app –filter=myMethodName src/Vendor/Bundle/Tests/MyTest.php 5. More about what parameters accept phpunit you can find on: http://phpunit.de/manual/3.7/en/textui.html
  • 5. Symfony 2 Unit Testing structure 1. Each bundle contains or must contains the folder Tests in which you can add all the unit test files and classes. 2. In the Tests folder, as a best practice, you must create the same folder structure as in the bundle root folder. As an example, if you have folder Entity in the root of the bundle, you must have Tests/Entity. Each folder from the test must contains a file (class) for each class we need to test, by adding the suffix Test, as an example, if you have the class Users which is places in Entity/Users.php, you must create class UsersTest in Tests/Entity/UsersTest.php. 3. Each method from a testing class must have the prefix test. For example, if you have to test the method getUsername, create the method testGetUsername. If you must have more than one test method, which handle the testing of the same method, you can add as a suffix and index or a scope, as an example testGetUsername1, testGetUsername2, … or something like testGetUsernameException.
  • 8. 1. $client = static::createClient(); 2. Util methods: 3. request: simulate a request on a provided route 4. insulate: allow you to run the test in a separated process 5. click: simulate the click on a link 6. submit: allow you to submit a form 7. back: navigate on the previous request 8. forward: navigate on a forward request, if exists 9. reload: reload the current request 10. restart: clear all cookies and history Controller tests (Functional Tests) - Client 11. getHistory: provide the history of the requests 12. getCookieJar: provide all the cookies of the test 13. getRequest: the HttpKernel request instance 14. getInternalRequest: the BrowserKit request instance 14. getResponse: the HttpKernel response instance 15. getInternalResponse: the BrowserKit response instance 16. getCrawler: get the Crawler object of Symfony 2 unit testing 17. getContainer: get the container of the Symfony 2 18. getKernel: get the Kernel of the Symfony 2c
  • 9. 1. A Crawler instance is returned each time you make a request with the Client. It allows you to traverse HTML documents, select nodes, find links and forms. 2. Like jQuery, the Crawler has methods to traverse the DOM of an HTML/XML document. For example, the following finds all input[type=submit] elements, selects the last one on the page, and then selects its immediate parent element. 3. More about the Crawler you can find: http://symfony.com/doc/current/book/testing.html#the- crawler Controller tests (Functional Tests) - Crawler
  • 10. phpunit -c app –coverage-html /path/to/save Code coverage (1)
  • 12. 1. A test method can cover one or more methods, to provide the methods which are tested by the test method, you can add the annotation @covers {Class}::{Method}. Add one annotation for each method which is testes by your test method. 2. If a test method does not cover any method, you can add the annotation @coversNothing. 3. If you wish to have part of code which are ignored by the code coverage, you can include those part between comments like: ➢ // @codeCoverageIgnoreStart ➢ // @codeCoverageIgnoreEnd 4. If you have a full method, or class, which must be ignored by the code coverage, you can add the annotation @codeCoverageIgnore, in the doc block. This is useful for constructors which just assign parameters values to class properties. Code coverage (3)
  • 13. 1. assertTrue($condition): Reports an error if $condition is FALSE. 2. assertFalse($condition): Reports an error if $condition is TRUE. 3. assertNull(variable): Reports an error if $variable is not NULL. 4. assertEmpty(actual): Reports an error if $actual is not empty. 5. assertCount($expectedCount, $haystack): Reports an error if the number of elements in $haystack is not $expectedCount. 6. More asserts you can find on: http://phpunit.de/manual/current/en/appendixes.assertions.html Assertions
  • 14. 1. @dataProvider {provider method name} offer you the posibility to have multiple parameters on a test method. You can test a method on different scenarios using this annotation. Data provider requires another public method which must return an array with all the scenarios. Be carefull, if your test method requires more than one parameters, the result of the data provider method must be an array of arrays. 2. @depends {method name} allow a test method to be executed only if the test on which is depend was successfully. 3. @expectedException {exception class} expects to the test method to throw an exception. 4. @expectedExceptionCode {exception code} expects to the test method to throw an exception with a specific code (can be combined with expectedException and expectedExceptionMessage). 5. @expectedExceptionMessage {exception message} expects to the test method to throw an exception with a specific message (can be combined with expectedException and expectedExceptionCode). 6. More annotation you can find on: http://phpunit.de/manual/current/en/appendixes.annotations.html Annotations
  • 15. A more complex method which include if statement and can throw exceptions Example – The method which is tested
  • 18. Example – Skipping because of @depends