I have a method in a class that scans a directory and creates an array of all the subdirectories. It's pretty simple and works great. However, I would like to add a unit test for this method and I am having a hard time figuring out how.
Here's my problem: I can create a virtual file system using vfsstream and it works fine. However, I can't pass that to my class to create an array from. It needs a real directory to scan. I want to test against a controlled directory (obviously so I know exactly what will the be result of every scan so I can test it). The scanned directory in production may change frequently.
So, my only solution is to create a test-specific fake directory in my test folders, pass that path to my scanner, and then check it against what I know to be in that fake directory. Is this best practice or am I missing something?
Thank you!
Here is some code:
The Test
function testPopulateAuto()
{
$c = new \Director\Core\Components\Components;
// The structure of the file system I am checking against. This is what I want to generate.
$check = array(
'TestFolder1',
'TestFolder2',
);
$path = dirname( __FILE__ ) . "/test-file-system/"; // Contains TestFolder1 and TestFolder1
$list = $c->generateList( $path ); // Scans the path and returns an array that should be identical to $check
$this->assertEquals($check, $list);
}
Sorry if I misunderstood your question, but scandir should work with custom stream. Example:
$structure = array(
'tmp' => array(
'music' => array(
'wawfiles' => array(
'mp3' => array(),
'hello world.waw' => 'nice song',
'abc.waw' => 'bad song',
'put that cookie down.waw' => 'best song ever',
"zed's dead baby.waw" => 'another cool song'
)
)
)
);
$vfs = vfsStream::setup('root');
vfsStream::create($structure, $vfs);
$music = vfsStream::url('root/tmp/music/wawfiles');
var_dump(scandir($music));
Output:
array(5) {
[0]=>
string(7) "abc.waw"
[1]=>
string(15) "hello world.waw"
[2]=>
string(3) "mp3"
[3]=>
string(24) "put that cookie down.waw"
[4]=>
string(19) "zed's dead baby.waw"
}
Related
I am pretty much below beginner level for REGEX/REGEXP and have hit a blocking point in a project I am working in, where I am trying to get the ids for posts that match the search criteria , but I want to restrict the search between 2 sub-strings. I am trying to figure out is how to write the REGEXP in the meta_query:
$args = array(
'post_type'=> 'custom',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $page,
'meta_query' => array(
array(
'key' => 'key',
'value' => "title*".$search."*_title",
'compare' => 'REGEXP',
)
),
);
And an example of the field in the DB :
a:302:{s:5:"title";s:10:"Test title";s:6:"_title";s:19:"
Unfortunately none of the combinations I tried based on documentation of SQL REGEXP won't return any values and I am trying to understand how I can pull this off and would appreciate any input.
Also would rather stick to WP_Query for now even though an SQL LIKE "%title%{$search}%_title%" works perfectly , so an alternative solution would be how to set the compare to 'LIKE' and parse it '%' since that is not possible out of the box as the % get escaped I believe.
I'm trying to create an ad using the php sdk.
I can create the campaign, targeting, adset, and the creative (which returns a creative_id that I can validate using the graph explorer).
But when I finally run the code to create the ad itself, I get an exception that looks like this:
"error_user_title" => "Missing creative spec"
"error_user_msg" => "No creative spec found for given adgroup."
I just can't find anything referring to this error.
Below is the relevant portion of my code:
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::LINK => $route,
AdCreativeLinkDataFields::MESSAGE => $petition_statement,
AdCreativeLinkDataFields::NAME => $banner_title,
AdCreativeLinkDataFields::IMAGE_HASH => $image_hash,
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => $pageid,
AdCreativeObjectStorySpecFields::INSTAGRAM_ACTOR_ID=>$instagram_id,
AdCreativeObjectStorySpecFields::LINK_DATA=>$link_data
));
$creative = new AdCreative(null,$account_id);
$creative->setData(array(
AdCreativeFields::TITLE => $banner_title,
AdCreativeFields::BODY => $banner_subtitle,
AdCreativeFields::IMAGE_HASH => $image_hash,
AdCreativeFields::OBJECT_TYPE => 'SHARE',
AdCreativeFields::OBJECT_STORY_SPEC=>$object_story_spec
));
$creative->create();
echo 'Creative ID: '.$creative->id . "\n";
$ad = new Ad(null, $account_id);
$ad->setData(array(
AdFields::NAME => $short_name,
AdFields::ADSET_ID => $adset->id,
AdFields::CREATIVE => $creative,
AdFields::TRACKING_SPECS => array(array(
'action.type' => 'offsite_conversion',
'fb_pixel' => $pixel_code,
))
));
$ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED));
Appreciate any help.
I've often said that the only skill you need to be a successful developer is the ability to agonize over a problem for days, read through source code, google it, refactor, rewrite and then realize you forgot something fucking obvious.
AdFields::CREATIVE => $creative,
should read
AdFields::CREATIVE => $creative->id,
But the ability to persist isn't the skill you need. The real skill is to somehow resist the overwhelming urge to chuck your computer out the window and do something productive with your life instead.
After hours of testing it seems Trevor's answer is incorrect. This is the right syntax:
AdFields::CREATIVE => array('creative_id'=>$creative->id)
I hope anyone can help me out with my testing environment.
my setup
I am implementing unit tests based on phpunit 3.7.22 with the cake release 2.6.9. Running on ubuntu 12.04 LTS with PHP 5.4.43-1 and postgresql 9.1.
I immplemented controller tests mocking cakes Auth Component to have a user in the session, since my tests depent on that. My controllers return json results, since its an API for a JS-based frontend. I call my controller methods using the testAction() call of a generated controller.
<?php
App::uses('RequesttypesController', 'Svc.Controller');
class RequesttypesWithResultControllerTest extends ControllerTestCase
{
public $fixtures = array(
'app.requesttype',
'app.user',
'app.privilege',
'app.groupsprivilege',
'app.groupsuser',
'app.groupscompany',
'app.company',
);
/**
* Mock the requesttype object so that it can return results depending on the desired outcome
*
* #see CakeTestCase::setUp()
*/
public function setUp()
{
parent::setUp();
$this->controller = $this->generate('Svc.Requesttypes', array(
'models' => array(
'Requesttype'
),
'components' => array(
'Auth' => array(
'user'
),
'Session',
'RequestHandler'
)
));
$this->controller->Auth->staticExpects($this->any())
->method('user')
->will($this->returnValue(array(
'id' => 123,
'username' => 'myTestUser',
'company' => 'myTestCompany',
'usertype_id' => '456',
))
);
$authResult = $this->controller->Auth->user();
}
public function tearDown()
{
parent::tearDown();
unset($this->controller);
}
/**
* A logged in user produces a number of requesttypes
*/
public function testLoggedInUser()
{
$result = $this->testAction('/svc/requesttypes/getMyRequesttypes', array('return' => 'vars'));
$this->assertNotEmpty($this->vars, 'Did not receive webservice response');
$this->assertTrue(isset($this->vars['data']['code']), 'Received invalid webservice response');
$this->assertEqual($this->vars['data']['code'], SvcAppController::RESPONSE_CODE_SUCCESS);
}
}
?>
This test passes without errors. Now I want to test my controller-action with different setups, for example users with a different usertype, from a different company, and so on. If I now create a second test-method in my RequesttypesWithResultControllerTest-class, calling the same testAction-url, i get a MissingActionException saying:
"Action RequesttypesController::() could not be found."
It seems that the testAction calls an empty controller-action, even if the action-url is passed as a parameter. I tried reinitializing the controller by nulling it and calling $this->generate() again, but this does not help either.
Of course I can help myself out by creating an own test-controller for every test ending up in a bunch of duplicate test-code, but this somehow seems not right to me.
Am I misusing the test-environment or how can this exception be explained? Any ideas?
Thanks in advance for sharing my headache!
After some further code debugging we finally found the error. We accidently changed the require statement of the last line of the /Config/routes.php file to a require_once because of some "Class already defined Exceptions" thrown in the test-environment.
Wrong routes.php:
require_once CAKE . 'Config' . DS . 'routes.php';
For the application itself that made no difference, since the routes are only needed to be initialized once per request. But in a test-environment, the routes are reinitialized several times, which was not possible anymore with the require_once include.
This is how the line is supposed to look like, which it does by default:
Correct routes.php:
require CAKE . 'Config' . DS . 'routes.php';
i am using doctrine 2 with Zendframework 2
i am trying to return an object but keep getting this error:
Your proxy directory must be writable
this is my query:
$this->getEntityManager()
->getRepository('Messages\Entity\Messages')
->findOneBy(array('id' => 6,
'receiverId' => 16
));
However, the same query return an array without any problems re:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(array('u'))
->from('Messages\Entity\Messages','u')
->where('u.id = :id')
->andWhere('u.receiverUserId = :receiverId')
->setParameter('receiverId',16)
->setParameter('id',(int)6);
$query = $qb->getQuery();
return $data = $query->getArrayResult();
If you are using Setup::createAnnotationMetadataConfiguration you can simply fix it by
Create the following directory in your project root. data/DoctrineORMModule/Proxy
chmod -R 755 data/DoctrineORMModule/Proxy
In your bootstrap include the path to the data directory as in:
Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src/models"), $this->isDevMode, "data/DoctrineORMModule/Proxy")
That fixed it for me.
Proxies are simple classes which extends your actual entities and used internally by doctrine to hydrate associations of your entities by lazy-loading them. Doctrine decides to use or not to use a proxy instances on runtime for different situations and it really depends on your queries and associations in your entities. You may want to read about this subject in-depth at official documentation.
In your case, doctrine trying to generate a proxy class for your Messages entity but your proxy directory is simply not writable as error output's said.
This seems like misconfiguration of DoctineModule. (Assuming that you are using DoctrineORMModule to integrate doctrine with ZF2) Doctrine needs a writeable directory to put that generated proxy classes. For ZF2's view, data directory on application root perfectly fits for this requirement.
Make sure existence of the line below in your public/index.php:
chdir(dirname(__DIR__));
and try to use a configuration like this:
<?php
/**
* module/Application/config/module.config.php
*/
return array(
'router' => array(
// ...
),
// ...
'doctrine' => array(
'driver' => array(
//...
),
/**
* Generating proxies on runtime and using array cache instead of apc(u)
* greatly reduces the performance. So, you may want to override
* this settings on production environment.
*/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
),
),
)
);
If you are using Fedora 24, enter the following commands:
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/YOUR_SITE/data';
restorecon -v '/var/www/html/YOUR_SITE/data'
With this, you are changing the label of the /var/www/html and giving write permission to httpd.
I am using CakePHP to develop a website and currently struggling with cookie.
The problem is that when I write cookie with multiple dots,like,
$this->Cookie->write("Figure.1.id",$figureId);
$this->Cookie->write("Figure.1.name",$figureName);`
and then read, cakePHP doesn't return nested array but it returns,
array(
'1.id' => '82',
'1.name' => '1'
)
I expected something like
array(
(int) 1 => array(
'id'=>'82',
'name'=>'1'
)
)
Actually I didn't see the result for the first time when I read after I write them. But from second time, result was like that. Do you know what is going on?
I'm afraid it doesn't look as if multiple dots are supported. If you look at the read() method of the CookieComponent (http://api.cakephp.org/2.4/source-class-CookieComponent.html#256-289), you see this:
277: if (strpos($key, '.') !== false) {
278: $names = explode('.', $key, 2);
279: $key = $names[0];
280: }
and that explode() method is being told to explode the name of your cookie into a maximum of two parts around the dot.
You might be best serializing the data you want to store before saving and then deserializing after reading as shown here: http://abakalidis.blogspot.co.uk/2011/11/cakephp-storing-multi-dimentional.html