createMock(SettingsController::class);
SettingsController::setInstance($settings);
$this->repository = $this->createMock(FormsRepository::class);
$this->wp = $this->createMock(WPFunctions::class);
$this->renderer = $this->createMock(Renderer::class);
$this->hook = new DisplayFormInWPContent($this->wp, $this->repository, $this->renderer);
}
public function testAppendsRenderedFormAfterPostContent() {
$renderedForm = '
';
$renderedFormStyles = '';
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
$this->renderer->expects($this->once())->method('renderStyles')->willReturn($renderedFormStyles);
$this->renderer->expects($this->once())->method('renderHTML')->willReturn($renderedForm);
$form = new FormEntity('My Form');
$form->setSettings([
'segments' => ['3'],
'place_form_bellow_all_pages' => '',
'place_form_bellow_all_posts' => '1',
]);
$form->setBody([[
'type' => 'submit',
'params' => ['label' => 'Subscribe!'],
'id' => 'submit',
'name' => 'Submit',
]]);
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
$result = $this->hook->display('content');
expect($result)->notEquals('content');
expect($result)->endsWith($renderedFormStyles . $renderedForm);
}
public function testItPassThruNonStringPostContent() {
$this->wp->expects($this->never())->method('isSingle');
$this->wp->expects($this->never())->method('isSingular');
$this->repository->expects($this->never())->method('findAll');
expect($this->hook->display(null))->null();
expect($this->hook->display([1,2,3]))->equals([1,2,3]);
expect($this->hook->display(1))->equals(1);
expect($this->hook->display(1.1))->equals(1.1);
}
public function testDoesNotAppendFormIfDisabled() {
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isPage')->willReturn(false);
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
$form = new FormEntity('My Form');
$form->setSettings([
'segments' => ['3'],
'place_form_bellow_all_pages' => '',
'place_form_bellow_all_posts' => '',
]);
$form->setBody([[
'type' => 'submit',
'params' => ['label' => 'Subscribe!'],
'id' => 'submit',
'name' => 'Submit',
]]);
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
$result = $this->hook->display('content');
expect($result)->equals('content');
}
public function testAppendsMultipleRenderedFormAfterPostContent() {
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
$form1 = new FormEntity('My Form');
$form1->setSettings([
'segments' => ['3'],
'place_form_bellow_all_pages' => '',
'place_form_bellow_all_posts' => '1',
]);
$form1->setBody([[
'type' => 'submit',
'params' => ['label' => 'Subscribe1'],
'id' => 'submit',
'name' => 'Submit',
]]);
$form2 = new FormEntity('My Form');
$form2->setSettings([
'segments' => ['3'],
'place_form_bellow_all_pages' => '',
'place_form_bellow_all_posts' => '1',
]);
$form2->setBody([[
'type' => 'submit',
'params' => ['label' => 'Subscribe2'],
'id' => 'submit',
'name' => 'Submit',
]]);
$renderedForm1 = '';
$renderedForm2 = '';
$renderedStyles1 = '';
$renderedStyles2 = '';
$this->repository->expects($this->once())->method('findAll')->willReturn([$form1, $form2]);
$this->renderer->expects($this->exactly(2))->method('renderHTML')->willReturnOnConsecutiveCalls($renderedForm1, $renderedForm2);
$this->renderer->expects($this->exactly(2))->method('renderStyles')->willReturnOnConsecutiveCalls($renderedStyles1, $renderedStyles2);
$result = $this->hook->display('content');
expect($result)->notEquals('content');
expect($result)->contains($renderedStyles1);
expect($result)->contains($renderedForm1);
expect($result)->contains($renderedStyles2);
expect($result)->endsWith($renderedForm2);
}
public function testDoesNotAppendFormIfNotOnSinglePage() {
$this->wp->expects($this->once())->method('isSingle')->willReturn(false);
$this->repository->expects($this->never())->method('findAll');
$result = $this->hook->display('content');
expect($result)->equals('content');
}
public function testDoesNotAppendFormIfNotOnPost() {
$this->wp->expects($this->any())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
$this->wp->expects($this->any())->method('isSingular')->willReturn(false);
$form = new FormEntity('My Form');
$form->setSettings([
'segments' => ['3'],
'place_form_bellow_all_pages' => '',
'place_form_bellow_all_posts' => '1',
]);
$form->setBody([[
'type' => 'submit',
'params' => ['label' => 'Subscribe!'],
'id' => 'submit',
'name' => 'Submit',
]]);
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
$result = $this->hook->display('content');
expect($result)->equals('content');
}
public function testAppendsRenderedFormAfterPageContent() {
$renderedForm = '';
$renderedStyles = '';
$this->renderer->expects($this->once())->method('renderHTML')->willReturn($renderedForm);
$this->renderer->expects($this->once())->method('renderStyles')->willReturn($renderedStyles);
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
$this->wp
->expects($this->never())
->method('setTransient');
$form = new FormEntity('My Form');
$form->setSettings([
'segments' => ['3'],
'place_form_bellow_all_pages' => '1',
'place_form_bellow_all_posts' => '',
]);
$form->setBody([[
'type' => 'submit',
'params' => ['label' => 'Subscribe!'],
'id' => 'submit',
'name' => 'Submit',
]]);
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
$result = $this->hook->display('content');
expect($result)->notEquals('content');
expect($result)->endsWith($renderedStyles . $renderedForm);
}
public function testSetsTransientToImprovePerformance() {
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isPage')->willReturn(false);
$this->wp
->expects($this->once())
->method('setTransient');
$form1 = new FormEntity('My Form');
$form2 = new FormEntity('My Form');
$this->repository->expects($this->once())->method('findAll')->willReturn([$form1, $form2]);
$this->hook->display('content');
}
public function testDoesNotQueryDatabaseIfTransientIsSet() {
$this->wp->expects($this->any())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isPage')->willReturn(false);
$this->wp->expects($this->any())->method('getPostType')->willReturn('post');
$this->wp
->expects($this->once())
->method('getTransient')
->willReturn(['post' => true]);
$this->repository->expects($this->never())->method('findAll');
$this->hook->display('content');
}
}