代码之家  ›  专栏  ›  技术社区  ›  BARNOWL

Symfony功能单元测试未能断言500与200相同

  •  0
  • BARNOWL  · 技术社区  · 6 年前

    我正在尝试测试注册方法,但是我在日志中看到了这个。

    {“route”:“signup”,“route_parameters”:{“route”:“signup”,“_controller”:“App\controller\UserController::signup”},“request_uri”: http://localhost/signup “,”方法“:”POST“} 带着匿名令牌。[[][2018-08-30 00:13:50]条令。调试: “启动交易”[[[][2018-08-30 00:13:50]条令。调试: 异常原则\DBAL\Exception\TableNotFoundException:“一个 执行“插入到用户(电子邮件、用户名, 是活动的,密码,角色)值吗?, ?, ?, ?, ?)':SQLSTATE[HY000]: 一般错误:1没有这样的表:user“at 第63行{“异常”:“[对象] (Doctrine\DBAL\Exception\TableNotFoundException(代码:0):一个 是活动的,密码,角色)值吗?, ?, ?, ?, ?)':\n\nSQLSTATE[HY000]:一般错误:1没有这样的表:用户位于 条令\DBAL\Driver\PDOException(代码:HY000):SQLSTATE[HY000]: 一般错误:1没有这样的表:用户位于 /Applications/MAMP/htdocs/myúu project/vendor/docine/dbal/lib/docine/dbal/Driver/PDOConnection.php:82, PDOException(代码:HY000):SQLSTATE[HY000]:常规错误:1没有 表:用户 []

    这是个错误

    Failed asserting that 500 is identical to 200.

    <?php
    
    namespace App\Tests\Controller;
    
    use App\Entity\Product;
    use App\Entity\Category;
    use App\Entity\User;
    use App\Repository\UserRepository;
    use App\Repository\CategoryRepository;
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\Request;
    use Doctrine\DBAL\Schema\SchemaException;
    use Doctrine\ORM\Tools\SchemaTool;
    
    class SignUpControllerTest extends WebTestCase
    {
    
        /**
         * @var \Doctrine\ORM\EntityManager
         */
        private $em;
    
    
        /**
         * {@inheritDoc}
         */
    
        protected function setUp()
        {
            self::bootKernel();
            $this->em = static::$kernel->getContainer()
                ->get('doctrine')
                ->getManager();
    
            parent::setUp();
        }
    
        public function testUserCreate()
        {
            $client = static::createClient();
            $client->request('POST', '/signup', [], [], [], json_encode([
                'user' => [
                    'username' => 'chuck_norris',
                    'password' => 'foobar',
                    'email' => 'chuck@norris.com',
                    'roles' => 'ROLE_USER'
                ],
            ]));
            $response = $client->getResponse();
            $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
            $data = json_decode($response->getContent(), true);
            $this->assertArrayHasKey('user', $data);
            $this->assertSame('chuck@norris.com', $data['user']['email']);
            $this->assertSame('chuck_norris', $data['user']['username']);
    
        }
    

    UserController.php用户控制器

    /**
     * @Route("/signup", name="signup")
     */
    
    public function signup(Request $request, UserPasswordEncoderInterface $passwordEncoder )
    {
        $user = new User();
    
        $entityManager = $this->getDoctrine()->getManager();
    
        $user->setEmail($request->get('email'));
        $user->setPlainPassword($request->get('password'));
        $user->setUsername($request->get('username'));
        $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($password);
    
        $entityManager->persist($user);
        $entityManager->flush();
    
        return $this->redirectToRoute('login');
    
    
    
    }
    

    <?php
    
    namespace App\Entity;
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    use Symfony\Component\Security\Core\User\UserInterface;
    
    /**
     * @ORM\Entity
     * @UniqueEntity(fields="email", message="Email already taken")
     * @UniqueEntity(fields="username", message="Username already taken")
     */
    class User implements UserInterface, \Serializable
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=190, unique=true)
         * @Assert\NotBlank()
         * @Assert\Email()
         */
        private $email;
    
        /**
         * @ORM\Column(type="string", length=190, unique=true)
         * @Assert\NotBlank()
         */
        private $username;
    
        /**
         * @ORM\Column(name="is_active", type="boolean")
         */
        private $isActive;
    
    
        /**
         * @Assert\NotBlank()
         * @Assert\Length(max=190)
         */
        private $plainPassword;
    
        /**
         * The below length depends on the "algorithm" you use for encoding
         * the password, but this works well with bcrypt.
         *
         * @ORM\Column(type="string", length=64)
         */
        private $password;
    
        /**
         * @ORM\Column(type="array")
         */
        private $roles;
    
        /**
         * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="user")
         */
        private $products;
    
        public function __construct()
        {
            $this->roles = array('ROLE_USER');
            $this->isActive = true;
            $this->products = new ArrayCollection();
        }
    
        // other properties and methods
    
        public function getEmail()
        {
            return $this->email;
        }
    
        public function isEnabled()
        {
            return $this->isActive;
        }
    
        public function setEmail($email)
        {
            $this->email = $email;
        }
    
        public function getUsername()
        {
            return $this->username;
        }
    
        public function setUsername($username)
        {
            $this->username = $username;
        }
    
        public function getPlainPassword()
        {
            return $this->plainPassword;
        }
    
        public function setPlainPassword($password)
        {
            $this->plainPassword = $password;
        }
    
        public function getPassword()
        {
            return $this->password;
        }
    
        public function setPassword($password)
        {
            $this->password = $password;
        }
    
        public function getSalt()
        {
            // The bcrypt and argon2i algorithms don't require a separate salt.
            // You *may* need a real salt if you choose a different encoder.
            return null;
        }
    
        public function getRoles()
        {
            return $this->roles;
        }
    
        public function eraseCredentials()
        {
        }
    
        /** @see \Serializable::serialize() */
        public function serialize()
        {
            return serialize(array(
                $this->id,
                $this->username,
                $this->password,
                // see section on salt below
                // $this->salt,
            ));
        }
    
        /** @see \Serializable::unserialize() */
        public function unserialize($serialized)
        {
            list (
                $this->id,
                $this->username,
                $this->password,
                // see section on salt below
                // $this->salt
            ) = unserialize($serialized, array('allowed_classes' => false));
        }
    
        /**
         * @return Collection|Product[]
         */
        public function getProducts(): Collection
        {
            return $this->products;
        }
    
        public function addProduct(Product $product): self
        {
            if (!$this->products->contains($product)) {
                $this->products[] = $product;
                $product->setUser($this);
            }
    
            return $this;
        }
    
        public function removeProduct(Product $product): self
        {
            if ($this->products->contains($product)) {
                $this->products->removeElement($product);
                // set the owning side to null (unless already changed)
                if ($product->getUser() === $this) {
                    $product->setUser(null);
                }
            }
    
            return $this;
        }
    }
    

    enter image description here

    0 回复  |  直到 6 年前