The goal: Unit test an InputFilter in Zend Framework 2.
The problem: A mocked DbAdapter is needed.
As I am relatively new to Unit Testing I just got started with mocking classes. After doing a lot of research I'm still unable to find a proper solution for my problem so here we go with my filter to start things off:
class ExampleFilter extends Inputfilter
{
protected $dbAdapter;
public function __construct(AdapterInterface $dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
public function init()
{
$this->add(
[
'name' => 'example_field',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags'],
],
'validators' => [
[
'name' => 'Db\NoRecordExists',
'options' => [
'adapter' => $this->dbAdapter,
'table' => 'example_table',
'field' => 'example_field',
],
],
],
]
);
}
}
Without the need of an adapter, testing this filter would be rather easy. My problem is to create the Filter in my TestClass as seen here:
class ExampleFilterTest extends \PHPUnit_Framework_TestCase
{
protected $exampleFilter;
protected $mockDbAdapter;
public function setUp()
{
$this->mockDbAdapter = $this->getMockBuilder('Zend\Db\Adapter')
->disableOriginalConstructor()
->getMock();
$this->exampleFilter = new ExampleFilter($this->mockDbAdapter);
}
}
When creating the filter like this the ExampleFilter class will end up saying that I did provide a wrong class to its constructor. It's receiving a mock object when expecting one of type Zend\Db\Adapter\Adapter.
I could create a real adapter of course but I want to avoid performing actual queries to the database as it is a unit test and this would go far beyond the border of my unit to test.
Can anyone tell me how I can achieve my goal of testing the filter with a mocked DbAdapter?
Well... As I was commenting on gontrollez hint I already smelled my mistake. I had to create a mock of 'Zend/Db/Adapter/AdapterInterface' instead of just '/Zend/Db/Adapter'.
Thanks for bringing me on the right path anyway gontrollez :)
Related
I am using the BsModalRef for showing modals and sending data using the content property. So we have some like this :
this.followerService.getFollowers(this.bsModalRef.content.channelId).subscribe((followers) => {
this.followerList = followers;
this.followerList.forEach((follower) => {
follower.avatarLink = this.setUserImage(follower.userId);
this.followerEmails.push(follower.email);
});
});
We are setting the channelId in content of bsModalRef (this.bsModalRef.content.channelId). It is working fine. Now i am writing a unit test for this. Problem is i am not able to mock it. I have tried overriding, spy etc but nothing seems to work. I am using the approach mentioned in this link. One alternative is to use TestBed but i am not much aware of its use. Can anyone please help me finding any approach by which this can be achieved ?
I recently had to do something similar and Mocking the method call worked. The tricky part is injecting the BsModalService in both the test suite and the component.
describe('MyFollowerService', () => {
configureTestSuite(() => {
TestBed.configureTestingModule({
imports: [...],
declarations: [...],
providers: [...]
}).compileComponents();
});
// inject the service
beforeEach(() => {
bsModalService = getTestBed().get(BsModalService);
}
it('test', () => {
// Mock the method call
bsModalService.show = (): BsModalRef => {
return {hide: null, content: {channelId: 123}, setClass: null};
};
// Do the test that calls the modal
});
});
As long as you're calling bsModal as follows this approach will work
let bsModalRef = this.modalService.show(MyChannelModalComponent));
Finally, here are some links that have more indepth coverage about setting up the tests with TestBed.
https://chariotsolutions.com/blog/post/testing-angular-2-0-x-services-http-jasmine-karma/
http://angulartestingquickstart.com/
https://angular.io/guide/testing
I've made a web service using yii2 basic template I got a table called 'ely_usuario' when I call it with:
http://localhost/basic/web/index.php/ely-usuario/
it works fine and returns me all the rows in ely_usuario table
but when I try to get just one record, for example:
http://localhost/basic/web/index.php/ely-usuario/29
it doesn't work, show me a not found page, I've made the model class using gii
here's my Controller:
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class ElyUsuarioController extends ActiveController
{
public $modelClass = 'app\models\ElyUsuario';
}
My configs:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'ely-usuario'],
],
],
Another weird thing that you might noticed is that 'enableStrictParsing' is false, in the yii2 guide it says to be true but for me it only works with false
Thanks
You need to change the code in your configs.I hope you will get an idea from the following code.It works fine for me!
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(ely-usuario)>/<action>/<id:\d+>' => '<controller>/<action>',
],
],
And in your controller please check your specific action.It must be coded like as:
public function actionTransactions($id=null){
if($id!=null){
//retrieve single row
}else{
//retrieve multiple rows
}
Also please check this link for reference:
Why RESTfull API request to view return 404 in Yii2?
I hope it helps!
So I have this PromptComponent and the content will be updated by a service data.
Now I am trying to do the unit test this component which has this service dependency.
Here is the Component:
export class PromptComponent {
#Input() module: any;
#select() selectedPrompt;
constructor(private moduleService: ModuleService){}
}
Here is the service:
getQuote(): Promise<string> {
return new Promise(resolve => {
setTimeout( () => resolve(this.nextQuote()), 500 );
});
}
private nextQuote() {
if (this.next === quotes.length) { this.next = 0; }
return quotes[ this.next++ ];
}
Here is the unit test code:
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
PromptComponent,
],
providers: [ ModuleService]
});
fixture = TestBed.createComponent(PromptComponent);
component = fixture.componentInstance;
const moduleService = fixture.debugElement.injector.get(ModuleService);
spy = spyOn(moduleService, 'getQuote').and.returnValue(Promise.resolve(testService));
I referred to the official demo on the angular documentation
https://angular.io/resources/live-examples/testing/ts/eplnkr.html, as inside the TwainComponent.spec.
Something really weird is that as expected, inside the official demo, the TwainComponent test passed smoothly. But I, on the other hand, got this strange injectionError. If I remove the parameter inside the constructor, I am able to create the fixture instance but get injection error when it runs thorught the injector.get() part. But as soon as I put the private moduleService:ModuleService back inside the component constructor, I will get fixture undefined and also with the injection error.
So that means I cannot even execute my code to the actual service injection part (fixture.debugElement.injector.get(ModuleService)), I already got the error.
Does anybody have the same issue?
My code is almost identical to the demo code on Plunker. Have no clue how to move forward....
Got a couple of problems with my unit testing with Jasmine. First one:
I need to test this in a Component called CaseList:
gotoDetail(case: Case){
this._router.navigate(['CaseDetail', {"id": case.id}]);
}
All my attempts at the tests give the error is this._router is undefined, well that's because I haven't defined it in my test, as I can't figure out how! I haven't even come up with any good attempts at the tests, as I have no idea how to proceed. So that's why I haven't posted any attempt here...
Edit: The part in the router-test which is related to to the problem above, but I test all the routing in a separate file! This test works!
it('Should navigate to Case Detail List', (done) => {
router.navigate(['CaseDetail', {id: 'test'}]).then(() => {
expect(location.path()).toEqual('/casedetail/test');
done();
}).catch(e => done.fail(e));
});
Second tests from a Detail Component (where user is navigated after choosing case) :
addStep(){
this.case.getSteps().push(new Step());
}
I also have a remove method I need to test:
removeStep(step: Step){
this.case.removeStep(step);
}
Constructor for this component:
constructor(public _routeParams: RouteParams, public _service: Service) {
this.case = _service.getById(_routeParams.get('id'));
}
So the test I tried doing for the add-method:
it('passes new step to case-class', () => {
spyOn(case, 'addStep')
.and.returnValue(Observable.of({complete: true}))
caseDetail.addStep();
expect(case.addStep).toHaveBeenCalledWith(step);
});
So these methods call the methods that are in a separate class called "Case".
The error I'm getting when testing these are that case is null. I guess the routing and service messes it up, as in the same Component I have a other "identical" methods, and testing those works fine. But they belong to a different class.
Method in same component, referring to a "Step"-class:
addFeedback(step: Step){
step.addFeedback(new Feedback());
}
The testing works perfectly:
it('passes feedback value to Step class', () => {
spyOn(step, 'addFeedback')
.and.returnValue(Observable.of({complete: true}))
caseDetail.addFeedback(step);
expect(step.addFeedback).toHaveBeenCalledWith(feedback);
})
So obviously in testing the component I should have everything defined that is needed, since the testing of the feedback method works. I just need to define the "case" object somehow, so that it doesn't complain about it being null.
Well hopefully you get my problem at hand and hopefully you can help! :)
For your test cases to work, you will have to add router and case as a provider before the test.
Routing Example:
import {RootRouter} from 'angular2/src/router/router';
import {Location, RouteParams, Router, RouteRegistry, ROUTER_PRIMARY_COMPONENT} from 'angular2/router';
import {SpyLocation} from 'angular2/src/mock/location_mock';
import {provide} from 'angular2/core';
describe('Router', () => {
let location, router;
beforeEachProviders(() => [
RouteRegistry,
provide(Location, {useClass: SpyLocation}),
provide(Router, {useClass: RootRouter}),
provide(ROUTER_PRIMARY_COMPONENT, {useValue: App}),
]);
beforeEach(inject([Router, Location], (_router, _location) => {
router = _router;
location = _location;
}));
it('Should be able to navigate to Home', done => {
router.navigate(['Index']).then(() => {
expect(location.path()).toBe('');
done();
}).catch(e => done.fail(e));
});
});
Case Provider:
import {Case} from '../case';
beforeEachProviders(() => [
provide(case, Case)
]);
I was following this tutorial for setting up Facebook PHP SDK 5.0 extension in my Yii 2.0 project. And it works as expected, but every time (in any of the controllers) I need to use some of the features from here this, I need to make an instance like this:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5',
// . . .
]);
and later use it:
// Send a GET request
$response = $fb->get('/me');
// Send a POST request
$response = $fb->post('/me/feed', ['message' => 'Foo message']);
// Send a DELETE request
$response = $fb->delete('/{node-id}');
but I'm not sure how practical is this, to make an instance of an object in every action/controller where I need to use it. I want to add this data as a general data in the config file. So I tried something like this:
'components' => [
.
.
'facebook' => [
'class' => 'Facebook\Facebook',
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5'
],
.
.
and later in the actions I want to take this value like:
$fb = Yii::$app->facebook;
and after that do all the operations mentioned above. So I want to generalize the values in the config file like all other extensions, but I keep getting the error:
Facebook\Exceptions\FacebookSDKException
Required "app_id" key not supplied in config and could not find fallback environment variable "FACEBOOK_APP_ID"
Is it possible this to be entered in web config file, and with that, to avoid creating the object with same credentials before each Facebook call?
EDIT 1:
Reply to #machour response:
I followed your suggestion and It was still throwing the same error. Then I found it working as follows:
<?php
namespace your\namespace;
use Facebook\Facebook;
class MyFacebook extends Facebook {
public $app_id = '{app-id}';
public $app_secret = '{app-secret}';
public $default_graph_version = 'v2.5';
public function __construct()
{
parent::__construct([
'app_id' => $this->app_id,
'app_secret' => $this->app_secret,
'default_graph_version' => $this->default_graph_version
]);
}
}
And then:
'components' => [
.
.
'facebook' => [
'class' => 'your\namespace\MyFacebook'
]
At some point this is acceptable solution, since the redundancy is eliminated. The keys are not only at one place.
But do you have any idea how to transfer all the keys to the config file instead of the MyFacebook class?
The problem is that Facebook\Facebook doesn't implement $app_id, $app_secret and $default_graph_version as public properties, so your parameters are not taken in account when Yii builds the object declared in your component.
One way to fix that is to create your own class that extends Facebook, with those public properties, and to correctly call Facebook\Facebook constructor from it's own constructor. And then point your configuration to that new class instead :
<?php
namespace your\namespace;
use Facebook\Facebook;
class MyFacebook extends Facebook {
public $app_id;
public $app_secret;
public $default_graph_version;
public function __construct()
{
parent::__construct([
'app_id' => $this->app_id,
'app_secret' => $this->app_secret,
'default_graph_version' => $this->default_graph_version
]);
}
}
And then:
'components' => [
.
.
'facebook' => [
'class' => 'your\namespace\MyFacebook',
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5'
],
That should do the trick.