access database from bootstrap zend framework 2 (doctrine2) - doctrine-orm

How I can access to DB from bootstrap using official DoctrineORMModule ?
for example, in my controller:
$allusers = $this->getEntityManager()->getRepository('Users\Entity\User')->findAll();
but I can't access to getEntityManager() and getRepository() when i'm in bootstrap.
I follow up this guide: http://ivangospodinow.com/zend-framework-2-acl-setup-in-5-minutes-tutorial/
but I'm stuck when trying to connect to db
public function getDbRoles(MvcEvent $e){
// I take it that your adapter is already configured
$dbAdapter = $e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter');
...
}

Using doctrine you would need to fetch the EntityManager with
$entityManager = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager');
//in my case:
var_dump( $entityManager->getRepository('Users\Entity\User') );

Related

Why running a Laravel 9 test is deleting my database?

I am usgin Laravel 9 test tool.
I dropped the database, recreated and imported using SQL statement.
With all the database set, I used the browser to log in with my user, and everything worded just fine.
Then I ran the php artisan test --filter HomeControllerTest and all the database was deleted! HOW COME?????
here is the test code:
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class HomeControllerTest extends TestCase
{
use RefreshDatabase;
public function test_home_view_requires_authentication()
{
// Log in as the created user and try to access the home page
$response = $this->postJson('/login', [
'email' => 'test#example.com',
'password' => 'password',
]);
$response->assertStatus(302);
$response->assertRedirect(route('home'));
$response = $this->get(route('home'));
$response->assertOk();
$response->assertViewIs('home');
$response->assertViewHasAll(['corretora', 'propostas']);
}
/**
* Test if home page can be accessed by a guest user.
*
* #return void
*/
public function test_home_view_requires_guest()
{
$response = $this->get(route('home'));
$response->assertStatus(302);
$response->assertRedirect(route('login'));
}
}
Why my database was deleted? i do have a backup, of course, but should test do it?
There was not an error. The use of the use RefreshDatabase; do the erasing.
I remove it and now is working good.

Cannot test Swift_Mailer because service already initialized or test. alias ignored

I already found few informations like
Symfony2 access private services in tests
or
Replace Symfony service in tests for php 7.2
But I dont know why its not working.
I have an autowiring service.
class MailService {
public function __construct(\Swift_Mailer $mailer, Environment $twig)
{
$this->mailer = $mailer;
$this->twig = $twig;
}
}
config/services_test.yaml
services:
# default configuration for services in *this* file
_defaults:
#autowire: true # Automatically injects dependencies in your services.
#autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: true
test.swiftmailer.transport: '#Swift_Mailer'
And when I try to replace.
$mailer = $this->getMockBuilder(\Swift_Mailer::class)
->disableOriginalConstructor()
->getMock();
$mailer->expects($this->once())->method('send')->willReturn(1);
self::$container->set('swiftmailer.mailer.default', $mailer);
self::$container->set('swiftmailer.default', $mailer);
self::$container->set('swiftmailer.mailers', [$mailer]);
But I dont know why its not working.
Any ideas? :)

Resolve ISitecoreService using SimpleInjector

ISitecoreService accepts database name as a string parameter in its constructor (web or master)
ISitecoreService service = new SitecoreService("master"); //or
ISitecoreService service = new SitecoreService("web");
Is it possible I dynamically send database name as parameter to IoC and resolve it? for example I send web/master string parameter and get a new instance of ISitecoreService
Like this?
container.Register<ISitecoreService>(() => new SitecoreService("master"));
Expanding on Stevens answer as I have experience with Sitecore and I love Simpleinjector.
If you like you can get at the Sitecore configuration when your application is bootstrapping using the configuration factory, access the website site configuration and use the database property.
var sites = Sitecore.Configuration.Factory.GetSiteInfoList();
var website = sites.Single(s => s.Name == "website");
ISitecoreService service = new SitecoreService(website.Database);
container.Register<ISitecoreService>(() => service);
This way your SitecoreService will be newed up with the same database that is defined in the website configuration.

Swagger editing api-docs?

I'm new to developing rest web services and now, I'm trying to add authenication via this url:
http://developers-blog.helloreverb.com/enabling-oauth-with-swagger/
Unfortunately, I'm stuck on the first step, when it says to edit the resource listing, which I believe is the api-docs, right? So, how am I supposed to edit that if its generated by the service and not stored as a file anywhere?
My Swagger version is 1.2
The link you provided refers to Swagger 1.2 but the latest version is Swagger 2.0
As a starting point, you may consider using editor.swagger.io and reviewing the petstore example, which contains authentication-related setting
Take a look at the pet store sample from the 1.3.12 tag which was the last version producing Swagger 1.2 - https://github.com/swagger-api/swagger-core/tree/v1.3.12/samples/java-jaxrs.
Specifically, you need to add the definitions to something like your Bootstrap class:
public class Bootstrap extends HttpServlet {
static {
// do any additional initialization here, such as set your base path programmatically as such:
// ConfigFactory.config().setBasePath("http://www.foo.com/");
ApiInfo info = new ApiInfo(
"Swagger Sample App", /* title */
"This is a sample server Petstore server. You can find out more about Swagger " +
"at http://swagger.io or on irc.freenode.net, #swagger. For this sample, " +
"you can use the api key \"special-key\" to test the authorization filters",
"http://helloreverb.com/terms/", /* TOS URL */
"apiteam#wordnik.com", /* Contact */
"Apache 2.0", /* license */
"http://www.apache.org/licenses/LICENSE-2.0.html" /* license URL */
);
List<AuthorizationScope> scopes = new ArrayList<AuthorizationScope>();
scopes.add(new AuthorizationScope("email", "Access to your email address"));
scopes.add(new AuthorizationScope("pets", "Access to your pets"));
List<GrantType> grantTypes = new ArrayList<GrantType>();
ImplicitGrant implicitGrant = new ImplicitGrant(
new LoginEndpoint("http://petstore.swagger.wordnik.com/oauth/dialog"),
"access_code");
grantTypes.add(implicitGrant);
AuthorizationType oauth = new OAuthBuilder().scopes(scopes).grantTypes(grantTypes).build();
ConfigFactory.config().addAuthorization(oauth);
ConfigFactory.config().setApiInfo(info);
}
}

Facebook Graph API showing blank page

I want to use facebook graph api to get the event details of a specific page.But I keep getting a blank page back.
<?php
error_reporting(E_ALL);
require 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
class HomeController extends BaseController {
public function index()
{
FacebookSession::setDefaultApplication('APP_ID', 'APP_SECRET');
$session = FacebookSession::newAppSession();
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/1531904510362357/'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
dd($graphObject);
}
$session should = new FacebookSession('accesstoken')
Another thing I've learned is that the new api (v2.0+) is very very strict on what is displayed. For example, you can no longer open up someones feed that doesn't also have an access token to your application.
I would go back and read on the permissions and make sure that the correct permissions are set and validated for your application.