Using Mango within a unit test in Kohana - php

So I'm switching our application to use Mango rather than the built in ORM within Kohana. I've switched over all the necessary application code to work as expected, but when our CI server runs through our unit tests, I get a "Class 'Mango' not found" error.
Tests provided are dumbed down, but the style I use in the UnitTest is exactly the same way I use them in a regular GET request. It works when I do a GET, but the unit test fails. Now hopefully unrelated, I cannot reproduce this locally, but can't ever get the unit test to work on our CI server.
My guess is that I'm not loading the module properly, but like I said, it works correctly in the application and only my unit tests are failing (with FATAL ERRORs).
application/classes/Model/User.php
class Model_User extends Mango {
protected $_fields = array(
'user_id' => array('type' => 'string', 'required'=>TRUE),
'first_name' => array('type' => 'string', 'required'=>TRUE),
'last_name' => array('type' => 'string', 'required'=>TRUE),
);
}
application/tests/UserTest.php
Class UserTest extends Unittest_TestCase
{
public function testUserCreation()
{
$user_data = array(
"user_id" => "1234asdf",
"first_name" => "Test",
"last_name" => "User",
);
$new_user = Mango::factory("User", $user_data);
$this->assertEquals($user_data, $new_user->as_array());
}
}
EDIT: Here's a link to the Mango module I've brought in: https://github.com/Wouterrr/MangoDB

If anyone stumbles upon this via google, I solved the issue. It appears as though our applications nginx config handles capitalizations more nicely than the CLI. Upon changing "Mango" to "mango" I saw the error message change to not finding it's parent class (for the same casing reasons). While I imagine I could have just changed all the casing, Kohana has a function for casing issues, so in application/bootstrap.php, you just have to run both of the following:
spl_autoload_register(array('Kohana', 'auto_load'));
spl_autoload_register(array('Kohana', 'auto_load_lowercase'));

Related

CakePHP 4, AWS Elasticsearch 7 - The security token included in the request is invalid

So I'm running CakePHP 4 on an EC2 instance, AWS ES 7 and I've setup the ElasticSearch plugin in CakePHP.
composer require cakephp/elastic-search "^3.0"
I've added the elastic datasource connection in config/app.php
'elastic' => [
'className' => 'Cake\ElasticSearch\Datasource\Connection',
'driver' => 'Cake\ElasticSearch\Datasource\Connection',
'host' => 'search-DOMAIN.REGION.es.amazonaws.com',
'port' => 443,
'transport' => "AwsAuthV4",
'aws_access_key_id' => "KEY",
'aws_secret_access_key' => "SECRET",
'aws_region' => "REGION",
'ssl' => 'true',
],
.. and I've activated the plugin
use Cake\ElasticSearch\Plugin as ElasticSearchPlugin;
class Application extends BaseApplication
{
public function bootstrap()
{
$this->addPlugin(ElasticSearchPlugin::class);
I've manually added 1 index record to ES via curl from the EC2 instance. So I know the communication between EC2 and ES works.
curl -XPUT -u 'KEY:SECRET' 'https://search-DOMAIN.REGION.es.amazonaws.com/movies?pretty' -d '{"director": "Burton, Tim", "genre": ["Comedy","Sci-Fi"], "year": 1996, "actor": ["Jack Nicholson","Pierce Brosnan","Sarah Jessica Parker"], "title": "Mars Attacks!"}' -H 'Content-Type: application/json'
I also managed to search for this record via curl without any problems.
In the AppController.php I tried this simple search just to see if the plugin works and for the life of me, I can't get it to work.
# /src/Controller/AppController.php
...
use Cake\ElasticSearch\IndexRegistry;
class AppController extends Controller
{
public function initialize(): void
{
parent::initialize();
$this->loadModel('movies', 'Elastic');
$query = $this->movies->find('all');
$results = $query->toArray();
I'm getting the following error:
Client error: POST
https://search-DOMAIN.REGION.es.amazonaws.com/movies/movies/_search
resulted in a 403 Forbidden response: {"message":"The security token
included in the request is invalid."}
Elastica\Exception\Connection\GuzzleException
Seems like the plugin adds the 'Index' name twice for some reason. I looked everywhere for a setting that I might have missed. If I copy and paste the above URL and remove the duplicate Index from the URL in a browser it works fine.
https://search-DOMAIN.REGION.es.amazonaws.com/movies/_search
Am I missing something here?
I've even tried this method, and I get the same problem with duplicated index values in the url.
$Movies = IndexRegistry::get('movies');
$query = $Movies->find('all');
$results = $query->toArray();
I've tried a new/clean CakePHP instance and I get the same problem? Is there something wrong with the plugin? Is there a better approach to communicate with ES via CakePHP?
I'm not familiar with the plugin and Elasticsearch, but as far as I understand, one of the movies is the index name, and one of them is the type name, where the type name - at least according to the documentation - should be singular, ie the path would instead be expected to look like:
/movies/movie/_search
Furthermore, Index classes assume that the type mapping has the singular name of the index. For example the articles index has a type mapping of article.
https://book.cakephp.org/elasticsearch/3/en/3-0-upgrade-guide.html#types-renamed-to-indexes
Whether that would be the correct path with respect to what is supported by the used Elasticsearch version, that might be a different question.
You may want to open an issue over at GitHub.

Unauthorized users return status 500 instead of 401 when using phpunit test

I'm working with buddy.works for continuous integration of my project. The issue is though, my phpunit tests pass on my local computer but fail on the buddy works pipeline.
I have tried googling and reading for over two days now and although I found many similar problems, I haven't encountered a solution that can even point me in the right direction.
public function test_orders_route_unauthenticated_user () {
$data = [
'orderID' => '001241',
'sku' => '123456',
'quantity' => 9,
'pricePerUnit' => 78,
'priceTotal' => 702,
];
$this->json('POST', 'api/orders', [$data])->assertStatus(401);
}
The test fails with status code 500 instead of 401 and I don't know what is causing this.
Edit:
Laravel 5.8.17, php unit which comes integrated with it, works as expected until being run on buddy works.
Had the same problem while testing my laravel 8 api.
I had to send header with my request.
public function testGetUsersWithoutAuthentication()
{
$response = $this->withHeaders(['Accept' => 'application/json',])->get('api/users');
$response->assertStatus(401);
}

UndefinedMethodException in appDevDebugProjectContainer.php line 1606

I'm somewhat lost what I've done here. My app was functioning for a while, and at some point I changed something that caused this error. I've cleared my dev cache. At some point I wonder if I updated my composer and caused something because I wasn't even touching the php side of my app when I broke it.
Here's the full error:
UndefinedMethodException in appDevDebugProjectContainer.php line 1606:
Attempted to call an undefined method named "AddAthlete" of class "FOS\RestBundle\Util\FormatNegotiator".
That particular function looks like this:
protected function getFosRest_ExceptionFormatNegotiatorService()
{
$this->services['fos_rest.exception_format_negotiator'] = $instance = new \FOS\RestBundle\Util\FormatNegotiator();
$instance->AddAthlete($this->get('fos_rest.request_matcher.0dfc4cce134bee15f08405cb5cea4845b13ff7d8c8f779004218432a2c552bd0cd9f9d27'), array('priorities' => array(0 => 'html', 1 => 'json', 2 => 'xml'), 'fallback_format' => NULL, 'prefer_extension' => '2.0', 'methods' => NULL, 'stop' => false));
return $instance;
}
I'm not particularly familiar with how this gets generated. Could someone help? If needed, here's a link to my bundle on github
Turns out I must have had some composer issues because when I pulled the repo down to my other machine, it worked fine (with the exception of new bugs, yay!)

Grid column does not work on Server

I want to add a column in order grid whose value come from another block.
The code I used in Grid.php are as given below :
$this->addColumn('sum_total', array(
'header' => Mage::helper('sales')->__('G.T.Used'),
'column_css_class' => 'a-right',
'type' => 'currency',
'currency' => 'base_currency_code',
'renderer' => 'Marketplace_Mpshippingmanager_Block_Adminhtml_Sumtotal'
));
Here is the code used in block :
class Marketplace_Mpshippingmanager_Block_Adminhtml_Sumtotal extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function input_render(Varien_Object $row)
{
//echo '$'.(float)($row['base_subtotal']-$row['base_grand_total']);
printf("$%.2f", ($row['base_subtotal'] - $row['base_grand_total']));
}
}
It work on localhost but make problem on server.
why?
If some one help is more appreciable.
Is localhost windows? Server Linux? I can't say for certain but I ran into this earlier in the week turned out Windows is case insensitive and Linux is case sensitive. The Varien Autoloader which parses all classnames and strings was throwing in '\' where I didn't want them and magento could not find my files.
I do not know if this helps but, it was so similar to my problem I figured I would put in my 2 cents. Here was my thread with the problem explained by a much smarter person. https://stackoverflow.com/questions/30085012/magento-mass-action-grid-does-not-update

How to insert a sample data with PHPSpec and Laravel

I change my environment to testing on basespec with sqlite as driver and store in the memory.
function it_should_validate()
{
// Create the user
$data = array('email' => 'test#gmail.com', 'password' => 'password', 'remember' => false);
$this->validate($data)->shouldBe(true);
}
How do i insert information to the testing database whenever i run the test? Right now the test fails because it always return false;
What you're trying to do is integration testing.
PhpSpec is a tool for unit testing. It puts a big emphasis on testing in isolation. That means, that the only object that's created is the object being tested. All its dependencies should be stubbed or mocked.
In other words there should be no database involved in specs you write.
You didn't show the full example of a class you're trying to spec, so it's hard to advice how your spec should look like.
Adviced reading:
Test Driven Development by Example
Growing Object-Oriented Software Guided by Tests

Categories