SlideShare ist ein Scribd-Unternehmen logo
1 von 17
https://github.com/sebastianbergmann/phpunit
public function testAdd($a, $b, $expected) {
$this->assertEquals($expected, $a + $b);
}
$this->withSession([‘post_confirm' => 1])
->visit(action(‘PostController@getConfirm'))
->see(‘Confirmed Post!’)
->see($input['email']);
$user = factory(AppModelsUser::class)->create(['nick_name' => 'Shanelle Marks']);
$this->be($user);
$this->visit(action('LoginController@getIndex'))
->seePageIs(action('TopPageController@getIndex'));
$this->post(action('LoginController@postChangePassword'), $input)
->seeInDatabase('users', [
'password' => $hashPassword,
])
public function testGetIndexWithoutPermission()
{
$administrator = factory(AppModelsAdministrator::class)->create(['role' => 3]);
$this->actingAsAdmin($administrator)
->get(action('AdminAdminArticleController@getIndex'))
->assertResponseStatus(403);
}
$crawler = new Crawler($html);
foreach ($crawler as $domElement) {
var_dump($domElement->nodeName);
}
Khởi tạo
Duyệt bằng Xpath
$crawler = $crawler->filterXPath('//div/p[@class="framgia"]/a');
Duyệt bằng jQuery-like selectors
$crawler = $crawler->filter('div > p[class="framgia"] > a');
NodeTraversing
$crawler->filter('body > p')->eq(0);
$crawler->filter('body > p')->first();
$crawler->filter('body > p')->last();
$crawler->filter('body > p')->siblings();
$crawler->filter('body > p')->nextAll();
$crawler->filter('body > p')->previousAll();
$crawler->filter('body > p')->children();
$crawler->filter('body > p')->parents();
Accessing NodeValues
$crawler->filter('body > p')->nodeName();
$crawler->filter('body > p')->text();
$crawler->filter('body > p')->html();
$crawler->filter('body > p')->attr('name');
$this->actingAs($user)->visit(action('RecordController@getDetail', $dietRecord->id))
->crawler->filter('img.posted-photo')
->each(function (Crawler $node, $i) use($img) {
$this->assertEquals($node->attr('src'), image_url($img->img_url));
});
$editButton = $this->actingAs($user2)->visit(route('mypage', $user->id))
->see(trans('mypage/labels.edit_method_in_practice'))
->crawler->filter('#myPracticeListEdit');
$this->assertContains('hide', $editButton->attr('class'));
$pagination = $this->actingAsAdmin($administrator)
->visit(action('AdminAdminMemberController@getIndex', $input))
->crawler->filter('ul.pagination');
$this->assertEquals(0, $pagination->count());
interface ExtensionManagerInterface
{
function checkExtension($fileName);
}
class ExtensionManager implements ExtensionManagerInterface
{
public function checkExtension($fileName)
{
//Some complex business logic might goes here. May be DB operation or file system handling
return false;
}
}
class FileChecker
{
private $objManager = null;
//Default constructor
function __construct()
{
$this->objManager = new ExtensionManager();
}
//Parameterized constructor
function __construct(ExtensionManagerInterface $tmpManager)
{
$this->objManager = $tmpManager;
}
public function checkFile($fileName)
{
//Check file size, file name length,...
return $this->objManager->checkExtension($fileName);
}
}
//Stub implementation to bypass actual Extension manager class.
class StubExtensionManager implements ExtensionManagerInterface
{
public function checkExtension($fileName)
{
return true;
}
}
public function testCheckFileFunction()
{
$stubExtensionManagerObject = new StubExtensionManager;
$fileCheckerObject = new FileChecker($stubExtensionManagerObject);
$this->assertTrue($fileCheckerObject->checkFile('framgia.com'));
}
Mockery
"mockery/mockery": "0.9.*",
$userServiceMock = Mockery::mock(AppServicesUserService::class);
$this->app->instance(AppServicesUserService::class, $userServiceMock);
Mockery::close();
public function testIndex()
{
$this->mock->shouldReceive('all')->once();
}
Cache::shouldReceive('get')
->times(3)
->with('key')
->andReturn('value');
->with(Mockery::any()) OR ->with(anything())
->with(Mockery::on(closure))
->with('/^foo/') OR ->with(matchesPattern('/^foo/'))
->with(Mockery::mustBe(2)) OR ->with(identicalTo(2))
->with(Mockery::not(2)) OR ->with(not(2))
->with(Mockery::anyOf(1, 2)) OR with(anyOf(1,2))
->with(Mockery::subset(array(0 => 'foo')))
->with(Mockery::contains(value1, value2))
->with(Mockery::hasKey(key))
->shouldReceive(method1, method2, ...)
->andThrow(Exception)
->andThrow(exception_name, message)
->zeroOrMoreTimes()
->atLeast()->times(3)
->atMost()->times(3)
->between(min, max)
$mock = Mockery::mock('foo')->shouldReceive('foo')->andReturn(1)->getMock();
$this->mock = Mockery::mock('MyClass[save, send]');
$this->mock = Mockery::mock('MyClass')->makePartial();
$this->mock
->shouldReceive('save')
->once()
->andReturn('true');
Constructor arguments
$mock = Mockery::mock('MyNamespaceMyClass[foo]', array($arg1, $arg2));
$experienceServiceMock = Mockery::mock(AppServicesExperienceService::class);
$experienceServiceMock->shouldReceive('saveExperience')->times(1)->andReturn(false);
$this->app->instance(AppServicesExperienceService::class, $experienceServiceMock);
// Test
$this->actingAs($user)
->withSession($sessionData)
->post(action('ExperienceController@postConfirm'))
->assertResponseStatus(500);
PHPUnit with Mocking and Crawling

Weitere ähnliche Inhalte

Was ist angesagt?

Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax pluginsInbal Geffen
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
How I started to love design patterns
How I started to love design patternsHow I started to love design patterns
How I started to love design patternsSamuel ROZE
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form componentSamuel ROZE
 
Introduction to jQuery - The basics
Introduction to jQuery - The basicsIntroduction to jQuery - The basics
Introduction to jQuery - The basicsMaher Hossain
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS RoutingEyal Vardi
 
WordPress-Powered Portfolios
WordPress-Powered PortfoliosWordPress-Powered Portfolios
WordPress-Powered PortfoliosTyler Sticka
 
JAVASCRIPT NÃO-OBSTRUTIVO com jQuery
JAVASCRIPT NÃO-OBSTRUTIVO com jQueryJAVASCRIPT NÃO-OBSTRUTIVO com jQuery
JAVASCRIPT NÃO-OBSTRUTIVO com jQueryZigotto Tecnologia
 
Design for succcess with react and storybook.js
Design for succcess with react and storybook.jsDesign for succcess with react and storybook.js
Design for succcess with react and storybook.jsChris Saylor
 
Violet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole TeamViolet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole TeamAnton Caceres
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using CodeceptionJeroen van Dijk
 
Real Time App with Node.js
Real Time App with Node.jsReal Time App with Node.js
Real Time App with Node.jsJxck Jxck
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS DirectivesEyal Vardi
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS ServicesEyal Vardi
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performanceYehuda Katz
 

Was ist angesagt? (20)

Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
How I started to love design patterns
How I started to love design patternsHow I started to love design patterns
How I started to love design patterns
 
5. CodeIgniter copy1
5. CodeIgniter copy15. CodeIgniter copy1
5. CodeIgniter copy1
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form component
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
 
Introduction to jQuery - The basics
Introduction to jQuery - The basicsIntroduction to jQuery - The basics
Introduction to jQuery - The basics
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS Routing
 
WordPress-Powered Portfolios
WordPress-Powered PortfoliosWordPress-Powered Portfolios
WordPress-Powered Portfolios
 
JAVASCRIPT NÃO-OBSTRUTIVO com jQuery
JAVASCRIPT NÃO-OBSTRUTIVO com jQueryJAVASCRIPT NÃO-OBSTRUTIVO com jQuery
JAVASCRIPT NÃO-OBSTRUTIVO com jQuery
 
Design for succcess with react and storybook.js
Design for succcess with react and storybook.jsDesign for succcess with react and storybook.js
Design for succcess with react and storybook.js
 
jQuery
jQueryjQuery
jQuery
 
Violet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole TeamViolet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole Team
 
React 101
React 101React 101
React 101
 
Matters of State
Matters of StateMatters of State
Matters of State
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Real Time App with Node.js
Real Time App with Node.jsReal Time App with Node.js
Real Time App with Node.js
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS Services
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
 

Ähnlich wie PHPUnit with Mocking and Crawling

PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersKacper Gunia
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in actionJace Ju
 
Design how your objects talk through mocking
Design how your objects talk through mockingDesign how your objects talk through mocking
Design how your objects talk through mockingKonstantin Kudryashov
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needKacper Gunia
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ EtsyNishan Subedi
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Building Persona: federated and privacy-sensitive identity for the Web (Open ...Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Building Persona: federated and privacy-sensitive identity for the Web (Open ...Francois Marier
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful softwareJorn Oomen
 
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...Francois Marier
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011Alessandro Nadalin
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Michelangelo van Dam
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConRafael Dohms
 

Ähnlich wie PHPUnit with Mocking and Crawling (20)

PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
 
Min-Maxing Software Costs
Min-Maxing Software CostsMin-Maxing Software Costs
Min-Maxing Software Costs
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
Design how your objects talk through mocking
Design how your objects talk through mockingDesign how your objects talk through mocking
Design how your objects talk through mocking
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Building Persona: federated and privacy-sensitive identity for the Web (Open ...Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
 
Taming Command Bus
Taming Command BusTaming Command Bus
Taming Command Bus
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 

Kürzlich hochgeladen

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Kürzlich hochgeladen (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

PHPUnit with Mocking and Crawling

  • 1.
  • 2.
  • 4. public function testAdd($a, $b, $expected) { $this->assertEquals($expected, $a + $b); } $this->withSession([‘post_confirm' => 1]) ->visit(action(‘PostController@getConfirm')) ->see(‘Confirmed Post!’) ->see($input['email']); $user = factory(AppModelsUser::class)->create(['nick_name' => 'Shanelle Marks']); $this->be($user); $this->visit(action('LoginController@getIndex')) ->seePageIs(action('TopPageController@getIndex')); $this->post(action('LoginController@postChangePassword'), $input) ->seeInDatabase('users', [ 'password' => $hashPassword, ]) public function testGetIndexWithoutPermission() { $administrator = factory(AppModelsAdministrator::class)->create(['role' => 3]); $this->actingAsAdmin($administrator) ->get(action('AdminAdminArticleController@getIndex')) ->assertResponseStatus(403); }
  • 5.
  • 6.
  • 7. $crawler = new Crawler($html); foreach ($crawler as $domElement) { var_dump($domElement->nodeName); } Khởi tạo Duyệt bằng Xpath $crawler = $crawler->filterXPath('//div/p[@class="framgia"]/a'); Duyệt bằng jQuery-like selectors $crawler = $crawler->filter('div > p[class="framgia"] > a'); NodeTraversing $crawler->filter('body > p')->eq(0); $crawler->filter('body > p')->first(); $crawler->filter('body > p')->last(); $crawler->filter('body > p')->siblings(); $crawler->filter('body > p')->nextAll(); $crawler->filter('body > p')->previousAll(); $crawler->filter('body > p')->children(); $crawler->filter('body > p')->parents(); Accessing NodeValues $crawler->filter('body > p')->nodeName(); $crawler->filter('body > p')->text(); $crawler->filter('body > p')->html(); $crawler->filter('body > p')->attr('name');
  • 8. $this->actingAs($user)->visit(action('RecordController@getDetail', $dietRecord->id)) ->crawler->filter('img.posted-photo') ->each(function (Crawler $node, $i) use($img) { $this->assertEquals($node->attr('src'), image_url($img->img_url)); }); $editButton = $this->actingAs($user2)->visit(route('mypage', $user->id)) ->see(trans('mypage/labels.edit_method_in_practice')) ->crawler->filter('#myPracticeListEdit'); $this->assertContains('hide', $editButton->attr('class')); $pagination = $this->actingAsAdmin($administrator) ->visit(action('AdminAdminMemberController@getIndex', $input)) ->crawler->filter('ul.pagination'); $this->assertEquals(0, $pagination->count());
  • 9.
  • 10.
  • 11.
  • 12. interface ExtensionManagerInterface { function checkExtension($fileName); } class ExtensionManager implements ExtensionManagerInterface { public function checkExtension($fileName) { //Some complex business logic might goes here. May be DB operation or file system handling return false; } } class FileChecker { private $objManager = null; //Default constructor function __construct() { $this->objManager = new ExtensionManager(); } //Parameterized constructor function __construct(ExtensionManagerInterface $tmpManager) { $this->objManager = $tmpManager; } public function checkFile($fileName) { //Check file size, file name length,... return $this->objManager->checkExtension($fileName); } }
  • 13. //Stub implementation to bypass actual Extension manager class. class StubExtensionManager implements ExtensionManagerInterface { public function checkExtension($fileName) { return true; } } public function testCheckFileFunction() { $stubExtensionManagerObject = new StubExtensionManager; $fileCheckerObject = new FileChecker($stubExtensionManagerObject); $this->assertTrue($fileCheckerObject->checkFile('framgia.com')); }
  • 14. Mockery "mockery/mockery": "0.9.*", $userServiceMock = Mockery::mock(AppServicesUserService::class); $this->app->instance(AppServicesUserService::class, $userServiceMock); Mockery::close();
  • 15. public function testIndex() { $this->mock->shouldReceive('all')->once(); } Cache::shouldReceive('get') ->times(3) ->with('key') ->andReturn('value'); ->with(Mockery::any()) OR ->with(anything()) ->with(Mockery::on(closure)) ->with('/^foo/') OR ->with(matchesPattern('/^foo/')) ->with(Mockery::mustBe(2)) OR ->with(identicalTo(2)) ->with(Mockery::not(2)) OR ->with(not(2)) ->with(Mockery::anyOf(1, 2)) OR with(anyOf(1,2)) ->with(Mockery::subset(array(0 => 'foo'))) ->with(Mockery::contains(value1, value2)) ->with(Mockery::hasKey(key)) ->shouldReceive(method1, method2, ...) ->andThrow(Exception) ->andThrow(exception_name, message) ->zeroOrMoreTimes() ->atLeast()->times(3) ->atMost()->times(3) ->between(min, max) $mock = Mockery::mock('foo')->shouldReceive('foo')->andReturn(1)->getMock();
  • 16. $this->mock = Mockery::mock('MyClass[save, send]'); $this->mock = Mockery::mock('MyClass')->makePartial(); $this->mock ->shouldReceive('save') ->once() ->andReturn('true'); Constructor arguments $mock = Mockery::mock('MyNamespaceMyClass[foo]', array($arg1, $arg2)); $experienceServiceMock = Mockery::mock(AppServicesExperienceService::class); $experienceServiceMock->shouldReceive('saveExperience')->times(1)->andReturn(false); $this->app->instance(AppServicesExperienceService::class, $experienceServiceMock); // Test $this->actingAs($user) ->withSession($sessionData) ->post(action('ExperienceController@postConfirm')) ->assertResponseStatus(500);