Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mockery & mocking basics
Search
Jan Škrášek
February 28, 2015
Programming
160
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Mockery & mocking basics
Source code:
https://github.com/hrach/posobota-mockery
Jan Škrášek
February 28, 2015
Other Decks in Programming
See All in Programming
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1k
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
710
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
5
510
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1.2k
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
7.7k
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
360
VibeCodingからAgenticWorkflowへ
starfish719
0
790
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
130
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.6k
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
140
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
510
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
How GitHub (no longer) Works
holman
316
150k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Typedesign – Prime Four
hannesfritz
42
3.1k
Accessibility Awareness
sabderemane
1
180
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
500
GraphQLとの向き合い方2022年版
quramy
50
15k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Transcript
Mockery & mocking basics Jan Škrášek @hrachcz github.com/nextras github.com/hrach
Poll • Who is testing? • Who use mocking tool?
• Who use Mockery?
Test types • Unit tests • Ingtegrations tests o Regression
tests • Acceptance tests • … Mock objects
Integration tests • object composition • real functionality
Unit tests • class functionality only! • all dependencies mocked
Mock object Mock object is simulated object that mimic the
behavior of real object in a controlled way.
Integration test $driver = $connection->getDriver(); $parser = new SqlProcessor($driver); Assert::same(
'SELECT `a`.`b`', $parser->process('SELECT [a.b]') );
Unit test $driver = Mockery::mock(IDriver::class); $parser = new SqlProcessor($driver); $driver->shouldReceive('delimite')
->with('a.b')->andReturn('`a`.`b`'); Assert::same( 'SELECT `a`.`b`', $parser->process('SELECT [a.b]') );
Mockery Composer mockery/mockery Pádraic Brady Current version 0.9
Mockery – general $mock = Mockery::mock(IDriver::class); // $mock instanceof MockInterface;
$mock->should… Mockery::close();
Mockery – methods $mock->shouldRecieve('method') ->once()->twice() ->times(4) ->ordered() ->with('foo') ->andReturn('bar')->andThrow()
Mockery – arguments $mock->shouldRecieve('method') ->with(Mockery::any()) ->with(2, Mockery::any()) ->with(Mockery::on(function($arg) { return
TRUE; }) ->with(Mockery::anyOf(1, 2)) ->with(Mockery::type('float')
Mockery – returning $mock->shouldRecieve('method') ->andReturn(TRUE) ->andReturnUsing(function($arg) { return strtoupper($arg); })
->andThrow() ->andSet($property, $value);
Unit tests • Are needed • Are useful • But…
there are some exceptions…
$this->mapper->shouldReceive('getRepository')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getEntityClassNames')->once()->andReturn(['EntityClass']); $this->metadataStorage->shouldReceive('get')->once()->with('EntityClass')->andReturn($this->entityM $this->queryBuilder->shouldReceive('getFromAlias')->once()->andReturn('books'); $this->mapper->shouldReceive('getStorageReflection')->once()->andReturn($this->reflection); $propertyMetadata = Mockery::mock('Nextras\Orm\Entity\Reflection\PropertyMetadata'); $propertyMetadata->relationshipRepository =
'AuthorsRepository'; $propertyMetadata->relationshipType = PropertyMetadata::RELATIONSHIP_MANY_HAS_ONE; // translator $this->entityMetadata->shouldReceive('getProperty')->once()->with('translator')->andReturn($property $this->model->shouldReceive('getRepository')->once()->with('AuthorsRepository')->andReturn($this-> $this->model->shouldReceive('getMapper')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getStorageReflection')->once()->andReturn($this->reflection); $this->reflection->shouldReceive('getStoragePrimaryKey')->once()->andReturn(['id']); $this->reflection->shouldReceive('convertEntityToStorageKey')->once()->with('translator')->andReturn( $this->mapper->shouldReceive('getTableName')->once()->andReturn('authors'); $this->mapper->shouldReceive('getRepository')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getEntityClassNames')->once()->andReturn(['EntityClass2']); $this->metadataStorage->shouldReceive('get')->once()->with('EntityClass2')->andReturn($this->entityM // name $this->entityMetadata->shouldReceive('getProperty')->once()->with('name'); $this->reflection->shouldReceive('convertEntityToStorageKey')->once()->with('name')->andReturn('nam $this->queryBuilder->shouldReceive('leftJoin')->once()->with('books', 'authors', 'authors', '[books.transl $this->queryBuilder->shouldReceive('addOrderBy')->once()->with('authors.name'); $this->builderHelper->processOrderByExpression('this->translator->name', ICollection::ASC, $this->qu
Unit tests • To much work to mock? o Just
SPLIT the class. • Should we mock?: o scalars/arrays/objects o entities/crates o model layer • Integration tests
Integration tests • Are quite difficult • You need: o
Setup database/queue o Prepare testing data (insert into database) o Working internet connection o … • Solution: PARTIAL MOCKS
Partial mocks • Mock only something • Mock only some
methods $mock = Mockery::mock(MysqlDriver::class) ->makePartial(); $mock->shouldReceive(…);
DEMO TIME
Recap • Use composition!!! • Use interfaces. • Create small
classes. • Choose your library carefully. • Write unit/integration tests.
Thank you Jan Škrášek @hrachcz github.com/nextras github.com/hrach Questions?