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
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
730
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
110
ふつうのFeature Flag実践入門
irof
7
3.8k
CSC307 Lecture 17
javiergs
PRO
0
320
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
400
Agentic UI
manfredsteyer
PRO
0
150
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
5.6k
New "Type" system on PicoRuby
pocke
1
850
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.7k
JavaDoc 再入門
nagise
0
330
AI時代のUIはどこへ行く?その2!
yusukebe
21
7.1k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
260
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
470
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Designing for Timeless Needs
cassininazir
1
250
Art, The Web, and Tiny UX
lynnandtonic
304
22k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
230
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
250
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Prompt Engineering for Job Search
mfonobong
0
340
GraphQLとの向き合い方2022年版
quramy
50
15k
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?